Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Exercise POSE.25.

1 – Minesweeper

Exercise POSE.25.1 – Minesweeper


2024-05-16

Instructions
• Copy the starter project in a separate directory on your network share
• Complete all methods according to the instructions and XML comments
• Solve the following tasks and make sure that the provided tests are all passing
• Make sure to run CleanSolutionDir.ps1 before you pack and hand in your solution

1 Description
Minesweeper is (or was 😉) a popular computer game. In short, the player is tasked with
finding all mines hidden in an area composed of fields. To accomplish this there are only two
kinds of interaction:
1. Unveiling a field
• If the field is empty (= no mine) it is unveiled and shows the number of mines in its
vicinity – more on that later
• If the field is a mine that means Game Over for the player
2. Flagging a field as a possible mine
• The goal of the game is to flag all mines and unveil all empty fields – if the user
accomplishes that they win
• Only mines must be flagged to win the game1
See fig. 1 for samples of all three possible game states.

(a) Running Game (b) Mine Hit – Game Over (c) All Mines Found – Victory

Figure 1: Game States

For this exercise you are going to use the Board again. This time with two new features:
• Handling mouse-click events
• Showing message boxes

In the starter code you will see Task, async and await – don’t worry about that!
INFO-CIRCLE Those are needed for the UI to work properly, but you are not expected to already
understand or use them. These parts are complete, you can program as usual.

1
Empty fields may be flagged during the game, but must not be flagged, but unveiled to actually win

POSE 1st year 1/4


Exercise POSE.25.1 – Minesweeper

2 Unveiling Logic
If the user unveils a field all of the following happens:
1. The field selected by the user is unveiled
2. It is determined if the field is a mine:
• If the selected field is a mine all other mines are revealed and the game ends
• If the selected field is empty:
a. The number of mines surrounding the selected field is calculated and shown in the
cell on the board
– This number is based on the count of mines hidden on the 8 fields surrounding
(= directly neighboring) the selected field
– Mind: different counts have various colors assigned – more on that later
b. If the number of neighboring mines is zero the unveiling logic is executed again for
the neighboring fields
– This is a recursive call
Take a look at fig. 2a which shows the cells highlighting with the following color code:
• Red: mine
• Green: field unveiled by user
• Blue: auto-unveiled fields, empty (= showing no number)
• Pink: auto-unveiled fields, have a mine in their vicinity and thus show the count of mines
in the surrounding fields (always 1 in this case)
• Grey: still hidden field

(a) Unveil Logic (b) Neighboring Mines

Fig. 2b show the count of neighboring mines using the following color code:
• Red: mine
• Blue: 0 mines in vicinity
• Orange: 1 mine in the vicinity
• Pink: 3 mines in the vicinity
• Green: Vicinity of the marked field

POSE 1st year 2/4


Exercise POSE.25.1 – Minesweeper

3 Implementation Details
• Input partially happens via the terminal (initial config) and UI interaction (mouse clicks on
the board)
• Game Loop:
1. The user has to define the size of the playground
– Support a size between 2 and 20
– Use the same size for both rows and columns ⇒ the user only has to enter one value
2. The user has to define how many mines will be hidden on the playground
– The minimum is 1
– The maximum is the amount of mines allowed by the playground size
◦ e.g. 5 ∗ 5 ⇒ max. 25 mines
3. The playground is generated
– A 2D array of the proper size is created
– Mines are randomly 2 placed until the total count equals the number defined by the
user
4. The Board is displayed
– Initialized with the proper values and initially displaying all fields as hidden
5. The user clicks on the playground to issue commands repeatedly until:
– Either the user has flagged all mines and unveiled all empty fields ⇒ the user wins,
congratulate them
– Or the user unveiled a mine ⇒ the user lost, unveil all remaining mines to show
them what they missed
• User Input:
– Initial configuration (size of playground and number of mines) is done via the terminal
– Interaction with the playground happens via mouse clicks
◦ The HandleMouseClick method will be called automatically by the UI. Put your
logic for unveiling and flagging fields etc. in there.
– Unveiling a field
◦ Left-clicking a not-yet unveiled field will unveil it
◦ As you already know: Hitting a mine will end the game, otherwise fields are unveiled
as described in sec. 2 and the number of neighboring mines is shown3
– Flagging a mine
◦ Right-clicking a not-yet unveiled field will flag it
◦ Flags the selected field (= set state to Flagged and display the flag symbol on the
Board)
◦ This does not validate if the selected field actually contains a mine
• Game End Messages
– Message boxes can be displayed using the Board.ShowMessageBox method
– If the user wins show a message box with the text You won!
– If the user loses show a message box with the text You lost!
• Implementation
– Read the XML doc of the provided skeleton code carefully to properly implement each
method
– Make sure to not alter the method signatures to ensure that the provided unit tests
work You may add additional methods if you feel those are helpful in structuring your
application
– Do not overlook the additional class Util which also contains some required methods
2
Make sure to use Util.Random
3
Mind the colors: 1 mine blue, 2 mines green, 3 mines red, etc. – check the skeleton code for all colors with
black being the fallback

POSE 1st year 3/4


Exercise POSE.25.1 – Minesweeper

4 Sample Run
See fig. 3 for a simple sample run.

Figure 3: Sample Run

The application has to be stopped by the user by pressing any key in the terminal – closing
the window will not end the terminal session.

POSE 1st year 4/4

You might also like