Question: The PlayMinesweeper class contains the methods you will use to create game boards and mimic the rules of the game. You will submit this file
The PlayMinesweeper class contains the methods you will use to create game boards and mimic the rules of the game. You will submit this file to Autolab.The Square class contains information about EACH game piece in the grid. Do not edit or submit to Autolab.The State class contains information regarding whether each game piece in the grid is open, closed, or flagged. Do not edit or submit to Autolab.The GraphicalDriver and TextDriver classes are used to test your methods interactively. Follow the prompts in the terminal or on your screen to use the driver. Feel free to edit this file, as it is provided only to help you test your code. It is not submitted and is not used to grade your code. Do not submit to Autolab.In GraphicalDriver, press the number key corresponding with the options listed on the menu to navigate to the next screen. Once on the next screen, follow the prompts accordingly.Use TextDriver to test your code first, as it is simpler to use. Once you have a good understanding of the assignment, then you may use the graphic driver.Both drivers function the same way if it works on one driver it will work on the other.Multiple input files are included. They store information regarding the specifications to create each game board. You are welcome to create your own input files, as they will not be submitted to Autolab.PlayMinsweeper.javaDO NOT add new import statements.DO NOT change any of the methods signatures.Methods to be implemented by you: placeMinesIn this method, you will update the instance variable, grid, to be a D array of Squares with m rows and n columns. Then, you will place mines in the grid according to the specifications given in the input file. Each Square object represents an individual game piece in the grid.The input file contains the grid size as well as all the mines to be placed on the grid.To complete this method you will read from the input file:Read the first two integers from the input file which refer to the length m rows and width n columns of the array. The two integers are separated by a space on the first line.Create an array of m x n array Squares and update grid to be a reference to the array.Read the ijcoordinates representing the location of each mine to place in the grid. These values are given as two integers separated by a space on one line.Notice that each Square has a number the instance variable sqNumUpdate sqNum at each ij coordinate to to represent a mine.Update the instance variable, totalMines, to match the number of mines placed in the grid.Use the StdIn library to read from a file:StdIn.setFileinputFile opens a file to be read.StdIn.readInt reads the next integer value from the opened file whether the value is in the current line or in the next lineTo read the mines use a while loop to read two integers in each loop iteration. You will also have to use readLine to read the remaining of the line you wont use the value read by readLinewhile StdIn.isEmptyi StdIn.readInt; j StdIn.readInt; StdIn.readLine;To test, run one of the drivers: GraphicalDriver or TextDriver.Select option from the menu in either driver to test individual methods first.Note that both drivers will give you the same output If characters are not appearing properly you need to edit Line # in Collage.java to false or Line # in TextDriver.java:Here is the expected output for this method using binfillGridIn this method, you will fill the remaining squares in the grid according to the Minesweeper rules.In Minesweeper, any square that does not contain a mine is considered a safe square. Safe squares have numbers indicating how many mines, if any, border that square.For this assignment, each Square object in the grid has a corresponding instance variable, sqNum see Square.java: indicates a mine indicates a safe square that does not border any minesany number greater than indicates a safe square that borders the specified number of minesTo complete this method, traverse the grid, and for each Square, check if sqNum is we have a mineIf this condition is met increment sqNum for each Square in the block surrounding the mine. This includes the Squares on the top and bottom, left and right, and diagonal from the mine:When testing your code, run placeMines followed by fillGrid in that order. Here is the expected output for this method using bin:openSquareThe objective of Minesweeper is to open all of the safe squares in the grid. The Square class also contains the state of the square sqState:open: the gamer clicked the square to be opened.closed: the gamer hasnt clicked this square.flagged: the gamer flagged this square as a potential mine.You will simulate this game feature by creating a method that updates the instance variable, sqState, of the Square to be opened and of adjacent Squares according to the Minesweeper rules.In Minesweeper, a safe square that does not border any mines is considered an empty square ie the corresponding sqNum is Note the following while completing this method:When a player opens a safe Square that borders one or more mines, ONLY that square will open.When a player opens an empty Square, the adjacent Squares will open in all directions up and down, left and right, and diagonal until reaching Squares that contain numbers.Flagged Squares are considered unopened and, therefore, can be selected to be opened. If a flagged Square is successfully opened, the flag will be removed from the Square, and the Square will enter an OPEN state. The instance variable, flagCount, is updated to account for the flag that was removed from the grid.If a player attempts to open a Square that contains a mine, the game will end.Note that this method returns TRUE if the Square was successfully opened and FALSE if the Square contains a mine.To complete this method:Check if the selected Square is FLAGGED.If this condition is met, decrement flagCount to account for the flag that will be removed from the grid.Check if the selected Square contains a mine.If this condition is met, return false.Update sqState to OPEN the selected Square.Check if the selected Square is an empty Square ie if sqNum is If this condition is met, use recursion to open the adjacent empty Squares in all directions up and down, left and right, and diagonal:For a Square in EACH direction, check if the Square is unopened.If this condition is met, call openSquare using the rowcoordinate and columncoordinate of the checked Square.Return TRUE to indicate that the Square was successfully opened.Note that the method will pass in the rowcoordinate and columncoordinate of the selected Square for you to use. Remember that row and column indices in a D array begin at Note that the directional component of the recursive algorithm is the same as the one in fillGrid To check if a Square in any direction is unopened, use the same strategy as you did in fillGrid when accessing Squares in the block surrounding a mine. For the recursive step, be sure to call openSquare using the rowcoordinate and columncoordinate of the Square you CHECKED.When testing your code, run placeMines and fillGrid in that order before running this method. Here is the expected output for this method using bin and selecting the Square at coordinates :placeFlagIn Minesweeper, as players use the number clues to solve the game, they can place flags on squares they suspect contain mines. You will simulate this game feature by creating a method that updates the instance variable, sqState, of the Square selected to be flagged.Note the following while completing this method:Flags CANNOT be placed on open Squares.If the Square selected to be flagged already contains a flag, the flag will be removed from the Square, and the Square will return to a CLOSED state.Every time a flag is added or removed from the grid, the instance variable, flagCount, is updated accordingly. To complete this method:Check if the selected Square is FLAGGED.If this condition is met, remove the flag from the Square by updating sqState to be CLOSED.Decrement flagCount to account for the flag that was removed from the grid.Check if the selected Square is CLOSED.If this condition is met, add the flag to the Square by updating sqState to be FLAGGED.Increment flagCount to account for the flag that was added to the grid.Note that the method will pass in the rowcoordinate and columncoordinate of the selected Square for you to use. Remember that row and column indices in a D array begin at When testing your code, run placeMines and fillGrid in that order before running this method. Here is the expected output for this method using bin and selecting the square at coordinates :checkWinConditionTo win a game of Minesweeper, a player must open all of the safe squares in the grid without detonating a mine. In this method, you will check whether a player has won their game of PlayMinesweeper according to this condition.Note that this method returns TRUE if the player opened all of the safe Squares in the grid and FALSE otherwise.To complete this method:Traverse the grid, and for each Square, check if:the Square is a safe Square ANDthe Square is unopened.If both conditions are met, return false to indicate that an unopened safe Square remains in the grid.Otherwise, return true to indicate that the player has opened all of the safe Squares in the grid.Note that the outcome of this method will vary depending on your progress in the game. To produce the result below, run placeMines fillGrid and openSquare on the Square at in that order before running this method. Here is the expected output of this method using bin immediately after opening the Square at :Note that the game has not been won yet because there remain unopened safe Squares in the grid.chooseDifficultyThe next two methods work together to simulate a more realistic Minesweeper experience by generating random game boards for players to interact with. In this method, you will set up game boards by updating the instance variable, grid, according to the criteria of the Minesweeper difficulty levels.Note that the method will pass in the difficulty level for you to use.To complete this method:Check if the difficulty input parameter level is Beginner.If this condition is met, update the grid to be an x array of Squares.Set the instance variable, totalMines, to to indicate the number of mines that will be placed in the grid.Check if the difficulty input parameter level is Intermediate.If this condition is met, update the grid to be a x array of Squares.Set the instance variable, totalMines, to to indicate the number of mines that will be placed in the grid.Check if the difficulty input parameter level is Advanced.If this condition is met, update the grid to be a x array of Squares.Set the instance variable, totalMines, to to indicate the number of mines that will be placed in the grid.Here is the expected output of this method selecting the Beginner difficulty level:playRandom challenge for the bored worth pointsThis method will be tested by Autolab to give you feedback but it is worth points.To play the full game, with random boards, you need this method. In Minesweeper, the first click is always safe ie the first square a player clicks will never contain a mine You will simulate this game feature by creating a method that randomly places mines in the game board after the player makes their first click.Note that the method will pass in a difficulty level and the rowcoordinate and columncoordinate of the players first click.To complete this method:If the difficulty input parameter level is Beginner, place mines in the grid.If the difficulty input parameter level is Intermediate, place mines in the grid.If the difficulty input parameter level is Advanced, place mines in the grid.To place a mine in the grid:Generate a rowcoordinate and columncoordinate using the StdRandom class.Check if the randomized coordinate pair:DOES NOT equal the coordinates of the players first click.DOES NOT contain a mine.If both conditions are met, update sqNum to to represent a mine.Repeat this process until the designated number of mines have been placed in the grid according to the criteria above.Note that the outcome of this method will vary as mines are placed in the grid randomly. When testing your code, run chooseDifficulty followed by playRandom in that order.Here is an example of the expected output of this method in TextDriver selecting the Beginner difficulty level and making the first click at coordinates :Here is an example of the expected output of this method in GraphicalDriver selecting the Beginner difficulty level and making the first click at coordinates :Note that the first image is produced after a player makes their first click. The location of the mines and the sqNum of safe Squares in the grid are revealed. A player can click anywhere on the grid or press Enter on their keyboard to produce the second image where all Squares are concealed except for the Square where they made their first click.At this point, you can relaunch either driver and select option to play an entire game of your very own PlayMinesweeper. Have fun!How to use GraphicalDriver to play a full game of PlayMinesweeper: Note that if the flag in the top left corner is red, you are in flagging mode, and if the flag is black, you are in opening modeSelect option from the menu to play a full game.Select the corresponding number keys on your keyboard to choose your difficulty level.To open a Square, click on itTo flag a Square, press F on your keyboard and click the Square you want to flag.To remove a flag from a Square, press F on your keyboard and click the Square you want to deflag.To open a Square with a flag, remove the flag before clicking on the Square to open it
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
