Advertisement
KeeganT

Mancala

Jun 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.98 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 String alphabet="ABCDEFGHIJKLMNOPQR";
  9.     static char[] alphArray = alphabet.toCharArray();
  10.     static ArrayList<Character> hiddenBoard2 = new ArrayList();
  11.     static ArrayList<Character> board2 = new ArrayList();
  12.     static ArrayList<Character> matches2 = new ArrayList();
  13.     static char[] hiddenBoard;
  14.     static char[] board;
  15.     static char[] matches;
  16.    
  17.     public static void generateArrays(int boardChoice)
  18.     {
  19.         if(boardChoice == 1)
  20.         {
  21.             for(int c = 0; c < 16; c++)
  22.             {
  23.                 hiddenBoard2.add('0');
  24.                 board2.add(' ');
  25.             }
  26.            
  27.             for(int c = 0; c < 8; c++)
  28.             {
  29.                 matches2.add(alphArray[c]);
  30.                 matches2.add(alphArray[c]);
  31.             }
  32.         }
  33.        
  34.         if(boardChoice == 2)
  35.         {
  36.             for(int c = 0; c < 36; c++)
  37.             {
  38.                 hiddenBoard2.add('0');
  39.                 board2.add(' ');
  40.             }
  41.            
  42.             for(int c = 0; c < 18; c++)
  43.             {
  44.                 matches2.add(alphArray[c]);
  45.                 matches2.add(alphArray[c]);
  46.             }
  47.         }
  48.         String str = hiddenBoard2.toString().replaceAll(",", "");
  49.         hiddenBoard = str.substring(1, str.length()-1).replaceAll(" ", "").toCharArray();
  50.         str = board2.toString().replaceAll(",","");
  51.         board = str.substring(1, str.length()-1).toCharArray();
  52.         str = matches2.toString().replaceAll(",", "");
  53.         matches = str.substring(1, str.length()-1).replaceAll(" ", "").toCharArray();
  54.     }
  55.    
  56.     public static void generateBoard1()
  57.     {
  58.         Random random = new Random();
  59.         for(int c = 0; c < 16; c++)
  60.         {
  61.             int spot = random.nextInt(16);
  62.             while(board[spot] != ' ')
  63.             {
  64.                 spot = random.nextInt(16);
  65.             }
  66.             board[spot] = matches[c];
  67.         }
  68.     }
  69.    
  70.     public static void generateBoard2()
  71.     {
  72.         Random random = new Random();
  73.         for(int c = 0; c < 36; c++)
  74.         {
  75.             int spot = random.nextInt(36);
  76.             while(board[spot] != ' ')
  77.             {
  78.                 spot = random.nextInt(36);
  79.             }
  80.             board[spot] = matches[c];
  81.         }
  82.     }
  83.    
  84.     public static void showBoard1(int spot1, int spot2)
  85.     {
  86.         for(int c = 0; c < 16; c++)
  87.         {
  88.             if((c-4) % 4 == 0)System.out.println("-----------------");
  89.             if(c % 4 == 0)System.out.print("| ");
  90.             if(c == spot1 || c == spot2)System.out.print(board[c] + " | ");
  91.             else System.out.print(hiddenBoard[c] + " | ");
  92.             if((c+1) % 4 == 0)System.out.println();
  93.         }
  94.         System.out.println("-----------------");
  95.     }
  96.    
  97.    public static void showBoard2(int spot1, int spot2)
  98.     {
  99.         for(int c = 0; c < 36; c++)
  100.         {
  101.             if((c-6) % 6 == 0)System.out.println("-------------------------");
  102.             if(c % 6 == 0)System.out.print("| ");
  103.             if(c == spot1 || c == spot2)System.out.print(board[c] + " | ");
  104.             else System.out.print(hiddenBoard[c] + " | ");
  105.             if((c+1) % 6 == 0)System.out.println();
  106.         }
  107.         System.out.println("-------------------------");
  108.     }
  109.    
  110.     public static void main(String[] args) throws InterruptedException
  111.     {
  112.         Scanner input = new Scanner(System.in);
  113.        
  114.         boolean win = false;
  115.         ArrayList<Integer> guessedSpots = new ArrayList();
  116.        
  117.         System.out.println("Welcome to match game!");
  118.         System.out.println("Select difficult:\n1) Easy\n2) Medium\n3) Hard");
  119.         int diff = input.nextInt();
  120.         if(diff == 3)
  121.         {
  122.             System.out.println("The goal of this game is to find all 18 matches (A - R)");
  123.             System.out.println("Pick 2 spots on the board (1 - 36) and try to find the hidden matches.");
  124.         }
  125.         else
  126.         {
  127.             System.out.println("The goal of this game is to find all 8 matches (A - H)");
  128.             System.out.println("Pick 2 spots on the board (1 - 16) and try to find the hidden matches.");
  129.         }
  130.         int lives = 10, points = 0, streak = 1;
  131.         if(diff == 1)
  132.         {
  133.             generateArrays(1);
  134.             generateBoard1();
  135.         }
  136.         else if(diff == 2)
  137.         {
  138.             generateArrays(1);
  139.             generateBoard1();
  140.             lives = 8;
  141.         }
  142.         else if(diff == 3)
  143.         {
  144.             generateArrays(2);
  145.             generateBoard2();
  146.         }
  147.         System.out.println("Careful! You only have " + lives + " lives.");
  148.        
  149.         while(lives > 0)
  150.         {
  151.             if(diff == 3)showBoard2(-1, -1);
  152.             else showBoard1(-1, -1);
  153.             System.out.println("Lives left: " + lives);
  154.             System.out.println("Pick spot 1: ");
  155.             int spot1 = input.nextInt();
  156.             while(guessedSpots.contains(spot1))
  157.             {
  158.                 System.out.println("You already guessed that spot! Guess another.");
  159.                 spot1 = input.nextInt();
  160.             }
  161.             if(diff == 3)
  162.             {
  163.                 while(spot1 > 36 || spot1 < 1)
  164.                 {
  165.                     System.out.println("That spot does not exist! Guess another.");
  166.                     spot1 = input.nextInt();
  167.                 }
  168.             }
  169.             else
  170.             {
  171.                 while(spot1 > 16 || spot1 < 1)
  172.                 {
  173.                     System.out.println("That spot does not exist! Guess another.");
  174.                     spot1 = input.nextInt();
  175.                 }
  176.             }
  177.             spot1--;
  178.             if(diff == 3)showBoard2(spot1, -1);
  179.             else showBoard1(spot1, -1);
  180.             System.out.println("Pick spot 2: ");
  181.             int spot2 = input.nextInt();
  182.             while(spot2 == spot1 + 1)
  183.             {
  184.                 System.out.println("You can't pick the same spot! Guess another.");
  185.                 spot2 = input.nextInt();
  186.             }
  187.             while(guessedSpots.contains(spot2))
  188.             {
  189.                 System.out.println("You already guessed that spot! Guess another.");
  190.                 spot2 = input.nextInt();
  191.             }
  192.             if(diff == 3)
  193.             {
  194.                 while(spot2 > 36 || spot2 < 1)
  195.                 {
  196.                     System.out.println("That spot does not exist! Guess another.");
  197.                     spot2 = input.nextInt();
  198.                 }
  199.             }
  200.             else
  201.             {
  202.                 while(spot2 > 16 || spot2 < 1)
  203.                 {
  204.                     System.out.println("That spot does not exist! Guess another.");
  205.                     spot2 = input.nextInt();
  206.                 }
  207.             }
  208.             spot2--;
  209.             if(board[spot1] == board[spot2])
  210.             {
  211.                 if(diff == 3)showBoard2(spot1, spot2);
  212.                 else showBoard1(spot1, spot2);
  213.                 System.out.println("Match found!");
  214.                 hiddenBoard[spot1] = ' ';
  215.                 hiddenBoard[spot2] = ' ';
  216.                 guessedSpots.add(spot1+1);
  217.                 guessedSpots.add(spot2+1);
  218.                 Thread.sleep(4000);
  219.                 points += streak;
  220.                 streak++;
  221.             }
  222.             else
  223.             {
  224.                 if(diff == 3)showBoard2(spot1, spot2);
  225.                 else showBoard1(spot1, spot2);
  226.                 System.out.println("Match not found!");
  227.                 lives--;
  228.                 Thread.sleep(4000);
  229.                 streak = 1;
  230.             }
  231.             for(int c = 0;c < 9999; c++)System.out.println();
  232.             if((diff != 3 && guessedSpots.size() == 16) || diff == 3 && guessedSpots.size() == 36)win = true;
  233.             if(win == true)break;
  234.         }
  235.         if(diff == 3)
  236.         {
  237.             if(win == true)
  238.             {
  239.                 if(points >= 18 && points <= 50)System.out.println("This board is just right for you! Well done!");
  240.                 else System.out.println("You have exceptional memory! Well done!");
  241.             }
  242.            
  243.             else
  244.             {
  245.                 System.out.println("You ran out of lives! Keep practicing!");
  246.             }
  247.         }
  248.        
  249.         else
  250.         {
  251.             if(win == true)
  252.             {
  253.                 System.out.println("Congrats! You found all the matches. Total points: " + points);
  254.             }
  255.  
  256.             else
  257.             {
  258.                 System.out.println("You ran out lives! Keep practing!");
  259.             }
  260.            
  261.             if(points >= 8 && points <= 20)System.out.println("This board is just right for you! Well done!");
  262.             else if(points == 36)System.out.println("You got a flawless game! Insane!");
  263.             else System.out.println("Well done! This board seems too easy for you. Maybe try the next difficulty!");
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement