# I asked ChatGPT to write code for a Crack the Code game.

Last Updated or created 2023-03-29

The Game

```elcome to Crack the Code!
The code has 4 digits. Each digit is between 0 and 9.
You have 10 tries to guess the code.
After each guess, you will be told how many digits you got correct and how many were misplaced.
Good luck!
Guess 1: 1234 - 0 correct, 0 misplaced
Guess 2: 5678 - 2 correct, 1 misplaced
Guess 3: 5689 - 1 correct, 2 misplaced
Guess 4: 9688 - 1 correct, 2 misplaced
....
Sorry, you did not crack the code. The code was 5568.

```

Generated python code

```import random

def generate_code():
code = ""
for i in range(4):
code += str(random.randint(0, 9))
return code

def check_guess(code, guess):
correct_digits = 0
misplaced_digits = 0
for i in range(4):
if guess[i] == code[i]:
correct_digits += 1
elif guess[i] in code:
misplaced_digits += 1
return (correct_digits, misplaced_digits)

def play_game():
print("Welcome to Crack the Code!")
print("The code has 4 digits. Each digit is between 0 and 9.")
print("You have 10 tries to guess the code.")
print("After each guess, you will be told how many digits you got correct and how many were misplaced.")
print("Good luck!")
code = generate_code()
num_guesses = 0
while num_guesses < 10:
guess = input("Enter your guess: ")
if len(guess) != 4 or not guess.isdigit():
print("Invalid input. Please enter a 4-digit number.")
continue
num_guesses += 1
correct, misplaced = check_guess(code, guess)
print(f"Guess {num_guesses}: {guess} - {correct} correct, {misplaced} misplaced")
if correct == 4:
print("Congratulations! You cracked the code!")
return
print(f"Sorry, you did not crack the code. The code was {code}.")

play_game()
```

After this i let the AI made some changes, which kinda worked.
Scary nevertheless