A professional Node.js/TypeScript backend system for a dynamic Tic Tac Toe game featuring AI opponent integration, comprehensive game state management, and historical game tracking.
- Dynamic Grid Sizes: Support for 3x3 up to 10x10 game boards
- AI Opponent: OpenAI-powered intelligent opponent
- Game State Evaluation: Real-time winner detection with winning line tracking
- Game History: Persistent storage of completed games
- RESTful API: Well-documented endpoints with Swagger/OpenAPI
- Docker Support: Fully containerized with Docker Compose
- Comprehensive Testing: Unit and integration test coverage
- Live Game: Front-end component for testing APIs and playing games against AI pod
- Node.js 18+
- MongoDB 7.0+
- OpenAI API Key
- Docker & Docker Compose (for containerized deployment)
- Clone the repository:
git clone https://github.com/alexa/TicTacToeGame.git
cd TicTacToeGame- Install dependencies:
npm install- Configure environment variables:
cp .env.example .env
# Edit .env with your configuration and include OpenAI API key- Run the backend application for development:
# Development mode
npm run devRun the entire stack (backend, frontend, and MongoDB) with Docker Compose:
# Set your OpenAI API key
export OPENAI_API_KEY=openai_api_key
# Start all services
docker-compose up --build -d
# View logs
docker-compose logs -f
# Stop services
docker-compose downThe application includes a React-based frontend component that serves as a testing interface for the APIs. When using Docker Compose, the frontend is accessible at:
http://localhost
This frontend provides a user-friendly way to interact with the Tic Tac Toe game APIs and test their functionality.
Evaluates the current game state and determines if there's a winner.
{
"gameState": {
"board": [
["X", "O", null],
[null, "X", null],
[null, null, "X"]
],
"currentPlayer": "O",
"gridSize": 3
}
}{
"isGameOver": true,
"winner": "X",
"winningLine": [
{"row": 0, "col": 0},
{"row": 1, "col": 1},
{"row": 2, "col": 2}
],
"message": "Player X wins!"
}Retrieves a list of completed games with their results.
{
"games": [
{
"_id": "507f1f77bcf86cd799439011",
"winner": "X",
"gridSize": 3,
"finalBoard": [["X", "X", "X"], ["O", "O", null], [null, null, null]],
"winningLine": [{"row": 0, "col": 0}, {"row": 0, "col": 1}, {"row": 0, "col": 2}],
"createdAt": "2024-01-15T10:30:00Z"
}
]
}Gets AI move suggestion for the current game state.
{
"gameState": {
"board": [["X", null, null], [null, "O", null], [null, null, null]],
"currentPlayer": "X",
"gridSize": 3
}
}{
"move": {
"row": 0,
"col": 1
}
}Starts a new game against AI opponent.
{
"mode": "ai",
"playerSymbol": "X",
"gridSize": 3
}{
"gameId": "68565578-4346-44c3-b8f6-5bc622474f18",
"board": [
[null, null, null],
[null, null, null],
[null, null, null]
],
"currentPlayer": "X",
"aiSymbol": "O",
"gridSize": 3
}Makes a player move against AI opponent. The AI will automatically respond with its move.
{
"row": 0,
"col": 0
}{
"board": [
["X", null, "O"],
[null, null, null],
[null, null, null]
],
"currentPlayer": "X",
"status": "ongoing",
"winner": null,
"aiSymbol": "O",
"gridSize": 3
}Interactive API documentation is available via Swagger UI:
- Local: http://localhost:3000/api-docs
- Docker: http://localhost:3000/api-docs
Run the test suite:
# All tests
npm test
# Watch mode
npm run test:watch
# Coverage report
npm run test:coveragesrc/
├── config/ # Configuration files
├── controllers/ # Request handlers
├── services/ # Business logic
├── models/ # Database models
├── interfaces/ # TypeScript interfaces
├── middleware/ # Express middleware
├── routes/ # API routes
└── app.ts # Application entry point
- Service Layer Pattern: Separation of concerns between controllers and business logic
- TypeScript: Strong typing for better maintainability and developer experience
- MongoDB: NoSQL database for flexible game state storage
- OpenAI Integration: Fallback to random moves on API failures
- Docker Compose: Easy deployment and environment consistency
- Comprehensive Testing: Unit tests for logic, integration tests for API
- Validation errors return 400 Bad Request
- Server errors return 500 Internal Server Error
- All errors are logged with Winston
- Graceful fallbacks for AI service failures
- Database queries are limited to 100 most recent games
- AI requests have timeout protection
- Efficient game state evaluation algorithms
- Connection pooling for MongoDB
MIT