Implement in Java with a GUI the game Minesweeper with the following restriction
ID: 3737140 • Letter: I
Question
Implement in Java with a GUI the game Minesweeper with the following restrictions:
1. Board size: you should give the option to the user to choose at the start of the game between "Beginner", "Intermediate" and "Advanced". The board size and number of mines for each of these three choices are the following: Beginner: board 8x8 and 10 mines, Advanced: board 16x16 and 36 mines and finally Expert: board 24x24 and 91 mines
2. your game should have a timer that displays the number of seconds played so far, if the clock reaches 1 minute for Beginner, 3 minutes for Advanced or 10 minutes for Expert then the game is lost and the mines should explode
3. Implement some small animation of mines exploding and at the same time the game should also make an exploding sound. Obviously only the not yet uncovered mines should explode,the ones that were marked as mines should not explode. Furthermore your program should point out with a red X the locations where the player marked as mine and were not correct.
4. the control of the game should be done completely by mouse clicks: left click uncovers that location, right click marks as mine in that location, a second right click erases the mine marking for that location
5. BONUS for +2p a second click on a revealed location that has the number of mines adjacent to it marked will uncover automatically all the cells adjacent that are supposed to not have any mines.
As an example of what you should implement try this implementation of minesweeper: http://www.freeminesweeper.org/minecore.html
please provide me in .java execute in eclipse luna. thanks in advance :)
Explanation / Answer
import java.util.*;
public class Game{
import java.util.*;
public class Game{
public String[][] field = new String[12][12]; //12 rows, 12 columns. public String[][] display = new String[12][12]; //This is the field that is visible to the player. public Boolean isDone = false; public Boolean isWin = false; private String unknown = " ? "; private String mine = " * "; private String empty = " "; //Constructor places empty spaces in tiles. public Game(){ int row = 0; int column = 0; for(int x = 0; x < field.length; x++){ for(int y = 0; y < field[0].length; y++){ //Places blank spaces in buffer zones. if((x == 0 || x == field.length - 1)||(y == 0 || y == field[0].length - 1)){ field[x][y] = empty; display[x][y] = empty; } //Places ? in game field. else{ field[x][y] = unknown; display[x][y] = unknown; } } } } //Displays the field. public static void printGame(String[][] str){ for(int x = 1; x < str.length - 1; x++){ for(int y = 0; y < str[0].length ; y++){ //Formats row. if(y > 0 && y < str[0].length) System.out.print("|"); else System.out.println(""); System.out.print(str[x][y]); //Prints out content of each tile. } } } //Updates the console after every match. public void update(){ printGame(display); System.out.println(""); } //Places n mines at random on the field. public void generateMines(int n){ for(int m = 0; m < n; m++){ //Loops until a mine is placed. while(true){ int x, y = 0; //Clears vars. x = (int)(10*Math.random()); y = (int)(10*Math.random()); //So that a mine is placed in a tile visible to the player. if(x >= 1 && x <= 10){ if(y >= 1 && y <= 10){ //Checks if a mine is present in a spot. if(!field[x][y].equals(mine)){ field[x][y] = mine; break; } } } } } } //On first move, this clears the area around the selected tile. public void clear(int x, int y){ for(int i = (x - 1); i <= (x + 1); i++){ for(int j = (y - 1); j <= (y + 1); j++){ if(field[i][j].equals(unknown) == true){ display[i][j] = empty; field[i][j] = empty; } } } } //Gets the value of a tile. public String getTile(int x, int y){ return field[x][y]; } //Detects number of mines around a selected tile. public void detect(){ for(int x = 1; x < display.length - 2; x++){ //Cycles thru the entire visible display. for(int y = 1; y < display.length - 2; y++){ if(field[x][y].equals(empty) == true){ int nums = 0; //Var for counting mines. for(int i = (x - 1); i <= (x + 1); i++){ for(int j = (y - 1); j <= (y + 1); j++){ if(field[i][j].equals(mine) == true) nums++; //Incrememnts nums var when a mine is detected. } } display[x][y] = " " + nums + " "; } } } } //Takes user's selected coordinates and adjusts the board. public void turn(int x, int y){ if(field[x][y].equals(unknown) == true){ //If the spot hasn't been selected, it is cleared. isDone = false; display[x][y] = empty; field[x][y] = empty; }else if(field[x][y].equals(mine) == true){ //If the user selects a mine. isDone = true; //Game is done. isWin = false; //User doesn't win. System.out.println("You've lost!"); }else if(display[x][y].equals(empty) == true && field[x][y].equals(empty)){ isDone = false; System.out.println("This tile's been cleared!"); } } //Determines if a player has cleared all safe tiles. public void isVictory(){ int tile = 0; //Var for the number of uncleared tiles in the array. for(int i = 0; i < field.length; i++){ for(int j = 0; j < field[0].length; j++){ if(field[i][j].equals(unknown) == true) tile++; //If there are uncleared tiles, var is incremented. } } if(tile != 0) isWin = false; //If there are still uncleared tiles, player hasn't won. else{ isWin = true; isDone = true; } } //Determines if the game is finished. public Boolean getDone(){ return isDone; } //Determines if a player won. public Boolean getWin(){ return isWin; } //Displays location of mines at end of game. public void onEnd(){ printGame(field); } } public class GameRunner{ //Displays rules at beginning of game. public static void init(){ System.out.println("The object of the game is to clear the field of all safe tiles."); System.out.println("Play by entering the coordinates of a tile you'd like to select."); System.out.println("The range of the tiles is 1-10. The origin is the upper left tile."); System.out.println("There are 16 mines. Selecting a tile with a mine will end the game."); } public static void test(){ Game game = new Game(); game.generateMines(16); game.update(); Scanner scan = new Scanner(System.in); int x, y; System.out.print("Enter an x coordinate."); x = scan.nextInt(); System.out.print("Enter a y coordinate."); y = scan.nextInt(); /* * To ensure that the player does not lose on their first move, * the game will move a mine to another tile if the player * happens to select a tile with a mine present. */ if(game.getTile(x,y).equals(" * ") == true){ game.generateMines(1); game.field[x][y] = " ? "; } game.clear(x,y); game.detect(); game.update(); //After first move, loops until the game ends. while(true){ if(game.getDone() == true && game.getWin() == true){ //If the player wins. System.out.println("You win!"); game.onEnd(); break; }else if(game.getDone() == true){ //If the player loses. game.onEnd(); break; }else if(game.getDone() == false){ //While the player hasn't lost or won. x = -1; y = -1; //Resets values. System.out.print("Enter an x coordinate."); y = scan.nextInt(); System.out.print("Enter a y coordinate."); x = scan.nextInt(); game.turn(x,y); game.isVictory(); game.detect(); game.update(); } } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.