RGAlex

O3. Memory Game / O1. Mid Exam Retake

Feb 18th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package MidExam.O1;
  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 O3MemoryGame {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<String> testList = Arrays.stream(scanner.nextLine().split("\\s+")).
  13.                 map(String::toString).collect(Collectors.toList());
  14.  
  15.         String input = scanner.nextLine();
  16.         int counters = 0;
  17.         while (!input.equals("end")) {
  18.             if (testList.size() == 0) {
  19.                 break;
  20.             }
  21.             List<String> command = Arrays.asList(input.split("\\s+"));
  22.             int a = Integer.parseInt(command.get(0));
  23.             int b = Integer.parseInt(command.get(1));
  24.             counters += 1;
  25.             if (Math.min(a, b) < 0 || a == b || Math.max(a, b) >= testList.size()) {
  26.  
  27.                 int center = testList.size() / 2;
  28.                 testList.add(center, "-" + counters + "a");
  29.                 testList.add(center, "-" + counters + "a");
  30.                 System.out.println("Invalid input! Adding additional elements to the board");
  31.  
  32.             } else if (testList.get(a).equals(testList.get(b))) {
  33.  
  34.                 String element = testList.get(a);
  35.                 testList.remove(Math.min(a, b));
  36.                 testList.remove(Math.max(a, b) - 1);
  37.                 System.out.printf("Congrats! You have found matching elements - %s!%n", element);
  38.  
  39.             } else {
  40.                 System.out.println("Try again!");
  41.             }
  42.             input = scanner.nextLine();
  43.         }
  44.         if (testList.size() == 0) {
  45.             System.out.printf("You have won in %d turns!", counters);
  46.         } else {
  47.             System.out.println("Sorry you lose :(");
  48.             for (String s : testList) {
  49.                 System.out.print(s + " ");
  50.             }
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment