Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Minefield Game Implement a graphical version of the Minefield Game. The game wil

ID: 3656001 • Letter: M

Question

Minefield Game Implement a graphical version of the Minefield Game. The game will have a 5x5 grid of squares, with two of the squares designated as the Start and Finish. Hidden underneath two of the squares will be landmines. The objective of the game is to move from the Start to the Finish without encountering any landmines. The player cannot move diagonally. Write a class MineField that extends JFrame and implements ActionListener. It should have two panels: the top panel contains a 5x5 grid of buttons simulating the minefield and the bottom panel contains a Clear button and a Done button. Pushing the Clear button begins the game. It should cause two randomly chosen minefield buttons to be designated as Start and Finish, and two randomly chosen minefield buttons to have mines hidden below them. When a user clicks on a minefield button (from Start to Finish), it should change color to show the selected path. After a user completes the path, pushing the Done button should cause a check to see whether a mine was encountered and a message to appear indicating safe passage or failure. I strongly suggest that you develop this program incrementally, a little at a time. First, simply draw a window big enough to hold the panels. Then add the two panels. Then add buttons to the panels. Then add an ActionListener for each button that indicates when it was pushed. Then add the functionality for the Clear. Then add the functionality for the Done. Etc.

Explanation / Answer

import java.util.Scanner; public class Minesweeper { public static void main (String [] args) { int size = welcomeAndAskForSizeOfField(); int [] [] minesfield = new int [size] [size]; char [] []outputField = new char [size][size]; init (minesfield, outputField); displayOutput(outputField); boolean gameOver = false; while (gameOver != true) { gameOver = playGame(minesfield, outputField); displayOutput(outputField); } } // main //********************************************* welcomeAndAskForSizeOfField public static int welcomeAndAskForSizeOfField() { Scanner keybd = new Scanner(System.in); System.out.println("Welcome to the game of minesweeper! If you would like to play on a 5x5 grid press 5"); System.out.println("If you want to play on a 10x10 grid press 10"); int choice = keybd.nextInt();//input by user if (choice==5 || choice ==10) { return choice; } else//if not 5 or 10 { System.out.println("Error, please try again"); choice=welcomeAndAskForSizeOfField();//a loop until correct grid size is chosen } return choice; } //********************************************* init public static void init (int [] [] mines, char [] [] output) { for (int r = 0; r