Académique Documents
Professionnel Documents
Culture Documents
-------------------------------------------------
3. Implement a Minesweeper-like game. All its functionalities will be implemented
in
the GameMinesweeper class. The constructor will take as input the dimensions of the
game
board and the number of hidden mines. The number of mines cannot be larger than
one
third of the total number of cells on the game board. The position of the mines
will
be randomly generated. Each cell on the board will contain 0 for a unmined cell, 1
for
a mined cell and X for a cell which has not yet been checked. The user will type
in using
the keyboard a position on the board, specified by (a.,b), where a is the row and b
is the
column. At each step the application will print the current state of the board and
the number
of unmined cells left in the game. The game is won when the user checks all the
cells without
hitting a mine.
----------------------------------------------------
import java.util.Random;
import java.util.Scanner;
Start();
}
public static void Start() {
System.out.println("Enter the board dimension: ");
Scanner s = new Scanner(System.in);
GameMineSweeper game=new GameMineSweeper(s.nextInt());
game.Gen();
game.Print();
int nr=0;
for(int i=0; i<dim;i++) {
for(int j=0; j<dim;j++)
if(game.board[i][j]==0)
nr++;
}
int loop=0;
while(loop!=1) {
System.out.println("Enter a bidimensional position: ");
int x,y;
int lop=0;
x=s.nextInt();
y=s.nextInt();
while(lop!=1) {
if((x>dim-1)||(y>dim-1)) {
System.out.println("Invalid Position! ");
System.out.println("Enter a bidimensional position: ");
x=s.nextInt();
y=s.nextInt();
}
else lop=1;
}
if(game.board[x][y]==1) {
game.bprint[x][y]="1";
System.out.println(" ");
game.Print();
System.out.println("Game Over! ");
loop=1;
}
if(game.board[x][y]==0) {
game.bprint[x][y]="0";
System.out.println(" ");
game.Print();
nr--;
if(nr!=0)
System.out.println("Remaining empty spaces: "+nr);
else
System.out.println("You Won! ");
System.out.println(" ");
}
}
}
}