Machine Coding Question: Design Tic Tac Toe
Design and implement an NxN Tic Tac Toe game — turn-based play, move validation, win/draw detection, AI mode, and multi-session bonus features.
Design and implement a Tic Tac Toe game where two players take turns marking spaces in an NxN grid. The game should handle invalid moves, determine the winner, and announce the game result appropriately.
This one tests whether you can model turn-based game state cleanly — board representation, move validation, and win detection that generalizes beyond the classic 3x3 case — while keeping the design open to bonus features like sessions, pause/resume, and replay.
Objective
The primary objective of this project is to design and implement an NxN Tic Tac Toe game where two players take turns marking cells, invalid moves are rejected with a clear reason, and the game correctly detects a winner or a draw.
Functional Requirements
Requirements are split into three tiers so you know what to prioritize under time pressure: build first, build if time allows, and discuss only.
Part 1 — Basic Requirements
Core functionality you must build and get working first.
1. Game Setup
2. Valid Moves & Gameplay
3. Winning & Draw Conditions
4. Play Against an AI (Single Player Mode)
Let the user choose Human vs. Human or Human vs. AI. The basic AI plays a random valid move; an advanced AI with difficulty levels and optimal-move strategies can be introduced later.
Part 2 — Bonus Features
Extensibility checks — can your design support multiple concurrent sessions, custom symbols, and persistence without a rewrite.
Part 3 — Future Features
Discuss-only evolution — how the system could grow next.
Example Usage: Full Walkthrough
Here's how a sample playthrough runs end-to-end, one step at a time.
1. Launch and set up the game
Would you like to:
1. Start a New Game
Enter choice: 1
Enter Grid Size : 3
Choose Game Mode:
1. Human vs. Human
2. Human vs. AI
Enter choice: 1
Enter Player 1 Name: Alice
Enter Player 2 Name: Bob
Alice, choose your symbol (X or O): X
New game started! Game ID: abc12345
2. Alice and Bob take turns
Current Board:
| |
---------
| |
---------
| |
Alice's turn. Enter row and column (0-2): 0 0
Current Board:
X | |
---------
| |
---------
| |
Bob's turn. Enter row and column (0-2): 1 1
Current Board:
X | |
---------
| O |
---------
| |
Alice's turn. Enter row and column (0-2): 0 1
Current Board:
X | X |
---------
| O |
---------
| |
Bob's turn. Enter row and column (0-2): 2 2
Current Board:
X | X |
---------
| O |
---------
| | O
3. Alice completes a row and wins
Alice's turn. Enter row and column (0-2): 0 2
Current Board:
X | X | X
---------
| O |
---------
| | O
Alice wins the game!
4. Invalid move handling
Bob's turn (O): 0 0
Invalid move! Cell is already occupied. Try again.
Bob's turn (O): 1 2
5. Draw condition
The board is full! It's a draw.
Game Over.
Alice wins by completing the top row with X, X, X. An earlier attempt by Bob to play (0,0) is rejected since the cell is already occupied, and the turn correctly stays with Bob until a valid move is made.
Bonus Walkthrough: Sessions, Pause & Replay
1. Session menu
Would you like to:
1. Start a New Game
2. Resume an Existing Game
3. View Active Games
4. Replay a Finished Game
Enter choice: 1
2. Game setup with custom symbols
Enter Grid Size (e.g., 3 for 3x3, 4 for 4x4, etc.): 3
Choose Game Mode:
1. Human vs. Human
2. Human vs. AI
Enter choice: 1
Enter Player 1 Name: Alice
Enter Player 2 Name: Bob
Alice, choose your symbol (Any letter or character): X
Bob, choose your symbol (Any letter or character): O
New game started! Game ID: abc12345
To resume later, use Game ID: abc12345
3. Move options after each turn
Alice's turn. Enter row and column (0-2): 0 0
Move recorded!
Current Board:
X | |
---------
| |
---------
| |
Options:
1. Continue
2. Undo Last Move
3. Pause Game
Enter choice: 1
4. Pausing the game
Alice's turn. Enter row and column (0-2) or type 'pause' to save and exit: pause
Game saved!
To resume later, use Game ID: abc12345
Returning to the main menu...
5. Resuming the game
Enter choice: 2
Enter Game ID to resume: abc12345
Resuming game...
Current Board:
X | |
---------
| |
---------
| | O
Alice's turn. Enter row and column (0-2): 0 1
Move recorded!
Current Board:
X | X |
---------
| |
---------
| | O
6. Viewing active and completed games
Enter choice: 3
Active Games:
1. Game ID: xyz67890 (Ongoing)
2. Game ID: abc12345 (Completed)
7. Replaying a finished game
Enter choice: 4
Enter Game ID to replay: abc12345
Replaying game step by step...
Move 1: Alice placed 'X' at (0,0)
Move 2: Bob placed 'O' at (2,2)
Move 3: Alice placed 'X' at (0,1)
Move 4: Alice placed 'X' at (0,2)
Final Board:
X | X | X
---------
| |
---------
| | O
Alice wins the game! 🎉
8. Undo last move
Options:
1. Continue
2. Undo Last Move
3. Pause Game
Enter choice: 2
Move undone!
Current Board:
X | |
---------
| |
---------
| |
Bob's turn. Enter row and column (0-2): 2 2
Move recorded!
Current Board:
X | |
---------
| |
---------
| | O
Games persist independently by Game ID, so Alice can pause mid-game and resume exactly where she left off, and a finished game can be replayed move by move to the same final board.