aboutsummaryrefslogtreecommitdiff
path: root/Histacom2/OS/Win95/Win95Apps/MineSweeper/Square.cs
blob: 4f802408a553f466900f3fdbe83fb75be293eb26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using Histacom2.Engine;

namespace Histacom2.OS.Win95.Win95Apps.MineSweeper
{
    class Square
    {
        public event EventHandler Dismantle;
        public event EventHandler Explode;

        private Button _button;
        private bool _dismantled = false;
        private bool _questioned = false;
        private Game _game;
        private bool _minded = false;
        private bool _opened = false;
        private bool _exploded = false;
        private int _x;
        private int _y;

        public Square(Game game, int x, int y)
        {
            _game = game;
            _x = x;
            _y = y;
            _button = new Button();
            Button.Text = "";

            int w = _game.Panel.Width / _game.Width;
            int h = _game.Panel.Height / _game.Height;

            _button.Width = 16;
            _button.Height = 16;
            _button.Left = w * X;
            _button.Top = h * Y;
            _button.Font = new Font(TitleScreen.pfc.Families[0], 16F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            _button.Click += new EventHandler(Click);
            _button.MouseDown += new MouseEventHandler(DismantleClick);
            _button.MouseUp += new MouseEventHandler(MiddleClick);
            _button.FlatStyle = FlatStyle.Flat;
            _button.FlatAppearance.BorderSize = 0;
            _button.BackgroundImage = Properties.Resources.minesweepSquare;
            _button.BackgroundImageLayout = ImageLayout.Stretch;

            _game.Panel.Controls.Add(Button);
        }

        private void MiddleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Middle && Opened)
            {
                int c = 0;
                if (_game.IsBomb(X - 1, Y - 1)) c++;
                if (_game.IsBomb(X - 0, Y - 1)) c++;
                if (_game.IsBomb(X + 1, Y - 1)) c++;
                if (_game.IsBomb(X - 1, Y - 0)) c++;
                if (_game.IsBomb(X + 1, Y - 0)) c++;
                if (_game.IsBomb(X - 1, Y + 1)) c++;
                if (_game.IsBomb(X - 0, Y + 1)) c++;
                if (_game.IsBomb(X + 1, Y + 1)) c++;

                int s = 0;
                if (_game.IsDismantled(X - 1, Y - 1) && _game.IsBomb(X - 1, Y - 1)) s++;
                if (_game.IsDismantled(X - 0, Y - 1) && _game.IsBomb(X - 0, Y - 1)) s++;
                if (_game.IsDismantled(X + 1, Y - 1) && _game.IsBomb(X + 1, Y - 1)) s++;
                if (_game.IsDismantled(X - 1, Y - 0) && _game.IsBomb(X - 1, Y - 0)) s++;
                if (_game.IsDismantled(X + 1, Y - 0) && _game.IsBomb(X + 1, Y - 0)) s++;
                if (_game.IsDismantled(X - 1, Y + 1) && _game.IsBomb(X - 1, Y + 1)) s++;
                if (_game.IsDismantled(X - 0, Y + 1) && _game.IsBomb(X - 0, Y + 1)) s++;
                if (_game.IsDismantled(X + 1, Y + 1) && _game.IsBomb(X + 1, Y + 1)) s++;

                if (s == c)
                {
                    _game.OpenSpot(X - 1, Y - 1);
                    _game.OpenSpot(X - 0, Y - 1);
                    _game.OpenSpot(X + 1, Y - 1);
                    _game.OpenSpot(X - 1, Y - 0);
                    _game.OpenSpot(X - 0, Y - 0);
                    _game.OpenSpot(X + 1, Y - 0);
                    _game.OpenSpot(X - 1, Y + 1);
                    _game.OpenSpot(X - 0, Y + 1);
                    _game.OpenSpot(X + 1, Y + 1);
                }
            }
        }

        public Button Button
        {
            get { return (this._button); }
        }

