A modern web-based FreeCell solitaire game with an AI solver built using Python FastAPI and vanilla JavaScript. The game features a clean, responsive interface and includes a Monte Carlo Tree Search (MCTS) algorithm that can automatically play moves.
- Interactive Web Interface: Clean, modern UI with click-to-move functionality
- AI Solver: Monte Carlo Tree Search algorithm that can suggest and play optimal moves
- Game Controls: New game, undo moves, computer play, and quit options
- Responsive Design: Works on desktop and mobile devices
- Visual Feedback: Card selection highlighting and move validation
- Game State Management: Full undo/redo functionality with move history
The game features:
- Free Cells: 4 temporary storage slots for cards
- Home Cells: 4 foundation piles (one for each suit)
- Table Columns: 8 columns where the main gameplay occurs
- Action Buttons: Easy access to all game functions
- Python 3.7+
- FastAPI
- Uvicorn (for running the server)
-
Clone or download the project files
# Ensure you have all these files in your project directory: # - app.py # - main.py # - index.html # - styles.css # - script.js
-
Install dependencies
pip install fastapi uvicorn
-
Run the server
uvicorn app:app --reload
-
Open your browser Navigate to
http://localhost:8000to start playing!
FreeCell is a solitaire card game where the goal is to move all cards to the home cells (foundations) in ascending order by suit.
- Free Cells (4): Temporary storage for individual cards
- Home Cells (4): Foundation piles for each suit (Clubs, Diamonds, Hearts, Spades)
- Table Columns (8): Main playing area with cascading cards
- Column to Column: Cards must be placed in descending order with alternating colors
- Column to Free Cell: Any exposed card can be moved to an empty free cell
- Free Cell to Column: Cards from free cells can be moved to valid column positions
- Column/Free Cell to Home: Cards can be moved to home cells in ascending order by suit (A, 2, 3... K)
- Click to Select: Click on a card to select it (highlighted in blue)
- Click to Move: Click on a destination to move the selected card
- New Game: Start a fresh game with shuffled cards
- Computer Play: Let the AI make a move using MCTS algorithm
- Undo: Reverse the last move
- Quit: End the current game
- Backend: Python FastAPI server handling game logic and AI
- Frontend: Vanilla JavaScript with modern CSS styling
- AI Algorithm: Monte Carlo Tree Search (MCTS) for move optimization
GET /- Serve the main game interfaceGET /get_game_state- Get current board statePOST /start- Start a new gamePOST /move_column- Move card between columnsPOST /move_to_free- Move card to free cellPOST /move_from_free- Move card from free cellPOST /column_to_home- Move card to home cellPOST /free_to_home- Move card from free cell to homeGET /computer_play- Get AI move suggestionPOST /undo- Undo last moveGET /is_game_won- Check win condition
freecell-solver/
├── app.py # FastAPI server and API endpoints
├── main.py # Game logic and MCTS algorithm
├── index.html # Main HTML interface
├── styles.css # Custom CSS styling
├── script.js # Frontend JavaScript logic
└── README.md # This file
The computer player uses Monte Carlo Tree Search (MCTS) to determine optimal moves:
- Selection: Navigate down the game tree using UCB1 formula
- Expansion: Add new possible moves to the tree
- Simulation: Play out random games to completion
- Backpropagation: Update node statistics based on simulation results
The AI prioritizes moves to home cells and uses 100 simulations by default for move selection.
Modify the simulation count in script.js:
const response = await fetch('/computer_play?sim=100'); // Change 100 to desired numberEdit styles.css to customize:
- Card appearance and sizing
- Color schemes
- Hover effects and animations
- Layout and spacing
Modify main.py to adjust:
- Move validation logic
- Scoring system
- Win conditions
- Card shuffling algorithm
-
Server won't start
- Ensure FastAPI and Uvicorn are installed:
pip install fastapi uvicorn - Check that port 8000 is available
- Ensure FastAPI and Uvicorn are installed:
-
Cards not displaying
- Verify all files are in the same directory
- Check browser console for JavaScript errors
- Ensure server is running on
http://localhost:8000
-
AI not working
- Computer play requires valid moves to be available
- Check console for API errors
- Verify game state is properly initialized
- Reduce MCTS simulations for faster AI moves
- Use browser dev tools to monitor API calls
- Clear browser cache if experiencing display issues
Feel free to contribute improvements:
- Enhanced AI algorithms
- Better move validation
- Additional game statistics
- UI/UX improvements
- Mobile responsiveness enhancements
This project is open source and available under the MIT License.
Built with:
- FastAPI - Modern Python web framework
- Tailwind CSS - Utility-first CSS framework
- Inter Font - Clean, readable typography
Enjoy playing FreeCell! 🃏