aboutsummaryrefslogtreecommitdiff
path: root/Histacom2/OS/Win95/Win95Apps/MineSweeper/Game.cs
blob: 8f8d7e782daef8d13bcc1f6926a4071313cd2dc6 (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
using System;
using System.Drawing;
using System.Windows.Forms;


namespace Histacom2.OS.Win95.Win95Apps.MineSweeper
{
    public class Game
    {
        public event EventHandler DismantledMinesChanged;
        public event EventHandler Tick;

        private int _dismantledMines;
        private int _height;
        private int _incorrectdismantledMines;
        private int _mines;
        private Panel _panel;
        private Square[,] _squares;
        public Timer _timer;
        private int _width;
        public bool  ftime = true;
        public int Time;
        public bool win = false;
        private WinClassicMinesweeper _window;

        public Game(WinClassicMinesweeper window, Panel panel, int width, int height, int mines)
        {
            _panel = panel;
            _width = width;
            _height = height;
            _mines = mines;
            _window = window;
            win = false;
        }

        private void Dismantle(object sender, EventArgs e)
        {

            Square s = (Square)sender;
            if (s.Dismantled)
            {
                if (s.Minded) _dismantledMines++;
                else _incorrectdismantledMines++;
            }
            else
            {
                if (s.Minded) _dismantledMines--;
                else _incorrectdismantledMines--;
            }

            OnDismantledMinesChanged();

            if (_dismantledMines == Mines)
            {
                _timer.Enabled = false;
                Panel.Enabled = false;
                win = true;
            }
        }

        public int DismantledMines
        {
            get { return _dismantledMines + _incorrectdismantledMines; }
        }

        private void Explode(object sender, EventArgs e)
        {
            _panel.Enabled = false;
            _window.button1.BackgroundImage = Properties.Resources.WinClassicMinesweeperSad;
            if (_timer != null) _timer.Enabled = false;
            foreach (Square s in _squares)
            {
                s.RemoveEvents();
                if (s.Minded && !s.Exploded && !s.Dismantled)
                {
                    s.Button.BackgroundImage = Properties.Resources.minesweepSquareMine;
                }
                if (s.Dismantled && !s.Minded)
                {
                    s.Button.BackgroundImage = Properties.Resources.minesweepSquareWrong;
                }
            }
            _window.button1.BackgroundImage = Properties.Resources.WinClassicMinesweeperSad;
        }

        public int Height
        {
            get { return (this._height); }
        }

        public bool IsBomb(int x, int y)
        {
            if (x >= 0 && x < Width)
            {
                if (y >= 0 && y < Height)
                {
                    return _squares[x, y].Minded;
                }
            }
            return false;
        }

        public bool IsDismantled(int x, int y)
        {
            if (x >= 0 && x < Width)
            {
                if (y >= 0 && y < Height)
                {
                    return _squares[x, y].Dismantled;
                }
            }
            return false;
        }

        public int Mines
        {
            get { return (this._mines); }
        }

        protected void OnDismantledMinesChanged()
        {

            if (DismantledMinesChanged != null)
            {
                DismantledMinesChanged(this, new EventArgs());
            }
        }

        protected void OnTick()
        {
            if (Tick != null)
            {
                Tick(this, new EventArgs());
            }
        }

        public void OpenSpot(int x, int y)
        {
            if (x >= 0 && x < Width)
            {
                if (y >= 0 && y < Height)
                {
                    _squares[x, y].Open();
                }
            }
        }

        public Panel Panel
        {
            get { return (this._panel); }
        }

        public void Start()
        {

            //Panel.SuspendLayout();
            Time = 0;
            _dismantledMines = 0;
            _incorrectdismantledMines = 0;
            Panel.Enabled = true;
            Panel.Controls.Clear();

            // Create Spots
            _squares = new Square[Width, Height];
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    Square s = new Square(this, x, y);
                    s.Explode += new EventHandler(Explode);
                    s.Dismantle += new EventHandler(Dismantle);
                    s.Button.MouseDown += (send, args) => { if (_panel.Enabled) _window.button1.BackgroundImage = Properties.Resources.WinClassicMinesweeperGasp; };
                    s.Button.MouseUp += (send, args) => { if (_panel.Enabled) _window.button1.BackgroundImage = Properties.Resources.WinClassicMinesweeperSmile; };
                    _squares[x, y] = s;
                }
            }

            // Place Mines
            int b = 0;
            Random r = new Random();
            while (b < Mines)
            {
                int x = r.Next(Width);
                int y = r.Next(Height);

                Square s = _squares[x, y];
                if (!s.Minded)
                {
                    s.Minded = true;
                    b++;
                }
            }

            OnDismantledMinesChanged();
        }

        public void TimerTick(object sender, EventArgs e)
        {
            Time++;
            OnTick();
        }

        public int Width
        {
            get { return (this._width); }
        }
    }
}