Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExam.O1;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class O3MemoryGame {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> testList = Arrays.stream(scanner.nextLine().split("\\s+")).
- map(String::toString).collect(Collectors.toList());
- String input = scanner.nextLine();
- int counters = 0;
- while (!input.equals("end")) {
- if (testList.size() == 0) {
- break;
- }
- List<String> command = Arrays.asList(input.split("\\s+"));
- int a = Integer.parseInt(command.get(0));
- int b = Integer.parseInt(command.get(1));
- counters += 1;
- if (Math.min(a, b) < 0 || a == b || Math.max(a, b) >= testList.size()) {
- int center = testList.size() / 2;
- testList.add(center, "-" + counters + "a");
- testList.add(center, "-" + counters + "a");
- System.out.println("Invalid input! Adding additional elements to the board");
- } else if (testList.get(a).equals(testList.get(b))) {
- String element = testList.get(a);
- testList.remove(Math.min(a, b));
- testList.remove(Math.max(a, b) - 1);
- System.out.printf("Congrats! You have found matching elements - %s!%n", element);
- } else {
- System.out.println("Try again!");
- }
- input = scanner.nextLine();
- }
- if (testList.size() == 0) {
- System.out.printf("You have won in %d turns!", counters);
- } else {
- System.out.println("Sorry you lose :(");
- for (String s : testList) {
- System.out.print(s + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment