Advertisement
veronikaaa86

03. Memory Game

Feb 13th, 2023
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package midExamPrep;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class P03MemoryGame {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<String> elements = Arrays
  13.                 .stream(scanner.nextLine().split("\\s+"))
  14.                 .collect(Collectors.toList());
  15.  
  16.         String input = scanner.nextLine();
  17.         int count = 0;
  18.         while (!input.equals("end")) {
  19.             count++;
  20.             String[] tokens = input.split("\\s+");
  21.             int firstIndex = Integer.parseInt(tokens[0]);
  22.             int secondIndex = Integer.parseInt(tokens[1]);
  23.  
  24.             if (cheat(firstIndex, secondIndex, elements)) {
  25.                 System.out.println("Invalid input! Adding additional elements to the board");
  26.  
  27.                 elements.add(elements.size() / 2, String.format("-%da", count));
  28.                 elements.add(elements.size() / 2, String.format("-%da", count));
  29.             } else {
  30.                 String firstElement = elements.get(firstIndex);
  31.                 String secondElement = elements.get(secondIndex);
  32.                 if (firstElement.equals(secondElement)) {
  33.                     System.out.printf("Congrats! You have found matching elements - %s!%n", firstElement);
  34.                     elements.remove(firstElement);
  35.                     elements.remove(secondElement);
  36.                 } else {
  37.                     System.out.println("Try again!");
  38.                 }
  39.             }
  40.  
  41.             if (elements.isEmpty()) {
  42.                 break;
  43.             }
  44.  
  45.             input = scanner.nextLine();
  46.         }
  47.  
  48.         if (elements.isEmpty()) {
  49.             System.out.printf("You have won in %d turns!%n", count);
  50.         } else {
  51.             System.out.println("Sorry you lose :(");
  52.             System.out.println(String.join(" ", elements));
  53.         }
  54.     }
  55.  
  56.     public static boolean cheat(int firstIndex, int secondIndex, List<String> elements) {
  57.         if (firstIndex == secondIndex || isInvalidIndex(firstIndex, elements) || isInvalidIndex(secondIndex, elements)) {
  58.             return true;
  59.         }
  60.         return false;
  61.     }
  62.  
  63.     public static boolean isInvalidIndex(int index, List<String> elements) {
  64.         if (index < 0 || index >= elements.size()) {
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement