|
1 | 1 | import random |
2 | 2 |
|
| 3 | + |
3 | 4 | def generate_code(length): |
4 | 5 | return [random.randint(1, 6) for _ in range(length)] |
5 | 6 |
|
| 7 | + |
6 | 8 | def evaluate_guess(secret, guess): |
7 | | - correct_positions = sum(1 for i in range(len(secret)) if secret[i] == guess[i]) |
| 9 | + correct_positions = sum(1 for i in range( |
| 10 | + len(secret)) if secret[i] == guess[i]) |
8 | 11 | correct_numbers = len(set(secret) & set(guess)) - correct_positions |
9 | 12 | return correct_positions, correct_numbers |
10 | 13 |
|
| 14 | + |
11 | 15 | def ai_brute_force(secret, code_length): |
12 | 16 | possible_codes = [[i, j, k, l] for i in range(1, 7) for j in range(1, 7) |
13 | | - for k in range(1, 7) for l in range(1, 7)] |
| 17 | + for k in range(1, 7) for l in range(1, 7)] |
14 | 18 | attempts = 0 |
15 | 19 | while True: |
16 | 20 | guess = possible_codes.pop(0) |
17 | 21 | attempts += 1 |
18 | 22 | print(f"AI guess #{attempts}: {guess}") |
19 | 23 | correct_positions, correct_numbers = evaluate_guess(secret, guess) |
20 | | - |
| 24 | + |
21 | 25 | if correct_positions == code_length: |
22 | 26 | print(f"AI cracked the code in {attempts} attempts!") |
23 | 27 | break |
24 | | - |
25 | | - possible_codes = [code for code in possible_codes if evaluate_guess(code, guess) == (correct_positions, correct_numbers)] |
| 28 | + |
| 29 | + possible_codes = [code for code in possible_codes if evaluate_guess( |
| 30 | + code, guess) == (correct_positions, correct_numbers)] |
| 31 | + |
26 | 32 |
|
27 | 33 | def main(): |
28 | 34 | code_length = 4 |
29 | 35 | max_attempts = 10 |
30 | 36 | secret_code = generate_code(code_length) |
31 | | - |
| 37 | + |
32 | 38 | print("Welcome to the Codebreaker Game!") |
33 | 39 | print("Try to guess the AI's secret code.") |
34 | | - print(f"The secret code consists of {code_length} numbers between 1 and 6.") |
35 | | - |
| 40 | + print( |
| 41 | + f"The secret code consists of {code_length} numbers between 1 and 6.") |
| 42 | + |
36 | 43 | player_code = [] |
37 | 44 | for attempt in range(1, max_attempts + 1): |
38 | 45 | while True: |
39 | 46 | try: |
40 | | - player_code = [int(num) for num in input(f"Attempt #{attempt}: Enter your code (space-separated): ").split()] |
| 47 | + player_code = [int(num) for num in input( |
| 48 | + f"Attempt #{attempt}: Enter your code (space-separated): ").split()] |
41 | 49 | if len(player_code) != code_length or any(num < 1 or num > 6 for num in player_code): |
42 | 50 | raise ValueError |
43 | 51 | break |
44 | 52 | except ValueError: |
45 | 53 | print("Invalid input. Enter a valid code.") |
46 | | - |
47 | | - correct_positions, correct_numbers = evaluate_guess(secret_code, player_code) |
48 | | - print(f"Result: {correct_positions} in correct position, {correct_numbers} correct numbers but in wrong position.") |
49 | | - |
| 54 | + |
| 55 | + correct_positions, correct_numbers = evaluate_guess( |
| 56 | + secret_code, player_code) |
| 57 | + print( |
| 58 | + f"Result: {correct_positions} in correct position, {correct_numbers} correct numbers but in wrong position.") |
| 59 | + |
50 | 60 | if correct_positions == code_length: |
51 | | - print(f"Congratulations! You cracked the code in {attempt} attempts!") |
| 61 | + print( |
| 62 | + f"Congratulations! You cracked the code in {attempt} attempts!") |
52 | 63 | break |
53 | 64 | else: |
54 | | - print(f"Sorry, you couldn't crack the code. The AI's secret code was: {secret_code}") |
| 65 | + print( |
| 66 | + f"Sorry, you couldn't crack the code. The AI's secret code was: {secret_code}") |
| 67 | + |
55 | 68 |
|
56 | 69 | if __name__ == "__main__": |
57 | 70 | main() |
0 commit comments