aboutsummaryrefslogtreecommitdiff
path: root/TimeHACK.Main/OS/Win95/Win95Apps/MineSweeper/Square.cs
blob: 2029995e32d0566cb9d390cb5a9156e75ac39833 (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
using System;
using System.Drawing;
using System.Windows.Forms;
using TimeHACK.Engine;

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

        private Button _button;
        private bool _dismantled = false;
        private Game _game;
        private bool _minded = false;
        private bool _opened = 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 = w + 1;
            _button.Height = h + 1;
            _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.Paint += (sender, args) => Paintbrush.PaintClassicBorders(sender,args,2);
            _button.MouseDown += new MouseEventHandler(DismantleClick);
            _button.FlatStyle = FlatStyle.Flat;
            _button.FlatAppearance.BorderSize = 1;
            _button.BackgroundImageLayout = ImageLayout.Stretch;

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

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

        private void Click(object sender, System.EventArgs e)
        {
            if (!Dismantled)
            {
                if (Minded)
                {
                    Button.BackColor = Color.Red;
                    OnExplode();
                }
                else
                {
                    this.Open();
                }
            }
        }

        private void DismantleClick(object sender, MouseEventArgs e)
        {
            if (!Opened && e.Button == MouseButtons.Right)
            {
                if (Dismantled)
                {
                    _dismantled = false;
                    Button.BackgroundImage = null;
                    Button.Text = "?";
                }
                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 (!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)
                {
                    Button.Text = c.ToString();
                    switch (c)
                    {
                        case 1:
                            Button.ForeColor = Color.Blue;
                            break;
                        case 2:
                            Button.ForeColor = Color.Green;
                            break;
                        case 3:
                            Button.ForeColor = Color.Red;
                            break;
                        case 4:
                            Button.ForeColor = Color.DarkBlue;
                            break;
                        case 5:
                            Button.ForeColor = Color.DarkRed;
                            break;
                        case 6:
                            Button.ForeColor = Color.LightBlue;
                            break;
                        case 7:
                            Button.ForeColor = Color.Orange; // Guesed, never seen one!
                            break;
                        case 8:
                            Button.ForeColor = Color.Ivory; // Guesed, never seen one!
                            break;
                    }
                }
                else
                {
                    Button.BackColor = SystemColors.ControlLight;
                    Button.FlatStyle = FlatStyle.Flat;
                    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 void RemoveEvents()
        {
            _button.Click -= new EventHandler(Click);
            _button.MouseDown -= new MouseEventHandler(DismantleClick);
        }
    }
}