Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.Scanner;
- public class MineSweeper {
- Scanner in = new Scanner(System.in);
- int[][] winnings;
- int[][] configuration;
- int bombs, size = 0;
- String playerName;
- List<String> usedLocation;
- MineSweeper(){
- int repeat;
- System.out.println("Welcome To Minesweeper!\n=======================");
- System.out.print("Enter Player Name: ");
- playerName = in.nextLine();
- do{
- bombs = 0;
- System.out.print("\nChoose the size of grid (Press 1 for 5x5, Press 2 for 10x10): ");
- size = in.nextInt();
- in.nextLine();
- size = size == 1 ? 5 : 10;
- winnings = new int[size][size];
- configuration = new int[size][size];
- usedLocation = new ArrayList<String>();
- play();
- System.out.print("\nDear "+playerName+", do you wish to play again (Press 1 for Yes, Press 2 for No): ");
- repeat = in.nextInt();
- in.nextLine();
- }
- while(repeat == 1);
- System.out.print("\nSEE YOU BYE !");
- }
- private void play() {
- int x, y, gameResult, count = 0;
- boolean illegalMove = false, repeatedMove = false;
- if (bombs == 0){
- getGameDetails();
- }
- do {
- do {
- illegalMove = repeatedMove = false;
- System.out.print("\nEnter row and column of the cell you want to open [row col]: ");
- x = in.nextInt();
- y = in.nextInt();
- in.nextLine();
- if (x > size - 1 || y > size - 1 || x < 0 || y < 0) {
- illegalMove = true;
- System.out.println("Illegal move. Please enter again.\n");
- } else if (usedLocation.contains(x + "" + y)) {
- repeatedMove = true;
- System.out.println("This position has been checked already. Please enter egain\n");
- }
- } while (illegalMove || repeatedMove);
- gameResult = makeMove(x, y, ++count);
- } while (gameResult == 1);
- if(gameResult == 0){
- System.out.println("\n"+playerName+" have picked the Bomb.\n");
- } else if(gameResult == 2){
- System.out.println("\n"+playerName+" have won the game!\n");
- }
- }
- private int makeMove(int x, int y, int count) {
- int neighbor = 0, returnVal;
- boolean userWin;
- if(winnings[x][y] == 1){
- returnVal = 0;
- } else {
- usedLocation.add(x +""+ y);
- neighbor = getNeighborBombs(x, y);
- configuration[x][y] = neighbor;
- userWin = won(count);
- returnVal = userWin ? 2 : 1;
- }
- reportWinnings(returnVal);
- return returnVal;
- }
- private boolean won(int count) {
- int winningCount = winnings.length * winnings.length - bombs;
- if(count == winningCount){
- return true;
- } else {
- System.out.println("Open "+(winningCount - count)+" more correctly to win.");
- }
- return false;
- }
- private int getNeighborBombs(int x, int y) {
- int count = 0;
- //Top-Left
- if(x > 0 && y > 0){
- count = winnings[x-1][y-1] == 1 ? ++count : count;
- }
- //Top-Center
- if(x > 0){
- count = winnings[x-1][y] == 1 ? ++count : count;
- }
- //Top-Right
- if(x > 0 && y < winnings.length-1){
- count = winnings[x-1][y+1] == 1 ? ++count : count;
- }
- //Right
- if(y < winnings.length-1){
- count = winnings[x][y+1] == 1 ? ++count : count;
- }
- //Bottom-Right
- if(x < winnings.length-1 && y < winnings.length-1){
- count = winnings[x+1][y+1] == 1 ? ++count : count;
- }
- //Bottom
- if(x < winnings.length-1){
- count = winnings[x+1][y] == 1 ? ++count : count;
- }
- //Bottom-Left
- if(x < winnings.length-1 && y > 0){
- count = winnings[x+1][y-1] == 1 ? ++count : count;
- }
- //Left
- if(y > 0){
- count = winnings[x][y-1] == 1 ? ++count : count;
- }
- return count;
- }
- private void getGameDetails() {
- Random r = new Random();
- int maxBombs = winnings.length * winnings.length - 1;
- int x,y;
- do{
- System.out.print("How many bombs do you want to place (Max: "+maxBombs+")? ");
- bombs = in.nextInt();
- in.nextLine();
- }
- while(bombs > maxBombs || bombs < 1);
- for (int i = 0; i < bombs; i++) {
- x = r.nextInt(winnings.length);
- y = r.nextInt(winnings.length);
- if(winnings[x][y] == 0){
- winnings[x][y] = 1;
- } else {
- i--;
- }
- }
- }
- private void reportWinnings(int returnVal) {
- String number;
- for (int i = 0; i < configuration.length; i++) {
- for (int j = 0; j < configuration[i].length; j++) {
- number = usedLocation.contains(i +""+ j) ? String.valueOf(configuration[i][j]) : returnVal == 1 ? " " : winnings[i][j] == 1 ? "X" : " ";
- System.out.print("["+number+"]");
- }
- System.out.println();
- }
- }
- public static void main(String args[]) {
- MineSweeper mineSweeper = new MineSweeper();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment