-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.cs
345 lines (322 loc) · 13.6 KB
/
GameBoard.cs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Minesweeper
{
public class GameBoard
{
Game1 game;
SpriteBatch spriteBatch;
SpriteFont font;
Texture2D squaresTexture;
const int textureGridSize = 152;
enum TextureYOffset
{
DarkSquare = 0,
LightSquare = textureGridSize,
GraySquare = textureGridSize * 2,
Mine = textureGridSize * 3,
Flag = textureGridSize * 4,
Cross = textureGridSize * 5
}
BoardState state;
bool started = false;
bool lost = false;
int xDie = -1;
int yDie = -1;
int screenWidth = Game1.ScreenWidth, screenHeight = Game1.ScreenHeight;
int targetWidth, targetHeight;
Rectangle currentBoard;
int squareSize;
int mouseOverX = -1;
int mouseOverY = -1;
bool mouseHeld = false;
public GameBoard(Game1 game)
{
this.game = game;
state = new BoardState();
ScreenSizeChanged(Game1.ScreenWidth, Game1.ScreenHeight);
}
public GameBoard(Game1 game, BoardState oldState)
{
this.game = game;
state = oldState;
started = true;
ScreenSizeChanged(Game1.ScreenWidth, Game1.ScreenHeight);
}
//Update the location of size of the board
public void ScreenSizeChanged(int width, int height)
{
screenWidth = width;
screenHeight = height;
targetWidth = width - 4;
targetHeight = height - 80;
if (state.Squares != null)
{
updateCurrentBoard();
}
}
private void updateCurrentBoard()
{
//Get a size for the squares
int squareWidth = targetWidth / getBoardWidth();
int squareHeight = targetHeight / getBoardHeight();
squareSize = (squareWidth < squareHeight) ? squareWidth : squareHeight;
int boardWidth = squareSize * getBoardWidth();
int boardHeight = squareSize * getBoardHeight();
currentBoard = new Rectangle(screenWidth / 2 - boardWidth / 2, screenHeight / 2 - boardHeight / 2,
boardWidth, boardHeight);
}
private int getBoardWidth()
{
return state.Squares.GetLength(0);
}
private int getBoardHeight()
{
return state.Squares.GetLength(1);
}
public void NewGame(int width, int height, int mines)
{
started = false;
state.MineCount = mines;
lost = false;
xDie = -1;
yDie = -1;
//Make sure settings for game are valid
if (mines > (width - 1) * (height - 1) || width < 6 || height < 6)
{
//return;
}
state.Squares = new Square[width, height];
state.SquaresLeft = state.Squares.Length - mines; //The number of non-bomb squares to uncover
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
state.Squares[x, y] = new Square();
}
}
updateCurrentBoard();
}
public void LoadContent(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
//Load the font for the numbers written on uncovered squares
font = game.Content.Load<SpriteFont>("MenuFont");
//Load square textures
squaresTexture = game.Content.Load<Texture2D>("SquareTexturesLarge");
}
public bool HandleClick(MouseState mouseState, MouseState oldMouseState)
{
if (currentBoard.Contains(mouseState.X, mouseState.Y))
{
if (lost) //Don't allow input if game is over, new game should be started
{
mouseOverX = mouseOverY = -1;
return false;
}
mouseOverX = (mouseState.X - currentBoard.Left) / squareSize;
mouseOverY = (mouseState.Y - currentBoard.Top) / squareSize;
mouseHeld = mouseState.LeftButton == ButtonState.Pressed;
if (mouseOverX < getBoardWidth() && mouseOverY < getBoardHeight() &&
state.Squares[mouseOverX, mouseOverY].Number < 0) //If square isn't already uncovered
{
if (mouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed &&
!state.Squares[mouseOverX, mouseOverY].IsFlagged)
{
if (started)
{
//Left click released on square, uncover it
uncoverSquare(mouseOverX, mouseOverY);
}
else
{
//Generate field with no bombs near pressed square
generateField(mouseOverX, mouseOverY);
}
}
else if (mouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released)
{
//Square was right clicked, flag it
state.Squares[mouseOverX, mouseOverY].IsFlagged = !state.Squares[mouseOverX, mouseOverY].IsFlagged;
}
}
}
else
{
mouseOverX = -1;
mouseOverY = -1;
mouseHeld = false;
}
return mouseOverX > -1;
}
private void generateField(int xPressed, int yPressed)
{
started = true;
//Place the mines
Random rndm = new Random();
int minesLeft = state.MineCount;
while (minesLeft > 0)
{
int x = rndm.Next(0, getBoardWidth());
int y = rndm.Next(0, getBoardHeight());
if (!state.Squares[x, y].IsMine && !adjacentOrEqual(new Point(x, y), new Point(xPressed, yPressed)))
{
state.Squares[x, y].IsMine = true;
--minesLeft;
}
}
uncoverSquare(xPressed, yPressed);
}
private bool adjacentOrEqual(Point one, Point two)
{
return (one.X == two.X || one.X == two.X - 1 || one.X == two.X + 1) &&
(one.Y == two.Y || one.Y == two.Y - 1 || one.Y == two.Y + 1);
}
private List<Point> adjacentSquares(int sqrx, int sqry)
{
List<Point> adjSquares = new List<Point>();
for (int x = -1; x <= 1; ++x)
{
for (int y = -1; y <= 1; ++y)
{
if (x == 0 && y == 0) continue;
int adjx = sqrx + x;
int adjy = sqry + y;
//Square exists
if (adjx >= 0 && adjy >= 0 && adjx < getBoardWidth() && adjy < getBoardHeight())
{
adjSquares.Add(new Point(adjx, adjy));
}
}
}
return adjSquares;
}
private int bombCount(List<Point> adjSquares)
{
int bombCount = 0;
foreach (Point sqr in adjSquares)
{
if (state.Squares[sqr.X, sqr.Y].IsMine)
{
++bombCount;
}
}
return bombCount;
}
//Returns how many bombs there were around it
private void uncoverSquare(int x, int y)
{
//If square exists
if (state.Squares != null && getBoardWidth() > x && getBoardHeight() > y)
{
if (state.Squares[x, y].IsMine)
{
//Uncovering a mine, game over
xDie = x;
yDie = y;
gameOver(false);
return;
}
--state.SquaresLeft; //One less square to uncover
state.Squares[x, y].IsFlagged = false; //Unflag square
List<Point> adjSquares = adjacentSquares(x, y); //Get list of adjacent square positions
int bombs = bombCount(adjSquares); //Count the number of surrounding bombs
state.Squares[x, y].Number = bombs; //Set the squares number
if (bombs == 0) //Surrounding squares should be automatically uncovered if none of them are bombs
{
foreach (Point sqr in adjSquares)
{
//Uncover the square if it's still covered
if (state.Squares[sqr.X, sqr.Y].Number < 0)
{
uncoverSquare(sqr.X, sqr.Y);
}
}
}
//Check if there are no more squares uncovered
if (state.SquaresLeft <= 0)
{
gameOver(true);
}
}
}
private void gameOver(bool won)
{
lost = true;
}
public void Draw()
{
if (currentBoard != null && currentBoard.Left > 0)
spriteBatch.DrawString(font, string.Format("X: {0}, Y: {1}", mouseOverX, mouseOverY), new Vector2(currentBoard.Left, 5), Color.White);
if (state.Squares != null)
{
//Draw the squares
for (int x = 0; x < getBoardWidth(); ++x)
{
for (int y = 0; y < getBoardHeight(); ++y)
{
Square square = state.Squares[x, y];
TextureYOffset squareTexture;
//The square is uncovered, or game is over and it's a bomb or a flag
if (square.Number > -1 || (lost && (square.IsMine || square.IsFlagged)))
{
squareTexture = TextureYOffset.GraySquare;
}
else //Square is covered
{
//If the mouse is over this square, make it shiny. If it's flagged and mouse is pressed on it,
//it shouldn't be shiny
if (mouseOverX == x && mouseOverY == y && !(square.IsFlagged && mouseHeld))
{
squareTexture = (mouseHeld) ? TextureYOffset.GraySquare : TextureYOffset.LightSquare;
}
else
{
squareTexture = TextureYOffset.DarkSquare;
}
}
//Draw the square with the texture that has been chosen
Rectangle squareRect = new Rectangle(currentBoard.Left + x * squareSize,
currentBoard.Top + y * squareSize, squareSize, squareSize);
spriteBatch.Draw(squaresTexture, squareRect,
new Rectangle(0, (int)squareTexture, textureGridSize, textureGridSize), Color.White);
//Show bombs if the game has ended
if (lost && (square.IsMine || square.IsFlagged)) //Show bomb on flagged aswell, but with cross ontop
spriteBatch.Draw(squaresTexture, squareRect, new Rectangle(0, (int)TextureYOffset.Mine,
textureGridSize, textureGridSize), (xDie == x && yDie == y) ? Color.Red : Color.White);
if (square.Number > 0)
{
//Draw number for square
spriteBatch.Draw(squaresTexture, squareRect, new Rectangle(textureGridSize,
(square.Number - 1) * textureGridSize, textureGridSize, textureGridSize), Color.White);
}
else if (square.IsFlagged)
{
if (lost && !square.IsMine) //Flag is wrong
{
//Draw a cross on square
spriteBatch.Draw(squaresTexture, squareRect,
new Rectangle(0, (int)TextureYOffset.Cross, textureGridSize, textureGridSize), Color.White);
}
else
{
//Draw flag on square
spriteBatch.Draw(squaresTexture, squareRect,
new Rectangle(0, (int)TextureYOffset.Flag, textureGridSize, textureGridSize), Color.White);
}
}
}
}
}
}
public BoardState GetBoardState()
{
if (!started || lost)
return null;
return state;
}
}
}