Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package match.game;
- import java.util.Scanner;
- import java.util.Random;
- import java.util.ArrayList;
- public class MatchGame
- {
- static char[] hiddenBoard = new char[]{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
- static char[] board = new char[]{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
- static char[] matches = new char[]{'A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H'};
- public static void generateBoard()
- {
- Random random = new Random();
- int[] spotsTaken = new int[16];
- for(int c = 0; c < 16; c++)
- {
- int spot = random.nextInt(16);
- while(board[spot] != ' ')
- {
- spot = random.nextInt(16);
- }
- board[spot] = matches[c];
- }
- }
- public static void showBoard(int spot1, int spot2)
- {
- for(int c = 0; c < 16; c++)
- {
- if(c == spot1 || c == spot2) System.out.print(board[c] + " ");
- else System.out.print(hiddenBoard[c] + " ");
- if((c+1)%4==0)System.out.println();
- }
- }
- public static void showRealBoard()
- {
- for(int c = 0; c < 4; c++)System.out.print(board[c] + " ");
- System.out.println();
- for(int c = 4; c < 8; c++)System.out.print(board[c] + " ");
- System.out.println();
- for(int c = 8; c < 12; c++)System.out.print(board[c] + " ");
- System.out.println();
- for(int c = 12; c < 16; c++)System.out.print(board[c] + " ");
- System.out.println();
- }
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- boolean win = false;
- int lives = 10;
- ArrayList<Integer> guessedSpots = new ArrayList();
- generateBoard();
- System.out.println("Welcome to match game!");
- System.out.println("The goal of this game is to find all 8 matches (A - H)");
- System.out.println("Pick 2 spots on the board (1 - 16) and try to find the hidden matches.");
- System.out.println("Careful! You only have 10 lives.");
- while(lives > 0)
- {
- showBoard(-1,-1);
- System.out.println("Lives left: " + lives);
- System.out.println("Pick spot 1: ");
- int spot1 = input.nextInt();
- while(spot1 > 16 || spot1 < 1)
- {
- System.out.println("That spot does not exist! Guess another.");
- spot1 = input.nextInt();
- }
- while(guessedSpots.contains(spot1))
- {
- System.out.println("You already guessed that spot! Guess another.");
- spot1 = input.nextInt();
- }
- spot1--;
- showBoard(spot1,-1);
- System.out.println("Pick spot 2: ");
- int spot2 = input.nextInt();
- while(spot2 > 16 || spot2 < 1)
- {
- System.out.println("That spot does not exist! Guess another.");
- spot2 = input.nextInt();
- }
- while(guessedSpots.contains(spot2))
- {
- System.out.println("You already guessed that spot! Guess another.");
- spot2 = input.nextInt();
- }
- spot2--;
- if(board[spot1] == board[spot2])
- {
- showBoard(spot1,spot2);
- System.out.println("Match found!");
- hiddenBoard[spot1] = ' ';
- hiddenBoard[spot2] = ' ';
- guessedSpots.add(spot1+1);
- guessedSpots.add(spot2+1);
- }
- else
- {
- showBoard(spot1,spot2);
- System.out.println("Match not found!");
- lives--;
- }
- if(guessedSpots.size() == 16)win=true;
- System.out.println(guessedSpots.size());
- if(win == true)break;
- }
- if(win == true)
- {
- System.out.println("Congrats! You found all the matches. You win!");
- }
- else
- {
- System.out.println("You ran out of lives! Better luck next time!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement