dakata450

Untitled

Dec 31st, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. package midExam;
  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 MemoryGame {
  9. public static void main(String[] args) {
  10.  
  11.  
  12. Scanner scanner = new Scanner(System.in);
  13.  
  14. List<String> list = Arrays.stream(scanner.nextLine().
  15. split(" ")).collect(Collectors.toList());
  16. String comand = scanner.nextLine();
  17. int count = 0;
  18. boolean isEnd = true;
  19. boolean ishit = false;
  20.  
  21. while (!comand.equals("end")){
  22. if (comand.contains("end")){
  23. isEnd = false;
  24. break;
  25.  
  26. }
  27. count ++;
  28. String[] comandList = comand.split(" ");
  29. int firstIndex = Integer.parseInt(comandList[0]);
  30. int seconfIndex = Integer.parseInt(comandList[1]);
  31. if (list.size() == 0){
  32. ishit = true;
  33. break;
  34.  
  35. }
  36. if (firstIndex >= 0 && seconfIndex >= 0 && firstIndex < list.size() && seconfIndex < list.size()){
  37. String first = list.get(firstIndex);
  38. String second = list.get(seconfIndex);
  39. if (first.equals(second)){
  40. System.out.printf("Congrats! You have found matching elements - %s!%n", first);
  41. list.remove(first);
  42. list.remove(second);
  43. }else {
  44. System.out.println("Try again!");
  45. }
  46. }else{
  47. System.out.println("Invalid input! Adding additional elements to the board");
  48. int midleofList = list.size() / 2;
  49. list.add(midleofList, "-" + count + "a");
  50. list.add(midleofList, "-" + count + "a");
  51. }
  52.  
  53. comand = scanner.nextLine();
  54. }
  55. if (ishit){
  56. System.out.printf("You have won in %d turns!", count - 1);
  57. return;
  58. }
  59.  
  60. if (isEnd){
  61. System.out.println("Sorry you lose :(");
  62. for (String item : list) {
  63. System.out.print(item + " ");
  64. }
  65. }
  66. }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment