1. Overview of the Design
The Othello AI project is a web-based implementation of the classic board game Othello (also known as Reversi). The application allows users to play against a configurable AI opponent with multiple difficulty levels. A key feature is the real-time analysis heatmap, which visualizes the AI's strategic evaluation of potential moves, serving as an educational tool for players.
The system is built using a modern full-stack architecture, with a Python FastAPI backend and a vanilla JavaScript frontend. This approach demonstrates core software engineering principles, including API design, database management, AI algorithm implementation, and user interface development.
The primary purpose of the application is twofold:
- To provide an engaging and challenging Othello experience for players of all skill levels.
- To serve as a practical case study for developers, demonstrating how to integrate various AI models into a web application.
The application handles failure conditions, such as invalid user moves, by providing immediate feedback through the UI. Upon first use, the application presents a default game board, allowing a guest user to play against the "Easy" AI immediately. The main limitations are that guest users cannot play against harder AI difficulties, and their game history is not saved.
1.1 Assumptions, Constraints, and Risks
1.1.1 Assumptions
- End-User Environment: Users are expected to have a modern web browser (e.g., Chrome, Firefox, Safari, Edge) with JavaScript enabled.
- Technical Proficiency: Users are assumed to have a basic understanding of how to interact with web applications.
- Operating System: The backend is developed on a UNIX-like system (macOS) but is platform-agnostic.
- Dependencies: The system assumes that all backend dependencies listed in
requirements.txtare installed.
1.1.2 Constraints
- Hardware/Software Environment: The application is a web application and does not have a native desktop or mobile version.
- Database: The system uses SQLite for development, constraining high concurrency.
- Performance: AI response time is constrained by search depth and iterations.
- Security: Authentication uses JWTs in local storage, which has security implications (e.g., XSS).
1.1.3 Risks
- AI Performance: Advanced AI models can be slow.
Mitigation: AI complexity is limited; the frontend handles asynchronous moves. - Scalability: SQLite is not suitable for production.
Mitigation: SQLAlchemy allows for a seamless transition to a more robust database. - Security Vulnerabilities: Risk of SQL Injection, XSS, etc.
Mitigation: Use of SQLAlchemy, Pydantic validation, and secure password hashing. - Guest User Experience: Guests can only play the easiest AI.
Mitigation: This is a design choice to encourage registration.
2. Detailed Design
2.1 Data Models and Database Design
The database is designed to store information about users, games, leaderboard rankings, and user feedback.
2.1.1 Logical Data Model (ERD)
2.1.2 Data Dictionary
Table: users
| Column Name | Data Type | Constraints | Description |
|---|---|---|---|
| id | INTEGER | PRIMARY KEY, AUTOINCREMENT | Unique identifier for the user. |
| username | VARCHAR(50) | NOT NULL, UNIQUE | User's chosen username. |
| VARCHAR(100) | NOT NULL, UNIQUE | User's email address. | |
| password | VARCHAR(255) | NOT NULL | Hashed password. |
| is_admin | BOOLEAN | NOT NULL, DEFAULT FALSE | Flag indicating admin privileges. |
| is_active | BOOLEAN | NOT NULL, DEFAULT TRUE | Flag for suspending user accounts. |
2.1.3 Database Schema (SQL)
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
is_admin BOOLEAN NOT NULL DEFAULT 0,
is_active BOOLEAN NOT NULL DEFAULT 1
);
CREATE TABLE games (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
ai_level VARCHAR(20) NOT NULL,
user_color VARCHAR(5) NOT NULL,
winner VARCHAR(20) NOT NULL,
user_score INTEGER NOT NULL,
ai_score INTEGER NOT NULL,
timestamp DATETIME NOT NULL DEFAULT (CURRENT_TIMESTAMP),
FOREIGN KEY(user_id) REFERENCES users (id)
);
-- Other CREATE TABLE statements would go here...
2.2 User Interface
The user interface is a single-page application (SPA) built with vanilla JavaScript, HTML, and CSS.
2.2.1 Structure and Navigation
Navigation is handled via buttons and links in the header and modals that overlay the main view.
Navigation Flow:
Guest: Homepage ➔ Play as Guest / Login / Register / Leaderboard
Authenticated User: Game View ➔ New Game / Feedback / Leaderboard / Admin (if admin)
2.2.2 Screen Descriptions
1. Main Game Screen
The central view of the application, consisting of the game board and info panel.
2. New Game Modal
Appears when the "New Game" button is clicked, allowing game configuration.
3. Leaderboard Modal
Displays a table of the top-ranked players.
4. Admin Dashboard
An admin-only view for managing users and viewing feedback.
2.3 API Development
The backend provides a RESTful API for the frontend to consume. Key endpoints include:
POST /api/token: Authenticates a user and returns a JWT.POST /api/users/: Registers a new user.POST /api/game/move: Submits a player's move and gets the AI's response.GET /api/leaderboard/: Retrieves the leaderboard rankings.GET /api/admin/users/: (Admin) Retrieves a list of all users.
3. Non-Functional Requirements
- Security: Passwords are hashed with
bcrypt. Auth uses JWTs. SQLAlchemy ORM protects against SQL injection. - Capacity: SQLite is suitable for low traffic; can be migrated to PostgreSQL for scaling.
- Compatibility: Backend requires Python 3.8+. Frontend requires a modern browser (ES6).
- Reliability: Designed for 24/7 availability. Primary failure point is the host server.
- Performance: AI response times are critical and benchmarked for each difficulty level.
- Usability: The interface is designed to be simple and intuitive.
4. Tests
The project includes a testing suite using Python's pytest framework to ensure functionality and correctness. Tests are isolated using a separate, in-memory SQLite database.
General Test Scenarios Covered:
- User registration and login.
- Making a valid game move as a guest and as a logged-in user.
- API endpoint responses for both valid and invalid requests.
- Correct data persistence in the test database.
- Functionality of core game logic and AI move generation.
Appendix A: References
| Name | Author/Organization | Description | Location/Identifier |
|---|---|---|---|
| Official Rules of Othello | World Othello Federation | Official rules governing game logic. | worldothello.org |
| AI: A Modern Approach | Stuart Russell & Peter Norvig | Textbook covering Minimax and Alpha-Beta Pruning. | ISBN: 978-0134610993 |
| FastAPI Documentation | Tiangolo | Official documentation for the backend framework. | fastapi.tiangolo.com |
Appendix B: Key Terms
| Term | Definition |
|---|---|
| API | Application Programming Interface. Allows software applications to communicate. |
| Alpha-Beta Pruning | An optimization technique for the Minimax algorithm. |
| ERD | Entity-Relationship Diagram. A diagram showing database entity relationships. |
| JWT | JSON Web Token. Used for secure authentication. |
| MCTS | Monte Carlo Tree Search. An AI search algorithm using random sampling. |
| SPA | Single-Page Application. A web app that dynamically rewrites the current page. |