        private void Click(object sender, EventArgs e)
        {

            if (!Dismantled)
            {
                if (Minded)
                {
                    Button.BackgroundImage = Properties.Resources.minesweepSquareExploded;
                    _exploded = true;
                    OnExplode();
                }
                else
                {
                    this.Open();
                }
            }
            if (_game.ftime == true && !Minded)
            {
                _game.ftime = false;
                _game._timer = new Timer();
                _game._timer.Interval = 1000;
                _game._timer.Tick += new EventHandler(_game.TimerTick);
                _game._timer.Enabled = true;
            }
        }

        private void DismantleClick(object sender, MouseEventArgs e)
        {
            if (!Opened && e.Button == MouseButtons.Right)
            {
                if (Dismantled)
                {
                    _dismantled = false;
                    _questioned = true;
                    Button.BackgroundImage = Properties.Resources.minesweepSquareQuestion;
                }
                else if(_questioned)
                {
                    _questioned = false;
                    Button.BackgroundImage = Properties.Resources.minesweepSquare;
                    return;
                }
                else
                {
                    _dismantled = true;
                    Button.BackgroundImage = Properties.Resources.WinClassicMinesweeperFlag;
                }
                OnDismantle();
            }
        }

        public bool Dismantled
        {
            get { return (this._dismantled); }
        }

        public bool Minded
        {
            get { return (this._minded); }
            set { this._minded = value; }
        }

        protected void OnDismantle()
        {
            if (Dismantle != null)
            {
                Dismantle(this, new EventArgs());
            }
        }

        protected void OnExplode()
        {
            if (Explode != null)
            {
                Explode(this, new EventArgs());
            }
        }

        public void Open()
        {
            if (!Minded)
            {
                if (!Opened && !Dismantled)
                {
                    _opened = true;
                    // Count Bombs
                    int c = 0;
                    if (_game.IsBomb(X - 1, Y - 1)) c++;
                    if (_game.IsBomb(X - 0, Y - 1)) c++;
                    if (_game.IsBomb(X + 1, Y - 1)) c++;
                    if (_game.IsBomb(X - 1, Y - 0)) c++;
                    if (_game.IsBomb(X - 0, Y - 0)) c++;
                    if (_game.IsBomb(X + 1, Y - 0)) c++;
                    if (_game.IsBomb(X - 1, Y + 1)) c++;
                    if (_game.IsBomb(X - 0, Y + 1)) c++;
                    if (_game.IsBomb(X + 1, Y + 1)) c++;

                    if (c > 0)
                    {
                        switch (c)
                        {
                            case 1:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare1;
                                break;
                            case 2:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare2;
                                break;
                            case 3:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare3;
                                break;
                            case 4:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare4;
                                break;
                            case 5:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare5;
                                break;
                            case 6:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare6;
                                break;
                            case 7:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare7;
                                break;
                            case 8:
                                Button.BackgroundImage = Properties.Resources.minesweepSquare8;
                                break;
                        }
                    }
                    else
                    {
                        Button.BackgroundImage = Properties.Resources.minesweepSquare0;
                        Button.Enabled = false;

                        _game.OpenSpot(X - 1, Y - 1);
                        _game.OpenSpot(X - 0, Y - 1);
                        _game.OpenSpot(X + 1, Y - 1);
                        _game.OpenSpot(X - 1, Y - 0);
                        _game.OpenSpot(X - 0, Y - 0);
                        _game.OpenSpot(X + 1, Y - 0);
                        _game.OpenSpot(X - 1, Y + 1);
                        _game.OpenSpot(X - 0, Y + 1);
                        _game.OpenSpot(X + 1, Y + 1);
                    }
                }
            }
        }

        public bool Opened
        {
            get { return (this._opened); }
        }

        public int X
        {
            get { return (this._x); }
        }

        public int Y
        {
            get { return (this._y); }
        }

        public bool Exploded
        {
            get { return (this._exploded); }
        }

        public void RemoveEvents()
        {
            _button.Click -= new EventHandler(Click);
            _button.MouseDown -= new MouseEventHandler(DismantleClick);
            _button.MouseUp -= new MouseEventHandler(MiddleClick);
        }
    }
}