Minesweeper Game in java Create a class called NewGame that will contain a two d
ID: 670443 • Letter: M
Question
Minesweeper Game in java
Create a class called NewGame that will contain a two dimensional array of characters. within the class place the following data private attributes:
An array of characters called game that is at least 8 X 8, at least 5 bombs that are in random positions on the board. Cover the game with x, so that it is an array of 8x8 of X’s separated by lines, perhaps something like (/).
then add getters and setters for the array. take out any setters that might not be a good fit/proper.
then, do a print statement to the class that includes the values on the side for the rows and on the top for the columns. Create a constructor that fills an object with the information obtained by the random bombs. it run when you create an object.
Create a menu that allows you to choose the next step for your game. e.g:
Hint: 2, 5 -- looks in row 2 and column 5 for a bomb, reset game, new game, quit game.
Actions are such that choosing a spot that contains the bomb will end the game. Choosing a position that is completely blank will open that spot to the user.
Explanation / Answer
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class MineSweeper {
public static void main(String []args){
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Game !");
System.out.println("---------------------------------------------");
System.out.println("Options: 1. New Game 2. Reset Game 3.Quit");
System.out.println("Enter your option (1/2/3)");
int option = 0;
NewGame game = null;
do {
option = scan.nextInt();
if(option == 1) {
game = new NewGame();
option = MineSweeper.runGame(game, option, scan);
}
if(option == 2) {
if(game == null) {
System.out.println("First start the new game by entering 1");
} else {
option = MineSweeper.runGame(game, option, scan);
}
}
if(option == 3) {
break;
}
} while(option !=3);
}
public static int runGame(NewGame game, int option,Scanner scan) {
System.out.println(option == 1 ? "New Game Started" : "Game Restarted");
game.printGameArray();
if(option == 2)
System.out.println("Enter any time 2 to Reset Game, 3 to Quit to come out of the game");
int suboption = 0;
do {
System.out.println("Enter a location (in the format 2,3) ");
String loc = scan.next();
String fields[] = loc.split(",");
if(fields.length == 1) {
suboption = Integer.parseInt(fields[0]);
option = suboption;
break;
} else if(fields.length > 1){
int row = Integer.parseInt(fields[0])-1; //array index starts from 0
int column = Integer.parseInt(fields[1])-1;
if(game.isBomb(row, column)) {
System.out.println("Oops! Bomb. GAME OVER");
return 3; //quit
}
}
} while(suboption == 0 );
return option;
}
}
class NewGame {
private char[][] charArray;
private ArrayList<Integer> bombIndexRow;
private ArrayList<Integer> bombIndexColumn;
private static int MATRIX_N_X_N= 8; //n x n square array
private static int NO_OF_BOMBS = 5; // CHANge here count of bombs as per your requirement
public NewGame() {
charArray = new char[MATRIX_N_X_N][MATRIX_N_X_N];
for(int i = 0; i <MATRIX_N_X_N; i++) {
for(int j = 0; j < MATRIX_N_X_N; j++) {
charArray[i][j] = 'X';
}
}
initBombs();
}
public char[][] getCharArray() {
return charArray;
}
public void setCharArray(char[][] charArray) {
this.charArray = charArray;
}
private void setBombs(ArrayList<Integer> bombIndexRow, ArrayList<Integer> bombIndexColumn) {
this.bombIndexColumn = bombIndexColumn;
this.bombIndexRow = bombIndexRow;
}
public int getNoOfBombs() {
return NO_OF_BOMBS;
}
public void restBombs() {
initBombs();
}
public void newGame() {
restBombs();
//Do anything else you want
}
private void initBombs() {
ArrayList<Integer> bombIndexRow = new ArrayList<Integer>();
ArrayList<Integer> bombIndexColumn = new ArrayList<Integer>();
Random rm = new Random();
for(int i = 0; i < NO_OF_BOMBS; i++) {
int row = rm.nextInt(5);
int column = rm.nextInt(5);
//System.out.println(" Bomb in : ("+row+","+column+")");
bombIndexRow.add(row);
bombIndexColumn.add(column);
}
setBombs(bombIndexRow, bombIndexColumn);
}
public void printGameArray() {
for(int i = 0; i < MATRIX_N_X_N; i++)
System.out.print(" "+ (i+1));
for(int i = 0; i <MATRIX_N_X_N; i++) {
for(int j = 0; j <= MATRIX_N_X_N; j++) {
if(j == 0)
System.out.print(j+1);
else
System.out.print(" "+this.charArray[i][j-1]);
}
System.out.println();
}
}
public boolean isBomb(int row, int column) {
System.out.println(" isBomb ->Bomb in : ("+row+","+column+")");
for(int i = 0; i < NO_OF_BOMBS; i++) {
if(bombIndexRow.contains(row)) {
int index = bombIndexRow.indexOf(row);
//System.out.println(bombIndexColumn.get(index)+" isBomb-row->Bomb in : ("+row+","+column+")");
if(bombIndexColumn.get(index) == column) {
//System.out.println(index+" isBomb-column->Bomb in : ("+row+","+column+")");
return true;
}
}
}
return false;
}
public static int getMATRIX_N_X_N() {
return MATRIX_N_X_N;
}
public static void setMATRIX_N_X_N(int mATRIX_N_X_N) {
MATRIX_N_X_N = mATRIX_N_X_N;
}
public static int getNO_OF_BOMBS() {
return NO_OF_BOMBS;
}
public static void setNO_OF_BOMBS(int nO_OF_BOMBS) {
NO_OF_BOMBS = nO_OF_BOMBS;
}
}