-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
185 lines (143 loc) · 6.44 KB
/
Copy pathmain.py
File metadata and controls
185 lines (143 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from collections import deque, Counter
import random
import time
# Define choices directly
choices = ["rock", "r", "paper", "p", "scissors", "s"]
user_moves = {"rock": 0, "paper": 0, "scissors": 0}
total_rounds = 0
ai_wins = 0
recent_moves = deque(maxlen=5)
history = []
def get_user_choice(user_input):
user_input = user_input.lower()
if user_input in choices:
return user_input
else:
print("Invalid choice.")
return None
def update_user_moves(user_choice):
if user_choice in user_moves:
user_moves[user_choice] += 1
recent_moves.append(user_choice)
history.append(user_choice) # Track history for prediction
def determine_winner(user_choice, ai_choice):
if user_choice == ai_choice:
return "tie"
elif (
(user_choice in ["rock", "r"] and ai_choice in ["scissors", "s"]) or
(user_choice in ["paper", "p"] and ai_choice in ["rock", "r"]) or
(user_choice in ["scissors", "s"] and ai_choice in ["paper", "p"])
):
return "user"
else:
return "ai"
def predict_user_move():
if len(recent_moves) < 2:
return random.choice(["rock", "paper", "scissors"])
# Convert deque to list for slicing
recent_moves_list = list(recent_moves)
# Generate patterns of length 2 (bigrams) from recent moves
bigrams = [tuple(recent_moves_list[i:i + 2]) for i in range(len(recent_moves_list) - 1)]
bigram_counts = Counter(bigrams)
# Compute the probability of each move following a bigram
next_move_probs = Counter()
for bigram in bigrams:
if bigram in bigram_counts:
next_move_index = recent_moves_list.index(bigram[1]) + 1
if next_move_index < len(recent_moves_list):
next_move = recent_moves_list[next_move_index]
next_move_probs[next_move] += 1
# If we have probabilities, predict the next move
if next_move_probs:
most_likely_move = max(next_move_probs, key=next_move_probs.get)
predicted_move = {"rock": "paper", "paper": "scissors", "scissors": "rock"}.get(most_likely_move, random.choice(
["rock", "paper", "scissors"]))
return predicted_move
# Fallback to random choice
return random.choice(["rock", "paper", "scissors"])
def play_round(user_choice):
global total_rounds, ai_wins
ai_choice = predict_user_move()
update_user_moves(user_choice)
winner = determine_winner(user_choice, ai_choice)
total_rounds += 1
if winner == "ai":
ai_wins += 1
ai_win_rate = (ai_wins / total_rounds) * 100 if total_rounds > 0 else 0
print(f"You chose: {user_choice}")
print(f"AI chose: {ai_choice}")
if winner == "tie":
print("It's a tie!\n")
elif winner == "user":
print("You win!\n")
else:
print("AI wins!\n")
print(f"AI win rate: {ai_win_rate:.4f}%\n")
def play_game():
print("Welcome to Rock-Paper-Scissors!")
print("Type 'exit' to quit the game.\n")
while True:
user_choice = input("Enter your choice (rock/r, paper/p, scissors/s) or 'exit' to quit: ").lower()
if user_choice == "exit":
print("Thanks for playing!")
break
user_choice = get_user_choice(user_choice)
if user_choice:
play_round(user_choice)
def test104(moves):
global total_rounds, ai_wins, recent_moves, history, user_moves
total_rounds = 0
ai_wins = 0
recent_moves.clear()
history.clear()
user_moves = {"rock": 0, "paper": 0, "scissors": 0}
print("Testing with predefined moves...\n")
start_time = time.time()
for move in moves:
user_choice = get_user_choice(move)
if user_choice:
play_round(user_choice)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Final AI win rate: {ai_wins / total_rounds * 100:.2f}%\n" if total_rounds > 0 else "No rounds played.")
print(f"Time taken: {elapsed_time:.2f} seconds\n")
if __name__ == "__main__":
# Define the predefined moves(not random)
predefined_moves = [
"rock", "paper", "scissors", "paper", "rock", "scissors", "paper", "rock",
"scissors", "rock", "scissors", "paper", "rock", "scissors", "rock", "rock",
"paper", "scissors", "rock", "paper", "paper", "scissors", "rock", "paper",
"scissors", "rock", "paper", "rock", "paper", "scissors", "rock", "paper",
"scissors", "rock", "rock", "scissors", "rock", "paper", "scissors", "rock",
"paper", "scissors", "rock", "paper", "paper", "scissors", "paper", "rock",
"scissors", "paper", "rock", "scissors", "rock", "paper", "scissors", "rock",
"paper", "paper", "scissors", "rock", "paper", "scissors", "rock", "paper",
"scissors", "rock", "paper", "scissors", "paper", "scissors", "rock", "paper",
"scissors", "rock", "paper", "paper", "rock", "rock", "scissors", "rock",
"paper", "rock", "scissors", "rock", "paper", "scissors", "paper", "scissors",
"rock", "paper", "scissors", "paper", "rock", "scissors", "rock", "paper",
"scissors", "rock", "rock", "paper", "scissors", "paper", "rock", "scissors",
"rock", "paper", "paper", "rock", "rock", "scissors", "rock", "paper",
"scissors", "rock", "scissors", "paper", "rock", "paper", "scissors",
"rock", "paper", "scissors", "paper", "rock", "paper", "rock", "scissors",
"rock", "paper", "paper", "scissors", "paper", "scissors", "rock", "paper",
"scissors", "paper", "rock", "paper", "rock", "rock", "paper", "scissors",
"rock", "paper", "scissors", "paper", "scissors", "rock", "paper",
"scissors", "rock", "paper", "scissors", "rock", "paper", "scissors",
"rock", "scissors", "rock", "paper", "scissors", "rock"
]
predefined_moves_random = [random.choice(["rock", "paper", "scissors"]) for i in range(200)]
while True:
user_input = input("Enter 'test204' to run the not random test and 'testrand' for random test(200 rounds "
"each), 'play' to start the game, or 'exit' to quit: ").lower()
if user_input == 'test204':
test104(predefined_moves)
elif user_input == "testrand":
test104(predefined_moves_random)
elif user_input == 'play':
play_game()
elif user_input == 'exit':
print("Thanks for using the program. Goodbye!")
break
else:
print("Invalid input. Please try again.")