Upload 3 files
Browse files- app.py +99 -0
- game_moves.csv +4 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import csv
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.models import Sequential
|
| 6 |
+
from tensorflow.keras.layers import LSTM, Dense, Embedding
|
| 7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 8 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 9 |
+
|
| 10 |
+
# Mapping choices to numerical values
|
| 11 |
+
choices = {'rock': 0, 'paper': 1, 'scissors': 2}
|
| 12 |
+
rev_choices = {0: 'rock', 1: 'paper', 2: 'scissors'}
|
| 13 |
+
|
| 14 |
+
def get_computer_choice(model, past_moves):
|
| 15 |
+
if len(past_moves) < 3:
|
| 16 |
+
return random.choice(['rock', 'paper', 'scissors'])
|
| 17 |
+
|
| 18 |
+
# Prepare input data for prediction
|
| 19 |
+
sequence = [choices[move] for move in past_moves[-3:]]
|
| 20 |
+
sequence = pad_sequences([sequence], maxlen=3)
|
| 21 |
+
prediction = model.predict(sequence, verbose=0)
|
| 22 |
+
predicted_choice = rev_choices[np.argmax(prediction)]
|
| 23 |
+
|
| 24 |
+
# Counteract the predicted choice
|
| 25 |
+
counter_choices = {'rock': 'paper', 'paper': 'scissors', 'scissors': 'rock'}
|
| 26 |
+
return counter_choices[predicted_choice]
|
| 27 |
+
|
| 28 |
+
def get_winner(player, computer):
|
| 29 |
+
if player == computer:
|
| 30 |
+
return "It's a tie!"
|
| 31 |
+
elif (player == 'rock' and computer == 'scissors') or \
|
| 32 |
+
(player == 'scissors' and computer == 'paper') or \
|
| 33 |
+
(player == 'paper' and computer == 'rock'):
|
| 34 |
+
return "You win!"
|
| 35 |
+
else:
|
| 36 |
+
return "Computer wins!"
|
| 37 |
+
|
| 38 |
+
def save_move(player, computer, result):
|
| 39 |
+
with open('game_moves.csv', mode='a', newline='') as file:
|
| 40 |
+
writer = csv.writer(file)
|
| 41 |
+
writer.writerow([player, computer, result])
|
| 42 |
+
|
| 43 |
+
def load_data():
|
| 44 |
+
try:
|
| 45 |
+
with open('game_moves.csv', mode='r') as file:
|
| 46 |
+
reader = csv.reader(file)
|
| 47 |
+
next(reader) # Skip header
|
| 48 |
+
data = [row[0] for row in reader]
|
| 49 |
+
return data
|
| 50 |
+
except FileNotFoundError:
|
| 51 |
+
return []
|
| 52 |
+
|
| 53 |
+
def train_lstm_model(data):
|
| 54 |
+
tokenizer = Tokenizer(num_words=3)
|
| 55 |
+
tokenizer.fit_on_texts(data)
|
| 56 |
+
sequences = tokenizer.texts_to_sequences(data)
|
| 57 |
+
X = pad_sequences(sequences, maxlen=3)
|
| 58 |
+
y = np.array([choices[move] for move in data[1:]])
|
| 59 |
+
|
| 60 |
+
model = Sequential([
|
| 61 |
+
Embedding(input_dim=3, output_dim=10, input_length=3),
|
| 62 |
+
LSTM(20, return_sequences=False),
|
| 63 |
+
Dense(3, activation='softmax')
|
| 64 |
+
])
|
| 65 |
+
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
| 66 |
+
|
| 67 |
+
if len(X) > 1:
|
| 68 |
+
model.fit(X[:-1], y, epochs=10, verbose=0)
|
| 69 |
+
return model
|
| 70 |
+
|
| 71 |
+
def main():
|
| 72 |
+
print("Welcome to Rock, Paper, Scissors!")
|
| 73 |
+
with open('game_moves.csv', mode='w', newline='') as file:
|
| 74 |
+
writer = csv.writer(file)
|
| 75 |
+
writer.writerow(["Player Choice", "Computer Choice", "Result"])
|
| 76 |
+
|
| 77 |
+
past_moves = load_data()
|
| 78 |
+
model = train_lstm_model(past_moves)
|
| 79 |
+
|
| 80 |
+
while True:
|
| 81 |
+
player_choice = input("Enter rock, paper, or scissors (or 'quit' to exit): ").lower()
|
| 82 |
+
if player_choice == 'quit':
|
| 83 |
+
print("Thanks for playing!")
|
| 84 |
+
break
|
| 85 |
+
if player_choice not in ['rock', 'paper', 'scissors']:
|
| 86 |
+
print("Invalid choice. Please try again.")
|
| 87 |
+
continue
|
| 88 |
+
|
| 89 |
+
computer_choice = get_computer_choice(model, past_moves)
|
| 90 |
+
result = get_winner(player_choice, computer_choice)
|
| 91 |
+
print(f"Computer chose: {computer_choice}")
|
| 92 |
+
print(result)
|
| 93 |
+
print()
|
| 94 |
+
|
| 95 |
+
save_move(player_choice, computer_choice, result)
|
| 96 |
+
past_moves.append(player_choice)
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
main()
|
game_moves.csv
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Player Choice,Computer Choice,Result
|
| 2 |
+
rock,paper,Computer wins!
|
| 3 |
+
paper,scissors,Computer wins!
|
| 4 |
+
scissors,rock,Computer wins!
|
requirements.txt
ADDED
|
File without changes
|