|
| 1 | +import random |
| 2 | +import time |
| 3 | + |
| 4 | +def generate_pattern(): |
| 5 | + colors = ["red", "green", "blue", "yellow", "orange", "purple", "pink", "brown", "gray", "black", "white", "cyan", "magenta"] |
| 6 | + patterns = [ |
| 7 | + ["red", "green", "blue", "yellow", "orange"], |
| 8 | + ["purple", "pink", "brown", "gray", "black"], |
| 9 | + ["white", "cyan", "magenta", "red", "blue", "green"], |
| 10 | + ["brown", "gray", "pink", "red", "blue"], |
| 11 | + ["yellow", "orange", "red", "purple"], |
| 12 | + ["white", "gray", "black", "pink", "cyan", "blue"], |
| 13 | + ["yellow", "green", "orange", "blue"], |
| 14 | + ["magenta", "purple", "green", "cyan", "yellow"], |
| 15 | + ["red", "green", "blue", "yellow", "orange", "purple", "pink"], |
| 16 | + ["red", "blue", "orange", "yellow", "cyan", "magenta"], |
| 17 | + ["blue", "green", "yellow", "orange", "brown"], |
| 18 | + ["cyan", "magenta", "yellow", "gray", "purple"], |
| 19 | + ["red", "green", "blue", "yellow", "orange", "purple", "pink", "cyan"], |
| 20 | + ["blue", "red", "green", "yellow", "orange", "purple", "brown", "cyan"], |
| 21 | + ["magenta", "purple", "pink", "yellow", "orange", "green", "red", "gray"] |
| 22 | + ] |
| 23 | + pattern = random.choice(patterns) |
| 24 | + return pattern |
| 25 | + |
| 26 | +def get_next_pattern(pattern): |
| 27 | + colors = set(["red", "green", "blue", "yellow", "orange", "purple", "pink", "brown", "gray", "black", "white", "cyan", "magenta"]) |
| 28 | + pattern_set = set(pattern) |
| 29 | + missing_color = list(colors - pattern_set)[0] |
| 30 | + next_pattern = pattern[1:] + [missing_color] |
| 31 | + return next_pattern |
| 32 | + |
| 33 | +def play_game(): |
| 34 | + print("Welcome to the Pattern Recognition Game!") |
| 35 | + print("Try to identify the underlying rule of the color patterns.") |
| 36 | + print("For example, if the pattern is ['red', 'green', 'blue', 'yellow', 'orange'], the next pattern will be ['green', 'blue', 'yellow', 'orange', 'purple'] (alphabetical order).\n") |
| 37 | + |
| 38 | + score = 0 |
| 39 | + max_attempts = 3 |
| 40 | + hint_limit = 1 |
| 41 | + time_limit = 20 |
| 42 | + |
| 43 | + while True: |
| 44 | + pattern = generate_pattern() |
| 45 | + print("Pattern:", pattern) |
| 46 | + |
| 47 | + for attempt in range(max_attempts): |
| 48 | + user_input = input("Enter the next pattern: ").split() |
| 49 | + |
| 50 | + if user_input == get_next_pattern(pattern): |
| 51 | + score += 1 |
| 52 | + print("Correct!") |
| 53 | + break |
| 54 | + else: |
| 55 | + print(f"Wrong! {max_attempts - attempt - 1} attempts remaining.") |
| 56 | + |
| 57 | + else: |
| 58 | + print(f"\nGame Over! Your score: {score}\n") |
| 59 | + break |
| 60 | + |
| 61 | + # Give the player a hint option |
| 62 | + if hint_limit > 0: |
| 63 | + hint_choice = input("Would you like a hint? (yes/no): ").lower() |
| 64 | + if hint_choice == "yes": |
| 65 | + hint_limit -= 1 |
| 66 | + print("Hint: The next color is", get_next_pattern(pattern)[-1]) |
| 67 | + |
| 68 | + # Introduce levels based on the score |
| 69 | + if score >= 5: |
| 70 | + max_attempts = 2 |
| 71 | + if score >= 10: |
| 72 | + time_limit = 15 |
| 73 | + |
| 74 | + # Timer implementation |
| 75 | + start_time = time.time() |
| 76 | + while time.time() - start_time < time_limit: |
| 77 | + pass |
| 78 | + print("\nTime's up!\n") |
| 79 | + print(f"Your score: {score}\n") |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + play_game() |
0 commit comments