Question
Create a Java program that plays a game of Minesweeper the program must use ten mines (use a named constant). Mines must be placed randomly
Create a Java program that plays a game of Minesweeper the program must use ten mines (use a named constant). Mines must be placed randomly at the start of each game using the java.util.Random class. If the user enters an invalid input print "Please try again" on the next line and repeat the input prompt on the line after that. Winning the game the user guesses correctly the location of every mine gets a "You won" message and a "You lost" if the first guess is incorrect. With a prompt asking if they wish to play again.
Minesweeper class:
contains the main() method create an instance of the Boardgame class and call its run() method
Boardgame class:
contains a two-dimensional array (8 x 8 grid) board with the mines.
a public run() method which plays the game (character-based)
Mine class:
Each square on the board will be represented by an instance of a class named Mine the grid is a 2-D array of objects of class Mine. The class needs a field that is a char to represent a character in that grid.
each grid will display one of the following:
a hyphen ( - ) as no guess has been made for that cell
a digit (is a character, not an int) as the correct guess has been made and the cell has no mine. The digit represents the number of adjacent cells that contain a mine (digit 0 means there are no adjacent mines).
the character B indicates the cell contains a mine with the user guess being correct
User input:
If they guess no mine and its correct the grid changes to a digit indicating how many adjacent cells contain mines (0, 1, or 2)
If they guess there's a mine and its correct then the grid changes to the character B
If they guess incorrectly they lose the game and are asked to play again
Sample Output:
Minesweeper Game!
Will you be playing today? (y/n) : y
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
Would you like a hint? (y/n): n (y shows where the mines are on the board)
Enter a row number: 2
Enter a column number: 2
Does row 2 and column 2 have a mine? (y/n): n
- - - - - - - -
- 0 - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
(if they say yes to a hint shows the mines below)
- 1 - - - B - -
- 0 - - - - B -
- B - - B - -2-
- - - 1 - - - -
B - - - B - - B
- 0 - - - 2 - -
B - - 1 - - B -
- - B - - - 0 -
Thanks for playing.
Would you like to play again? (y/n): n
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started