Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2021
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package com.company;
  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 _03MemoryGame {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<String> sequence = Arrays.stream(scanner.nextLine().split(" " ))
  13.                 .collect(Collectors.toList());
  14.  
  15.         int moves = 0;
  16.         String line = scanner.nextLine();
  17.         while (!line.equals("end" )) {
  18.             moves++;
  19.             String[] indexes = line.split(" " );
  20.             int firstIndex = Integer.parseInt(indexes[0]);
  21.             int secondIndex = Integer.parseInt(indexes[1]);
  22.  
  23.  
  24.             if (firstIndex == secondIndex || firstIndex < 0 || firstIndex >= sequence.size()
  25.                     || secondIndex < 0 || secondIndex >= sequence.size()) {
  26.  
  27.                 int middleIndex = sequence.size() / 2;
  28.  
  29.                 sequence.add(middleIndex, "-" + moves + "a" );
  30.                 sequence.add(middleIndex, "-" + moves + "a" );
  31.                 System.out.println("Invalid input! Adding additional elements to the board" );
  32.             } else if (sequence.get(firstIndex).equals(sequence.get(secondIndex))) {
  33.                 String indexValue = sequence.get(firstIndex);
  34.                 if (firstIndex > secondIndex) {
  35.                     sequence.remove(firstIndex);
  36.                     sequence.remove(secondIndex);
  37.                 } else {
  38.                     sequence.remove(secondIndex);
  39.                     sequence.remove(firstIndex);
  40.                 }
  41.                 System.out.printf("Congrats! You have found matching elements - %s!\n", indexValue);
  42.             } else if (!sequence.get(firstIndex).equals(sequence.get(secondIndex))) {
  43.                 System.out.println("Try again!" );
  44.             }
  45.             if (sequence.isEmpty()) {
  46.                 System.out.printf("You have won in %d turns!", moves);
  47.                 return;
  48.             }
  49.  
  50.             line = scanner.nextLine();
  51.         }
  52.  
  53.         System.out.println("Sorry you lose :(" );
  54.         for (String string : sequence) {
  55.             System.out.print(string + " " );
  56.         }
  57.  
  58.  
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement