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 String alphabet="ABCDEFGHIJKLMNOPQR";
- static char[] alphArray = alphabet.toCharArray();
- static ArrayList<Character> hiddenBoard2 = new ArrayList();
- static ArrayList<Character> board2 = new ArrayList();
- static ArrayList<Character> matches2 = new ArrayList();
- static char[] hiddenBoard;
- static char[] board;
- static char[] matches;
- public static void generateArrays(int boardChoice)
- {
- if(boardChoice == 1)
- {
- for(int c = 0; c < 16; c++)
- {
- hiddenBoard2.add('0');
- board2.add(' ');
- }
- for(int c = 0; c < 8; c++)
- {
- matches2.add(alphArray[c]);
- matches2.add(alphArray[c]);
- }
- }
- if(boardChoice == 2)
- {
- for(int c = 0; c < 36; c++)
- {
- hiddenBoard2.add('0');
- board2.add(' ');
- }
- for(int c = 0; c < 18; c++)
- {
- matches2.add(alphArray[c]);
- matches2.add(alphArray[c]);
- }
- }
- String str = hiddenBoard2.toString().replaceAll(",", "");
- hiddenBoard = str.substring(1, str.length()-1).replaceAll(" ", "").toCharArray();
- str = board2.toString().replaceAll(",","");
- board = str.substring(1, str.length()-1).toCharArray();
- str = matches2.toString().replaceAll(",", "");
- matches = str.substring(1, str.length()-1).replaceAll(" ", "").toCharArray();
- }
- public static void generateBoard1()
- {
- Random random = new Random();
- 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 generateBoard2()
- {
- Random random = new Random();
- for(int c = 0; c < 36; c++)
- {
- int spot = random.nextInt(36);
- while(board[spot] != ' ')
- {
- spot = random.nextInt(36);
- }
- board[spot] = matches[c];
- }
- }
- public static void showBoard1(int spot1, int spot2)
- {
- for(int c = 0; c < 16; c++)
- {
- if((c-4) % 4 == 0)System.out.println("-----------------");
- if(c % 4 == 0)System.out.print("| ");
- if(c == spot1 || c == spot2)System.out.print(board[c] + " | ");
- else System.out.print(hiddenBoard[c] + " | ");
- if((c+1) % 4 == 0)System.out.println();
- }
- System.out.println("-----------------");
- }
- public static void showBoard2(int spot1, int spot2)
- {
- for(int c = 0; c < 36; c++)
- {
- if((c-6) % 6 == 0)System.out.println("-------------------------");
- if(c % 6 == 0)System.out.print("| ");
- if(c == spot1 || c == spot2)System.out.print(board[c] + " | ");
- else System.out.print(hiddenBoard[c] + " | ");
- if((c+1) % 6 == 0)System.out.println();
- }
- System.out.println("-------------------------");
- }
- public static void main(String[] args) throws InterruptedException
- {
- Scanner input = new Scanner(System.in);
- boolean win = false;
- ArrayList<Integer> guessedSpots = new ArrayList();
- System.out.println("Welcome to match game!");
- System.out.println("Select difficult:\n1) Easy\n2) Medium\n3) Hard");
- int diff = input.nextInt();
- if(diff == 3)
- {
- System.out.println("The goal of this game is to find all 18 matches (A - R)");
- System.out.println("Pick 2 spots on the board (1 - 36) and try to find the hidden matches.");
- }
- else
- {
- 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.");
- }
- int lives = 10, points = 0, streak = 1;
- if(diff == 1)
- {
- generateArrays(1);
- generateBoard1();
- }
- else if(diff == 2)
- {
- generateArrays(1);
- generateBoard1();
- lives = 8;
- }
- else if(diff == 3)
- {
- generateArrays(2);
- generateBoard2();
- }
- System.out.println("Careful! You only have " + lives + " lives.");
- while(lives > 0)
- {
- if(diff == 3)showBoard2(-1, -1);
- else showBoard1(-1, -1);
- System.out.println("Lives left: " + lives);
- System.out.println("Pick spot 1: ");
- int spot1 = input.nextInt();
- while(guessedSpots.contains(spot1))
- {
- System.out.println("You already guessed that spot! Guess another.");
- spot1 = input.nextInt();
- }
- if(diff == 3)
- {
- while(spot1 > 36 || spot1 < 1)
- {
- System.out.println("That spot does not exist! Guess another.");
- spot1 = input.nextInt();
- }
- }
- else
- {
- while(spot1 > 16 || spot1 < 1)
- {
- System.out.println("That spot does not exist! Guess another.");
- spot1 = input.nextInt();
- }
- }
- spot1--;
- if(diff == 3)showBoard2(spot1, -1);
- else showBoard1(spot1, -1);
- System.out.println("Pick spot 2: ");
- int spot2 = input.nextInt();
- while(spot2 == spot1 + 1)
- {
- System.out.println("You can't pick the same spot! Guess another.");
- spot2 = input.nextInt();
- }
- while(guessedSpots.contains(spot2))
- {
- System.out.println("You already guessed that spot! Guess another.");
- spot2 = input.nextInt();
- }
- if(diff == 3)
- {
- while(spot2 > 36 || spot2 < 1)
- {
- System.out.println("That spot does not exist! Guess another.");
- spot2 = input.nextInt();
- }
- }
- else
- {
- while(spot2 > 16 || spot2 < 1)
- {
- System.out.println("That spot does not exist! Guess another.");
- spot2 = input.nextInt();
- }
- }
- spot2--;
- if(board[spot1] == board[spot2])
- {
- if(diff == 3)showBoard2(spot1, spot2);
- else showBoard1(spot1, spot2);
- System.out.println("Match found!");
- hiddenBoard[spot1] = ' ';
- hiddenBoard[spot2] = ' ';
- guessedSpots.add(spot1+1);
- guessedSpots.add(spot2+1);
- Thread.sleep(4000);
- points += streak;
- streak++;
- }
- else
- {
- if(diff == 3)showBoard2(spot1, spot2);
- else showBoard1(spot1, spot2);
- System.out.println("Match not found!");
- lives--;
- Thread.sleep(4000);
- streak = 1;
- }
- for(int c = 0;c < 9999; c++)System.out.println();
- if((diff != 3 && guessedSpots.size() == 16) || diff == 3 && guessedSpots.size() == 36)win = true;
- if(win == true)break;
- }
- if(diff == 3)
- {
- if(win == true)
- {
- if(points >= 18 && points <= 50)System.out.println("This board is just right for you! Well done!");
- else System.out.println("You have exceptional memory! Well done!");
- }
- else
- {
- System.out.println("You ran out of lives! Keep practicing!");
- }
- }
- else
- {
- if(win == true)
- {
- System.out.println("Congrats! You found all the matches. Total points: " + points);
- }
- else
- {
- System.out.println("You ran out lives! Keep practing!");
- }
- if(points >= 8 && points <= 20)System.out.println("This board is just right for you! Well done!");
- else if(points == 36)System.out.println("You got a flawless game! Insane!");
- else System.out.println("Well done! This board seems too easy for you. Maybe try the next difficulty!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement