dhurdev

MineSweeper

Nov 14th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class MineSweeper {
  7.     Scanner in = new Scanner(System.in);
  8.     int[][] winnings;
  9.     int[][] configuration;
  10.     int bombs, size = 0;
  11.     String playerName;
  12.     List<String> usedLocation;
  13.    
  14.     MineSweeper(){
  15.         int repeat;
  16.         System.out.println("Welcome To Minesweeper!\n=======================");
  17.         System.out.print("Enter Player Name: ");
  18.         playerName = in.nextLine();
  19.        
  20.         do{
  21.             bombs = 0;
  22.             System.out.print("\nChoose the size of grid (Press 1 for 5x5, Press 2 for 10x10): ");
  23.             size = in.nextInt();
  24.             in.nextLine();
  25.            
  26.             size = size == 1 ? 5 : 10;
  27.             winnings = new int[size][size];
  28.             configuration = new int[size][size];
  29.             usedLocation = new ArrayList<String>();
  30.            
  31.             play();
  32.            
  33.             System.out.print("\nDear "+playerName+", do you wish to play again (Press 1 for Yes, Press 2 for No): ");
  34.             repeat = in.nextInt();
  35.             in.nextLine();
  36.         }
  37.         while(repeat == 1);
  38.        
  39.         System.out.print("\nSEE YOU BYE !");
  40.     }
  41.    
  42.     private void play() {
  43.         int x, y, gameResult, count = 0;
  44.         boolean illegalMove = false, repeatedMove = false;
  45.        
  46.         if (bombs == 0){
  47.             getGameDetails();
  48.         }
  49.  
  50.         do {
  51.             do {
  52.                 illegalMove = repeatedMove = false;
  53.                 System.out.print("\nEnter row and column of the cell you want to open [row col]: ");
  54.                 x = in.nextInt();
  55.                 y = in.nextInt();
  56.                 in.nextLine();
  57.  
  58.                 if (x > size - 1 || y > size - 1 || x < 0 || y < 0) {
  59.                     illegalMove = true;
  60.                     System.out.println("Illegal move. Please enter again.\n");
  61.                 } else if (usedLocation.contains(x + "" + y)) {
  62.                     repeatedMove = true;
  63.                     System.out.println("This position has been checked already. Please enter egain\n");
  64.                 }
  65.             } while (illegalMove || repeatedMove);
  66.  
  67.             gameResult = makeMove(x, y, ++count);
  68.         } while (gameResult == 1);
  69.        
  70.         if(gameResult == 0){
  71.             System.out.println("\n"+playerName+" have picked the Bomb.\n");
  72.         } else if(gameResult == 2){
  73.             System.out.println("\n"+playerName+" have won the game!\n");
  74.         }
  75.     }
  76.  
  77.     private int makeMove(int x, int y, int count) {
  78.         int neighbor = 0, returnVal;
  79.         boolean userWin;
  80.        
  81.         if(winnings[x][y] == 1){
  82.             returnVal = 0;
  83.         } else {
  84.             usedLocation.add(x +""+ y);
  85.             neighbor = getNeighborBombs(x, y);
  86.             configuration[x][y] = neighbor;
  87.             userWin = won(count);
  88.             returnVal = userWin ? 2 : 1;
  89.         }
  90.        
  91.         reportWinnings(returnVal);
  92.        
  93.         return returnVal;
  94.     }
  95.  
  96.     private boolean won(int count) {
  97.         int winningCount = winnings.length * winnings.length - bombs;
  98.        
  99.         if(count == winningCount){
  100.             return true;
  101.         } else {
  102.             System.out.println("Open "+(winningCount - count)+" more correctly to win.");
  103.         }
  104.        
  105.         return false;
  106.     }
  107.  
  108.     private int getNeighborBombs(int x, int y) {
  109.         int count = 0;
  110.        
  111.         //Top-Left
  112.         if(x > 0 && y > 0){
  113.             count = winnings[x-1][y-1] == 1 ? ++count : count;
  114.         }
  115.        
  116.         //Top-Center
  117.         if(x > 0){
  118.             count = winnings[x-1][y] == 1 ? ++count : count;
  119.         }
  120.        
  121.         //Top-Right
  122.         if(x > 0 && y < winnings.length-1){
  123.             count = winnings[x-1][y+1] == 1 ? ++count : count;
  124.         }
  125.        
  126.         //Right
  127.         if(y < winnings.length-1){
  128.             count = winnings[x][y+1] == 1 ? ++count : count;
  129.         }
  130.        
  131.         //Bottom-Right
  132.         if(x < winnings.length-1 && y < winnings.length-1){
  133.             count = winnings[x+1][y+1] == 1 ? ++count : count;
  134.         }
  135.        
  136.         //Bottom
  137.         if(x < winnings.length-1){
  138.             count = winnings[x+1][y] == 1 ? ++count : count;
  139.         }
  140.        
  141.         //Bottom-Left
  142.         if(x < winnings.length-1 && y > 0){
  143.             count = winnings[x+1][y-1] == 1 ? ++count : count;
  144.         }
  145.        
  146.         //Left
  147.         if(y > 0){
  148.             count = winnings[x][y-1] == 1 ? ++count : count;
  149.         }
  150.        
  151.         return count;
  152.     }
  153.  
  154.     private void getGameDetails() {
  155.         Random r = new Random();
  156.         int maxBombs = winnings.length * winnings.length - 1;
  157.         int x,y;
  158.        
  159.         do{
  160.             System.out.print("How many bombs do you want to place (Max: "+maxBombs+")?  ");
  161.             bombs = in.nextInt();
  162.             in.nextLine();
  163.         }
  164.         while(bombs > maxBombs || bombs < 1);
  165.        
  166.         for (int i = 0; i < bombs; i++) {
  167.             x = r.nextInt(winnings.length);
  168.             y = r.nextInt(winnings.length);
  169.            
  170.             if(winnings[x][y] == 0){
  171.                 winnings[x][y] = 1;
  172.             } else {
  173.                 i--;
  174.             }
  175.         }
  176.     }
  177.  
  178.     private void reportWinnings(int returnVal) {
  179.         String number;
  180.         for (int i = 0; i < configuration.length; i++) {
  181.             for (int j = 0; j < configuration[i].length; j++) {
  182.                 number = usedLocation.contains(i +""+ j) ? String.valueOf(configuration[i][j]) : returnVal == 1 ? " " : winnings[i][j] == 1 ? "X" : " ";
  183.                 System.out.print("["+number+"]");
  184.             }
  185.             System.out.println();
  186.         }
  187.     }
  188.  
  189.     public static void main(String args[]) {
  190.         MineSweeper mineSweeper = new MineSweeper();
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment