Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package midExam;
- 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> list = Arrays.stream(scanner.nextLine().
- split(" ")).collect(Collectors.toList());
- String comand = scanner.nextLine();
- int count = 0;
- boolean isEnd = true;
- boolean ishit = false;
- while (!comand.equals("end")){
- if (comand.contains("end")){
- isEnd = false;
- break;
- }
- count ++;
- String[] comandList = comand.split(" ");
- int firstIndex = Integer.parseInt(comandList[0]);
- int seconfIndex = Integer.parseInt(comandList[1]);
- if (list.size() == 0){
- ishit = true;
- break;
- }
- if (firstIndex >= 0 && seconfIndex >= 0 && firstIndex < list.size() && seconfIndex < list.size()){
- String first = list.get(firstIndex);
- String second = list.get(seconfIndex);
- if (first.equals(second)){
- System.out.printf("Congrats! You have found matching elements - %s!%n", first);
- list.remove(first);
- list.remove(second);
- }else {
- System.out.println("Try again!");
- }
- }else{
- System.out.println("Invalid input! Adding additional elements to the board");
- int midleofList = list.size() / 2;
- list.add(midleofList, "-" + count + "a");
- list.add(midleofList, "-" + count + "a");
- }
- comand = scanner.nextLine();
- }
- if (ishit){
- System.out.printf("You have won in %d turns!", count - 1);
- return;
- }
- if (isEnd){
- System.out.println("Sorry you lose :(");
- for (String item : list) {
- System.out.print(item + " ");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment