Advertisement
go6odn28

memory_game_my

Feb 17th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. def is_valid_index(lst, idx):
  2.     return 0 <= idx < len(lst)
  3.  
  4.  
  5. def memory_game():
  6.     elements = input().split()
  7.     moves = 0
  8.     while True:
  9.         command = input()
  10.         if command == "end":
  11.             break
  12.  
  13.         indices_data = command.split()
  14.         index_1, index_2 = int(indices_data[0]), int(indices_data[1])
  15.         moves += 1
  16.         if is_valid_index(elements, index_1) and is_valid_index(elements, index_2) and index_1 != index_2:
  17.             if elements[index_1] == elements[index_2]:
  18.                 matching_element = elements[index_1]
  19.                 while matching_element in elements:
  20.                     elements.remove(matching_element)
  21.                 print(f"Congrats! You have found matching elements - {matching_element}!")
  22.  
  23.             else:
  24.                 print("Try again!")
  25.  
  26.         else:
  27.             print("Invalid input! Adding additional elements to the board")
  28.             index_middle_of_the_list = len(elements) // 2
  29.             elements.insert(index_middle_of_the_list, f"-{moves}a")
  30.             elements.insert(index_middle_of_the_list + 1, f"-{moves}a")
  31.  
  32.         if not elements:
  33.             print(f"You have won in {moves} turns!")
  34.             break
  35.  
  36.     if elements:
  37.         print("Sorry you lose :(")
  38.         print(*elements)
  39.  
  40.  
  41. memory_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement