Download Day 15. 2D Arrays - Briar Cliff University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Day 15. 2D Arrays
***** Today:

Write a Minesweeper program
This program demonstrates the use of 2D arrays, as well as use of the mouse for input.
Planning
Minesweeper is a 2D array of cells. Initially all cells are covered. The user clicks on the cells
(sometimes right-click, sometimes left-click). What do we need to keep track of for each
cell?
1.
2.
3.
4.
5.
Is there a bomb here? (y/n)
Has the cell been uncovered? (y/n)
Has the cell had a flag planted on it? (y/n)
How many neighbors does it have? (int)
Where is it located on the screen? (rectangle)
Create a struct called Cell that will hold the five elements above.
We need a 2D array of Cells. Minesweeper is 10x10. We can make our array 10x10, but it
makes it difficult to count neighbors. We can make our life much easier if we put a border
around the board and make it 12x12 (indexes 0..11) but only display 1..10. Use:
const int boardSize = 10;
But use (boardSize+2) for sizing your array.
Pictures needed:
Save these to your computer and add them as content.
Planting the mines, setting up the board
Planting the mines
The 10x10 version of Minesweeper has 10 mines hidden. How do we decide where to put
them? If we use a random number generator, we may get 10 bombs, but we may get 9 or
11 (or 8 or 12). We need a way to get exactly 10 bombs planted on the board. Here is one
way to do it:
Create an array of 100 Booleans. Set the first 10 values to true and the last 90 values to
false. This will give us exactly 10 out of 100 bombs (the true cells). But they aren't
randomly distributed. Here is how to randomly distribute them. Create another array with
100 randomly generated numbers (doubles). Sort this array. As you swap two elements in
this array, swap the same elements in the array of Booleans. This will randomly distribute
the 10 bombs through the 100-element array. Then copy the elements of the 1D array to
corresponding locations in the 2D array.
Sorting
To sort, use the bubble sort algorithm.
Mapping
We need a way to map a 100-element 1D array to a 100-element (10x10) 2D array. If you
write down the subscripts, it's pretty easy to do:
1D
1
2
3
4
5
6
7
8
9
10
11
12
...
20
21
22
...
30
...
91
92
...
100
2D
1,1
1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
1,10
2,1
2,2
...
2,10
3,1
3,2
...
3,10
...
10,1
10,2
...
10,10
Use the above table to compute the mapping formula.
Counting Neighbors
For each cell (nested loops that go from 1 to 10), we need to count the number of
neighboring cells that have a mine in them. This is a great place to use a function. The
function will be called CountNeighbors and needs three pieces of information to do its job:
(1) the array, (2) the row of the cell under consideration, and (3) the column of the cell
under consideration.
Loading Content
We will load the content into Texture2D variables. We will load the images of the numbers 0
through 8 into a 9-element array numbered from 0 to 8. We will load the remaining three
textures into separate variables (e.g. bomb, flag, blank).
Drawing the Board
If you have computed the correct rectangle values, you should be able to easily draw the
board. For each cell, see if it has a flag planted. If so, draw the flag. Then, see if it is
covered. If so, draw the blank. If not, draw a bomb if it holds a bomb, and draw the
appropriate number if it does not hold a bomb.
Mouse Clicks
See the mouse documentation. We will need a MouseState variable. We will need a way to
check the left button and a way to check the right button. We need to convert an X and a Y
on the screen to a row and a column in the array (this is a good place to use functions). We
must ignore clicks that are off of the game board.
Left button
There




are several cases:
The cell is covered and a bomb is in the cell. Reveal the bomb. The game is over.
The cell is covered and a number is in the cell. Reveal the number.
The cell is covered and has a flag. Do nothing (ignore the click).
The cell is already uncovered. Do nothing (ignore the click).
Right button
There are several cases:
 The cell is covered and no flag has been planted. Display the flag. The cell is still
covered but has a flag displayed.
 The cell is covered and a flag has been previously planted. Un-display the flag. The
cell is still covered.
 The cell is uncovered. Do nothing (ignore the click).
Winning, Losing, Game States
The player loses when he left-clicks on a bomb. The game state is GameOverLose.
The player wins when he has identified all of the bombs by right-clicking and planting a flag
on each bomb. The game state is GameOverWin.
The player may not plant more than 10 flags. If 10 flags have been planted and the player
tries to plant an eleventh flag, do not allow him to plant a flag. He must "un-plant" one first
(by right-clicking on it).
When the player plants a flag, keep track of whether he has correctly identified a bomb or
not. Also keep track of how many flags have been planted. If the number of bombs correctly
identified is 10, the game is over and the player wins.
When the game is neither won nor lost, the game state is Playing.