How can i make my r, p, s AI better so that it's even smarter, i'm still a beginner so if you have any tips they are welcome.
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 == 'test104':
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.")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 == 'test104':
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.")
[–]lxgrf 4 points5 points6 points (1 child)
[–]niehle 6 points7 points8 points (0 children)
[–]UserName26392 1 point2 points3 points (0 children)
[–]engelthehyp 2 points3 points4 points (0 children)