Advertisement
KeeganT

Amazing

Jun 16th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package match.game;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. import java.util.ArrayList;
  5.  
  6. public class MatchGame
  7. {
  8.     static char[] hiddenBoard = new char[]{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
  9.     static char[] board = new char[]{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  10.     static char[] matches = new char[]{'A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H'};
  11.    
  12.     public static void generateBoard()
  13.     {
  14.         Random random = new Random();
  15.         int[] spotsTaken = new int[16];
  16.         for(int c = 0; c < 16; c++)
  17.         {
  18.             int spot = random.nextInt(16);
  19.             while(board[spot] != ' ')
  20.             {
  21.                 spot = random.nextInt(16);
  22.             }
  23.             board[spot] = matches[c];
  24.         }
  25.     }
  26.    
  27.     public static void showBoard(int spot1, int spot2)
  28.     {
  29.         for(int c = 0; c < 16; c++)
  30.         {
  31.             if(c == spot1 || c == spot2) System.out.print(board[c] + "  ");
  32.             else System.out.print(hiddenBoard[c] + "  ");
  33.             if((c+1)%4==0)System.out.println();
  34.         }
  35.     }
  36.    
  37.     public static void showRealBoard()
  38.     {
  39.         for(int c = 0; c < 4; c++)System.out.print(board[c] + "  ");
  40.         System.out.println();
  41.         for(int c = 4; c < 8; c++)System.out.print(board[c] + "  ");
  42.         System.out.println();
  43.         for(int c = 8; c < 12; c++)System.out.print(board[c] + "  ");
  44.         System.out.println();
  45.         for(int c = 12; c < 16; c++)System.out.print(board[c] + "  ");
  46.         System.out.println();
  47.     }
  48.    
  49.     public static void main(String[] args)
  50.     {
  51.         Scanner input = new Scanner(System.in);
  52.        
  53.         boolean win = false;
  54.         int lives = 10;
  55.         ArrayList<Integer> guessedSpots = new ArrayList();
  56.         generateBoard();
  57.        
  58.         System.out.println("Welcome to match game!");
  59.         System.out.println("The goal of this game is to find all 8 matches (A - H)");
  60.         System.out.println("Pick 2 spots on the board (1 - 16) and try to find the hidden matches.");
  61.         System.out.println("Careful! You only have 10 lives.");
  62.        
  63.         while(lives > 0)
  64.         {
  65.             showBoard(-1,-1);
  66.             System.out.println("Lives left: " + lives);
  67.             System.out.println("Pick spot 1: ");
  68.             int spot1 = input.nextInt();
  69.             while(spot1 > 16 || spot1 < 1)
  70.             {
  71.                 System.out.println("That spot does not exist! Guess another.");
  72.                 spot1 = input.nextInt();
  73.             }
  74.             while(guessedSpots.contains(spot1))
  75.             {
  76.                 System.out.println("You already guessed that spot! Guess another.");
  77.                 spot1 = input.nextInt();
  78.             }
  79.             spot1--;
  80.             showBoard(spot1,-1);
  81.             System.out.println("Pick spot 2: ");
  82.             int spot2 = input.nextInt();
  83.             while(spot2 > 16 || spot2 < 1)
  84.             {
  85.                 System.out.println("That spot does not exist! Guess another.");
  86.                 spot2 = input.nextInt();
  87.             }
  88.             while(guessedSpots.contains(spot2))
  89.             {
  90.                 System.out.println("You already guessed that spot! Guess another.");
  91.                 spot2 = input.nextInt();
  92.             }
  93.             spot2--;
  94.             if(board[spot1] == board[spot2])
  95.             {
  96.                 showBoard(spot1,spot2);
  97.                 System.out.println("Match found!");
  98.                 hiddenBoard[spot1] = ' ';
  99.                 hiddenBoard[spot2] = ' ';
  100.                 guessedSpots.add(spot1+1);
  101.                 guessedSpots.add(spot2+1);
  102.             }
  103.             else
  104.             {
  105.                 showBoard(spot1,spot2);
  106.                 System.out.println("Match not found!");
  107.                 lives--;
  108.             }
  109.             if(guessedSpots.size() == 16)win=true;
  110.             System.out.println(guessedSpots.size());
  111.             if(win == true)break;
  112.         }
  113.         if(win == true)
  114.         {
  115.             System.out.println("Congrats! You found all the matches. You win!");
  116.         }
  117.        
  118.         else
  119.         {
  120.             System.out.println("You ran out of lives! Better luck next time!");
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement