SvetlanPetrova

MemoryGame SoftUni

Jul 8th, 2021
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class MemoryGame {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         List<String> sequence = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  10.         String userInput = scanner.nextLine();
  11.         int counter = 0;
  12.  
  13.         while (!userInput.equals("end")) {
  14.             counter++;
  15.             int[] userInputArray = Arrays.stream(userInput.split(" ")).mapToInt(Integer::parseInt).toArray();
  16.  
  17.             int firstIndex = userInputArray[0];
  18.             int secondIndex = userInputArray[1];
  19.  
  20.             if (userIsCheating(firstIndex, secondIndex, sequence)) {
  21.                 System.out.println("Invalid input! Adding additional elements to the board");
  22.                 String penaltyNumber = String.format("-%da", counter);
  23.                 sequence.add(sequence.size()/2, penaltyNumber);
  24.                 sequence.add(sequence.size()/2, penaltyNumber);
  25.                 userInput = scanner.nextLine();
  26.                 //to ensure we have the next input before we move to the next While iteration (if that's the case
  27.                 // => the below userInput in line... will be skipped dute to the Continue statement
  28.                 continue;
  29.                 //if the user is cheating => we need to skip all the remaining operations
  30.                 // in this While iteration and "continue" to the next iteration of the While cycle
  31.             }
  32.  
  33.             String firstNumber = sequence.get(firstIndex);
  34.             String secondNumber = sequence.get(secondIndex);
  35.  
  36.             if (firstNumber.equals(secondNumber)) {
  37.                 sequence.remove(firstNumber);
  38.                 sequence.remove(secondNumber);
  39. //the remove method recognizes that we pass an Object as firstNumber & secondNumber are Objects
  40. //    Achieve the same by using indexes:
  41. //                if (firstIndex < secondIndex) {
  42. //                    sequence.remove(secondIndex);
  43. //                    sequence.remove(firstIndex);
  44. //                }
  45. //                else {
  46. //                    sequence.remove(firstIndex);
  47. //                    sequence.remove(secondIndex);
  48. //                }
  49.                 System.out.printf("Congrats! You have found matching elements - %s!%n", firstNumber);
  50.             }
  51.             else {
  52.                 System.out.println("Try again!");
  53.             }
  54.  
  55.             if (sequence.size() == 0) {
  56.                 break;
  57.             }
  58.  
  59.             userInput = scanner.nextLine();
  60.         }
  61.  
  62.         if (sequence.isEmpty()) {
  63.             System.out.printf("You have won in %d turns!", counter);
  64.         }
  65.         else {
  66.             System.out.println("Sorry you lose :(");
  67.             System.out.println(String.join(" ", sequence));
  68.         }
  69.  
  70.     }
  71.  
  72.     private static boolean userIsCheating(int firstIndex, int secondIndex, List<String> sequence) {
  73.         if (firstIndex == secondIndex) {
  74.             return  true;
  75.         }
  76.         if (firstIndex <0 || firstIndex >= sequence.size()) {
  77.             return  true;
  78.         }
  79.         if(secondIndex < 0 || secondIndex >= sequence.size()){
  80.             return true;
  81.         }
  82.  
  83.         return false;
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment