A full-stack Tic Tac Toe game built with Spring Boot backend and Angular frontend.
- ✅ Two-player turn-based gameplay (X and O)
- ✅ Real-time game state management
- ✅ Win detection (rows, columns, diagonals)
- ✅ Draw detection
- ✅ Input validation on both frontend and backend
- ✅ Reset game functionality
- ✅ Start new game
- ✅ Beautiful, responsive UI
- ✅ RESTful API architecture
- ✅ CORS configuration for local development
- Java 17
- Spring Boot 3.1.5
- Maven (Build tool)
- Spring Web (REST API)
- Spring Validation (Input validation)
- Angular 17
- TypeScript 5.2
- RxJS 7.8
- Node.js & npm (Package management)
Before running the application, ensure you have the following installed:
-
Java JDK 17 or higher
- Download from: Oracle JDK or OpenJDK
- Verify installation:
java -version
-
Maven 3.6 or higher
- Download from: Apache Maven website
- Verify installation:
mvn -version
-
Node.js 18 or higher (includes npm)
- Download from: Node.js official website
- Verify installation:
node -vandnpm -v
-
Angular CLI 17
- Install globally:
npm install -g @angular/cli@17 - Verify installation:
ng version
- Install globally:
.
├── backend/ # Spring Boot Backend
│ ├── src/
│ │ ├── main/
│ │ │ ├── java/com/tictactoe/
│ │ │ │ ├── Application.java # Main Spring Boot application
│ │ │ │ ├── config/
│ │ │ │ │ └── WebConfig.java # CORS configuration
│ │ │ │ ├── controller/
│ │ │ │ │ └── GameController.java # REST API endpoints
│ │ │ │ ├── model/
│ │ │ │ │ ├── Game.java # Game model
│ │ │ │ │ ├── GameResponse.java # API response model
│ │ │ │ │ └── Move.java # Move model
│ │ │ │ └── service/
│ │ │ │ └── GameService.java # Business logic
│ │ │ └── resources/
│ │ │ └── application.properties # Configuration
│ │ └── test/
│ └── pom.xml # Maven dependencies
│
├── frontend/ # Angular Frontend
│ ├── src/
│ │ ├── app/
│ │ │ ├── models/
│ │ │ │ └── game.model.ts # TypeScript interfaces
│ │ │ ├── app.component.ts # Main component logic
│ │ │ ├── app.component.html # Game UI template
│ │ │ ├── app.component.css # Component styles
│ │ │ ├── app.module.ts # App module
│ │ │ └── game.service.ts # HTTP service
│ │ ├── index.html # HTML entry point
│ │ ├── main.ts # TypeScript entry point
│ │ └── styles.css # Global styles
│ ├── angular.json # Angular configuration
│ ├── tsconfig.json # TypeScript configuration
│ ├── tsconfig.app.json # App TypeScript config
│ ├── tsconfig.spec.json # Test TypeScript config
│ └── package.json # npm dependencies
│
└── README.md # This file
If you have this code in a repository:
git clone <repository-url>
cd <repository-folder>- Navigate to the backend directory:
cd backend
-
Build the project using Maven:
mvn clean install
-
Run the Spring Boot application:
mvn spring-boot:run
Or alternatively:
java -jar target/tictactoe-backend.jar
-
Verify the backend is running:
- You should see: "Tic Tac Toe Backend is running on http://localhost:8080"
- Test the health endpoint: Open browser to
http://localhost:8080/api/game/health
Backend will run on: http://localhost:8080
-
Open a NEW terminal window (keep the backend running)
-
Navigate to the frontend directory:
cd frontend -
Install npm dependencies (first time only):
npm install
This will install all Angular dependencies listed in package.json
-
Start the Angular development server:
ng serve
Or:
npm start
-
Wait for the compilation to complete. You should see:
** Angular Live Development Server is listening on localhost:4200 **
Frontend will run on: http://localhost:4200
- Open your web browser
- Navigate to: http://localhost:4200
- You should see the Tic Tac Toe game board
- Click on any cell to make a move
- Players X and O take turns automatically
- The game detects wins and draws
- Use "Reset" to restart the current game
- Use "New Game" to start a fresh game
The backend provides the following REST API endpoints:
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/game/new |
Create a new game |
| GET | /api/game/{gameId} |
Get game state |
| POST | /api/game/{gameId}/move |
Make a move |
| POST | /api/game/{gameId}/reset |
Reset the game |
| DELETE | /api/game/{gameId} |
Delete a game |
| GET | /api/game/health |
Health check |
curl -X POST http://localhost:8080/api/game/{gameId}/move \
-H "Content-Type: application/json" \
-d '{"row": 0, "col": 0}'- ✅ Row and column must be between 0 and 2
- ✅ Cell must be empty before making a move
- ✅ Cannot make moves after game is over
- ✅ Game ID must exist
- Three in a row (horizontal)
- Three in a column (vertical)
- Three in a diagonal
- All 9 cells are filled with no winner
Port 8080 already in use:
# Find and kill the process using port 8080
# On Windows:
netstat -ano | findstr :8080
taskkill /PID <PID> /F
# On Mac/Linux:
lsof -ti:8080 | xargs kill -9Java version error:
- Ensure Java 17 or higher is installed:
java -version
Port 4200 already in use:
# Angular will automatically suggest another port
# Or kill the process:
# On Mac/Linux:
lsof -ti:4200 | xargs kill -9npm install fails:
# Clear npm cache and try again
npm cache clean --force
rm -rf node_modules package-lock.json
npm installCannot connect to backend:
- Ensure backend is running on port 8080
- Check CORS settings in
WebConfig.java - Verify API URL in
game.service.tsishttp://localhost:8080
- Backend uses in-memory storage (ConcurrentHashMap) for game state
- Games are stored per session and will be lost when backend restarts
- CORS is configured to allow requests from
http://localhost:4200 - Frontend uses Angular HttpClient for API communication
- All API responses are in JSON format
- Add database persistence (PostgreSQL/MySQL)
- Implement user authentication
- Add multiplayer support with WebSockets
- Add game history and statistics
- Implement AI opponent
- Add sound effects and animations
- Mobile responsive improvements
This project is open source and available for educational purposes.
Enjoy playing Tic Tac Toe! 🎮