Skip to content

Commit 3d90e8c

Browse files
style: format code with autopep8
Format code with autopep8 This commit fixes the style issues introduced in e743c6a according to the output from Autopep8. Details: None
1 parent c72bd53 commit 3d90e8c

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,70 @@
11
import random
22

3+
34
def generate_code(length):
45
return [random.randint(1, 6) for _ in range(length)]
56

7+
68
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])
811
correct_numbers = len(set(secret) & set(guess)) - correct_positions
912
return correct_positions, correct_numbers
1013

14+
1115
def ai_brute_force(secret, code_length):
1216
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)]
1418
attempts = 0
1519
while True:
1620
guess = possible_codes.pop(0)
1721
attempts += 1
1822
print(f"AI guess #{attempts}: {guess}")
1923
correct_positions, correct_numbers = evaluate_guess(secret, guess)
20-
24+
2125
if correct_positions == code_length:
2226
print(f"AI cracked the code in {attempts} attempts!")
2327
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+
2632

2733
def main():
2834
code_length = 4
2935
max_attempts = 10
3036
secret_code = generate_code(code_length)
31-
37+
3238
print("Welcome to the Codebreaker Game!")
3339
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+
3643
player_code = []
3744
for attempt in range(1, max_attempts + 1):
3845
while True:
3946
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()]
4149
if len(player_code) != code_length or any(num < 1 or num > 6 for num in player_code):
4250
raise ValueError
4351
break
4452
except ValueError:
4553
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+
5060
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!")
5263
break
5364
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+
5568

5669
if __name__ == "__main__":
5770
main()

0 commit comments

Comments
 (0)