An intelligent Rock-Paper-Scissors game that learns from your playing patterns using a neural network. The AI analyzes your previous moves and game outcomes to predict your next move and counter it strategically.
- Interactive Web Interface: Clean, responsive web UI with emoji-based game buttons
- Neural Network Learning: PyTorch-based neural network that learns from gameplay patterns
- Training Mode: Collect training data through interactive gameplay
- AI Prediction: Trained model predicts player moves based on game history
- Dual Play Modes: Random play (untrained) and AI play (trained)
- Real-time Feedback: Live game results and model status updates
The AI uses a neural network to learn patterns in your gameplay:
- Input Features: Previous player move, previous program move, and previous game result (9 features total via one-hot encoding)
- Neural Architecture:
- Input layer: 9 neurons (3 for each categorical feature)
- Hidden layer: 32 neurons with ReLU activation
- Output layer: 3 neurons (probabilities for Rock, Paper, Scissors)
- Training: Uses collected gameplay data to train the network with CrossEntropyLoss and Adam optimizer
- Prediction: Predicts your next move and plays the counter-move to maximize win probability
- Python 3.7+
- pip package manager
- Clone or download the project files
- Install required dependencies:
pip install fastapi uvicorn torchEnsure you have all these files in your project directory:
app.py- FastAPI web servermain.py- Core game logic and neural networkindex.html- Web interfacestyles.css- Stylingscript.js- Frontend JavaScript logic
- Start the web server:
uvicorn app:app --reload-
Open your browser and navigate to
http://localhost:8000 -
Training a Model:
- Enter the number of training samples (recommended: 50-100)
- Click "Train New Model"
- Play the specified number of games by clicking Rock, Paper, or Scissors
- The AI will collect data and train automatically
-
Playing Games:
- After training, click Rock, Paper, or Scissors to play
- The AI will use your gameplay patterns to predict and counter your moves
- Click "Reset Model" to clear training data and start over
Run the standalone version:
python main.pyFollow the prompts to:
- Train a new model with sample data
- Play games against the AI
- Quit the program
- Rock (🪨) beats Scissors (✂️)
- Paper (📄) beats Rock (🪨)
- Scissors (✂️) beats Paper (📄)
- Same moves result in a tie
Input Layer (9 neurons)
↓
Hidden Layer (32 neurons, ReLU)
↓
Output Layer (3 neurons, Raw logits)
- Epochs: 500 (default)
- Learning Rate: 0.005
- Loss Function: CrossEntropyLoss
- Optimizer: Adam
- Minimum Training Samples: 1 (recommended: 50+)
GET /- Web interfaceGET /play- Get random moveGET /evaluate- Evaluate game resultPOST /train_model- Train the neural networkGET /predict_move- Get AI predictionPOST /add_datum- Add training dataPOST /reset- Reset model
project/
├── app.py # FastAPI web server
├── main.py # Core game logic and neural network
├── index.html # Web interface
├── styles.css # Styling
├── script.js # Frontend JavaScript
└── README.md # This file
- Training Data: More samples generally improve AI performance (50-100 recommended)
- Consistent Patterns: The AI learns better when you have detectable patterns
- Game History: AI needs at least one previous game to make predictions
- Model not making smart moves: Increase training samples or ensure you have consistent patterns
- Web interface not loading: Check that all files are in the same directory
- Training fails: Ensure you have at least 2 samples to create training pairs
If you encounter import errors:
pip install --upgrade torch fastapi uvicornFeel free to enhance the project by:
- Improving the neural network architecture
- Adding more sophisticated features
- Enhancing the web interface
- Adding game statistics and analytics
This project is open source and available under the MIT License.
- Add game statistics and win/loss tracking
- Implement different AI difficulty levels
- Add multiplayer support
- Include more sophisticated pattern recognition
- Add export/import functionality for trained models