Question: Minesweeper in Python You will be implementing the game Minesweeper. You will be implementing a custom version of the game, where the player will indicate

Minesweeper in Python

You will be implementing the game Minesweeper.

You will be implementing a custom version of the game, where the player will indicate the height and width of the board, as well as the number of bombs. The bombs will be randomly placed on the board. The user will input a coordinate of the board, and after every turn an updated board will be printed out. The game will continue until:

A bomb is selected (Lose)

All non-bomb locations have been selected (Win)

You will be writing a Minesweeper class that will be the logic behind your game. Define the following methods:

Constructor takes in the dimensions of the board and how many bombs, and randomly places the bombs on the board. The number of bombs should not exceed (number of squares - 1).

print print the state of the board

checkValid check if the coordinates are a valid coordinate on the board and that the state of that coordinate has not been revealed yet

select takes the coordinate and returns a message if it is not a valid coordinate or if it is not blank. If it is, it will reveal the appropriate information for the square and its adjacent squares (according to the rules)

selectFirst takes the coordinate on the first turn, and if it is a bomb, it will randomly select another position for the bomb and do everything in select.

flag takes a coordinate to flag for a bomb. Should give an error message if the coordinate is not blank or if it is invalid.

unflag - takes a coordinate to unflag a bomb. Should give an error message if the coordinate was not flagged or if it is an invalid coordinate.

checkStatus returns the status of the game: win, lose, in progress

_getSolution returns the solution of the board for testing purposes only. This method will allow us to test your program, but you should not be calling/using it, other than for testing purposes.

You will be writing Driver script that will create a Minesweeper object every time a new game is started. This will be what drives the game. It should make the appropriate calls to the methods in Minesweeper depending on decisions made by the player. It should also keep track of how many games have been won or lost.

You may write any helper methods that you find necessary. Sample output may look like the following:

Enter a height: 3 Enter a length: 8 Enter the number of bombs: 5

1 2 3 4 5 6 7 8 1 - - - - - - - -

2 - - - - - - - - 3 - - - - - - - - Select an x coordinate: 1 Select a y coordinate: 1

1 2 3 4 5 6 7 8 1 0 1 - - - - - -

2 1 2 - - - - - - 3 - - - - - - - -

select or flag or unflag? flag Select an x coordinate: 1 Select a y coordinate: 3

1 2 3 4 5 6 7 8 1 0 1 F - - - - -

2 1 2 - - - - - - 3 - - - - - - - -

select or flag or unflag? unflag Select an x coordinate: 3 Select a y coordinate: 1 Invalid Input select or flag or unflag? unflag Select an x coordinate: 1 Select a y coordinate: 3

1 2 3 4 5 6 7 8 1 0 1 - - - - - -

2 1 2 - - - - - - 3 - - - - - - - -

select or flag or unflag? select Select an x coordinate: 2 Select a y coordinate: 6 1 2 3 4 5 6 7 8 1 0 1 - - - - - -

2 1 2 - - - 3 - - 3 - - - - - - - -

select or flag or unflag? select Select an x coordinate: 2 Select a y coordinate: 6 1 2 3 4 5 6 7 8 1 0 1 - - - B - -

2 1 2 - - - 3 - - 3 - - - - - - - -

Bomb! You lose

1 2 3 4 5 6 7 8 1 0 1 - - - B - -

2 1 2 B - B 3 - - 3 - B - - B - - -

Do you want to play again ("yes" or "no")? No Win: 0 Lose: 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!