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

Create a simple game of mine sweeper with the following classes and a driver Dow

ID: 650046 • Letter: C

Question

Create a simple game of mine sweeper with the following classes and a driver

Download the driver

Put the driver in your project

DO NOT ALTER THE DRIVER!

Write a class file called MineSpace

Attributes of this clas

Selected determines whether or not this space has been clicked on

IsAMine this is whether or not it is a mine

SurroundingMines The number of mines surrounding that space

Create a default constructor with the following

Selected is set to false

IsAMine is randomly set to either true or false

Create Accessors and Mutators for each instance variable

MAKE SURE TO CHECK FOR VALID VALUES

Write another class SimpleMineSweeperBackEdn

Attributes of this class are

Spaces a 2D array of type mine space

SPACE_AMT a constant value set to 5 as this is a 5x5 minefield

numNotMinesLeft the number of mines left on the board

gameOver whether or not the game is over

win whether or not the player wins

Create a default constructor

Declare the 2D array

Call reset spaces

Create the following Methods

resetSpaces this method returns nothing and takes no parameters constructs each individual space and counts the number of not mines. It also sets each mine surroundingMines value by looking at each of its neighbors (HINT: looking setting the number of mines value maybe best done in a private method). Also it sets both gameOver and win to false.

getNumNotMinesLeft returns how many spaces that arent mines remain

getGameOver returns whether or not the game is over

getWin returns whether or not the player has won

printMines prints each mine space in the 2D array. It should print whether or not it is a mine and the number of surrounding mines. This is really good for debugging.

selectSpace this method which returns an instance of Space takes in two integers corresponding to the button that was selected. Much like other projects the x increase left to right and the y increases from top to bottom. This method should check if it is already select. If it hasnt been then set selected to true. Next check if that space is a mine. If it is then set gameOver to true and win to false. If not then subtract the number of not mines left. Next check if the number of not mines is 0 then game over should be set to true and win should be set to true.

Explanation / Answer

Java Program:

MineSweeper.java
public class MineSweeper {
public static void main(String[] args) {
Game game = new Game();

}

}

--> Game.java
public class Game {
private Board board;
boolean finish = false;
boolean win = false;
int turn=0;
  
public Jogo(){
board = new Board();
Play(board);
}
  
public void Play(Board board){
do{
turn++;
System.out.println("Turn "+turn);
board.show();
finish = board.setPosition();
  
if(!finish){
board.openNeighbors();
finish = board.win();
}
  
}while(!finish);
  
if(board.win()){
System.out.println("Congratulations, you let the 10 fields with the mines in "+turn+" turns");
board.showMines();
} else {
System.out.println("Mine! You lost!");
board.showMines();
}
}
}
--> Board.java
import java.util.Random;
import java.util.Scanner;

public class Board {
private int[][] mines;
private char[][] boardgame;
private int Line, Column;
Random random = new Random();
Scanner input = new Scanner(System.in);
  
public Board (){
mines = new int[10][10];
boardgame = new char[10][10];
startMines();
randomMines();
fillTips();
startBoard();
  
}
  
public boolean win(){
int count=0;
for(int line = 1 ; line < 9 ; line++)
for(int column = 1 ; column < 9 ; column++)
if(boardgame[line][column]=='_')
count++;
if(count == 10)
return true;
else
return false;
}
  
public void openNeighbors(){
for(int i=-1 ; i<2 ; i++)
for(int j=-1 ; j<2 ; j++)
if( (mines[Line+i][Column+j] != -1) && (Line != 0 && Line != 9 && Column != 0 && Column != 9) )
boardgame[Line+i][Column+j]=Character.forDigit(mines[Line+i][Column+j], 10);
  
}
  
public int getPosition(int Line, int Column){
return mines[Line][Column];
}
  
public boolean setPosition(){
  
do{
System.out.print(" Line: ");
Line = input.nextInt();
System.out.print("Column: ");
Column = input.nextInt();
  
if( (boardgame[Line][Column] != '_') && ((Line < 9 && Line > 0) && (Column < 9 && Column > 0)))
System.out.println("Field already shown");
  
if( Line < 1 || Line > 8 || Column < 1 || Column > 8)
System.out.println("Choose a number between 1 and 8");
  
}while((Line < 1 || Line > 8 || Column < 1 || Column > 8) || (boardgame[Line][Column] != '_') );
  
if(getPosition(Line, Column)== -1)
return true;
else
return false;
  
}
  
public void show(){
System.out.println(" Lines");
for(int Line = 8 ; Line > 0 ; Line--){
System.out.print(" "+Line + " ");
  
for(int Column = 1 ; Column < 9 ; Column++){
System.out.print(" "+ boardgame[Line][Column]);
}
  
System.out.println();
}
  
System.out.println(" 1 2 3 4 5 6 7 8");
System.out.println(" Columns");
  
}
  
public void fillTips(){
for(int line=1 ; line < 9 ; line++)
for(int column=1 ; column < 9 ; column++){
  
for(int i=-1 ; i<=1 ; i++)
for(int j=-1 ; j<=1 ; j++)
if(mines[line][column] != -1)
if(mines[line+i][column+j] == -1)
mines[line][column]++;
  
}
  
}
  
public void showMines(){
for(int i=1 ; i < 9; i++)
for(int j=1 ; j < 9 ; j++)
if(mines[i][j] == -1)
boardgame[i][j]='*';
  
show();
}
  
public void startBoard(){
for(int i=1 ; i<mines.length ; i++)
for(int j=1 ; j<mines.length ; j++)
boardgame[i][j]= '_';
}
  
public void startMines(){
for(int i=0 ; i<mines.length ; i++)
for(int j=0 ; j<mines.length ; j++)
mines[i][j]=0;
}
  
public void randomMines(){
boolean raffled;
int Line, Column;
for(int i=0 ; i<10 ; i++){
  
do{
Line = random.nextInt(8) + 1;
Column = random.nextInt(8) + 1;
  
if(mines[Line][Column] == -1)
raffled=true;
else
raffled = false;
}while(raffled);
  
mines[Line][Column] = -1;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote