Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class MemoryGame {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> sequence = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
- String userInput = scanner.nextLine();
- int counter = 0;
- while (!userInput.equals("end")) {
- counter++;
- int[] userInputArray = Arrays.stream(userInput.split(" ")).mapToInt(Integer::parseInt).toArray();
- int firstIndex = userInputArray[0];
- int secondIndex = userInputArray[1];
- if (userIsCheating(firstIndex, secondIndex, sequence)) {
- System.out.println("Invalid input! Adding additional elements to the board");
- String penaltyNumber = String.format("-%da", counter);
- sequence.add(sequence.size()/2, penaltyNumber);
- sequence.add(sequence.size()/2, penaltyNumber);
- userInput = scanner.nextLine();
- //to ensure we have the next input before we move to the next While iteration (if that's the case
- // => the below userInput in line... will be skipped dute to the Continue statement
- continue;
- //if the user is cheating => we need to skip all the remaining operations
- // in this While iteration and "continue" to the next iteration of the While cycle
- }
- String firstNumber = sequence.get(firstIndex);
- String secondNumber = sequence.get(secondIndex);
- if (firstNumber.equals(secondNumber)) {
- sequence.remove(firstNumber);
- sequence.remove(secondNumber);
- //the remove method recognizes that we pass an Object as firstNumber & secondNumber are Objects
- // Achieve the same by using indexes:
- // if (firstIndex < secondIndex) {
- // sequence.remove(secondIndex);
- // sequence.remove(firstIndex);
- // }
- // else {
- // sequence.remove(firstIndex);
- // sequence.remove(secondIndex);
- // }
- System.out.printf("Congrats! You have found matching elements - %s!%n", firstNumber);
- }
- else {
- System.out.println("Try again!");
- }
- if (sequence.size() == 0) {
- break;
- }
- userInput = scanner.nextLine();
- }
- if (sequence.isEmpty()) {
- System.out.printf("You have won in %d turns!", counter);
- }
- else {
- System.out.println("Sorry you lose :(");
- System.out.println(String.join(" ", sequence));
- }
- }
- private static boolean userIsCheating(int firstIndex, int secondIndex, List<String> sequence) {
- if (firstIndex == secondIndex) {
- return true;
- }
- if (firstIndex <0 || firstIndex >= sequence.size()) {
- return true;
- }
- if(secondIndex < 0 || secondIndex >= sequence.size()){
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment