Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e743c6a

Browse files
Merge pull request avinashkranjan#2771 from andoriyaprashant/branch32
Codebreaker Game with AI Script Added
2 parents 5f2428a + e8c3c3a commit e743c6a

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

‎Codebreaker Game with AI/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Codebreaker Game with AI
2+
3+
Welcome to the Codebreaker Game with AI! This game allows you to challenge the computer's AI to crack a secret code that you create. You can also try to crack the AI's secret code.
4+
5+
## How to Play
6+
7+
1. Run the script using Python.
8+
2. The game will prompt you to input a secret code, which consists of a sequence of numbers between 1 and 6.
9+
3. The AI will then attempt to guess your secret code using logical deduction.
10+
4. The AI will provide feedback on each of its guesses.
11+
5. You will also have the chance to guess the AI's secret code by providing your own guesses and receiving feedback.
12+
6. The game ends when either you or the AI correctly guess the secret code or when the maximum number of attempts is reached.
13+
14+
## Features
15+
16+
- Player Interaction: You can enter your secret code and guess the AI's code.
17+
- AI Logic: The AI uses a brute-force algorithm to deduce the code.
18+
- Valid Input Handling: The game validates input to ensure correct format and range.
19+
- Limited Attempts: Both you and the AI have a limited number of attempts to crack the code.
20+
- Outcome Display: The game displays the outcome when the code is cracked or the game ends.
21+
22+
## Prerequisites
23+
24+
- Python
25+
26+
## How to Run
27+
28+
1. Open a terminal or command prompt.
29+
2. Navigate to the directory containing the script.
30+
3. Run the script using the following command:
31+
32+
```bash
33+
python codebreaker_game.py
34+
```
35+
36+
## Customize
37+
Feel free to modify the code and add your own features to enhance the game further!
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import random
2+
3+
def generate_code(length):
4+
return [random.randint(1, 6) for _ in range(length)]
5+
6+
def evaluate_guess(secret, guess):
7+
correct_positions = sum(1 for i in range(len(secret)) if secret[i] == guess[i])
8+
correct_numbers = len(set(secret) & set(guess)) - correct_positions
9+
return correct_positions, correct_numbers
10+
11+
def ai_brute_force(secret, code_length):
12+
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)]
14+
attempts = 0
15+
while True:
16+
guess = possible_codes.pop(0)
17+
attempts += 1
18+
print(f"AI guess #{attempts}: {guess}")
19+
correct_positions, correct_numbers = evaluate_guess(secret, guess)
20+
21+
if correct_positions == code_length:
22+
print(f"AI cracked the code in {attempts} attempts!")
23+
break
24+
25+
possible_codes = [code for code in possible_codes if evaluate_guess(code, guess) == (correct_positions, correct_numbers)]
26+
27+
def main():
28+
code_length = 4
29+
max_attempts = 10
30+
secret_code = generate_code(code_length)
31+
32+
print("Welcome to the Codebreaker Game!")
33+
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+
36+
player_code = []
37+
for attempt in range(1, max_attempts + 1):
38+
while True:
39+
try:
40+
player_code = [int(num) for num in input(f"Attempt #{attempt}: Enter your code (space-separated): ").split()]
41+
if len(player_code) != code_length or any(num < 1 or num > 6 for num in player_code):
42+
raise ValueError
43+
break
44+
except ValueError:
45+
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+
50+
if correct_positions == code_length:
51+
print(f"Congratulations! You cracked the code in {attempt} attempts!")
52+
break
53+
else:
54+
print(f"Sorry, you couldn't crack the code. The AI's secret code was: {secret_code}")
55+
56+
if __name__ == "__main__":
57+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /