In this project your job is to implement Tic-Tac-Toe for two players. You also can try writing some AI to play the game. If you find it easy, try to make it unbeatable.
- variables
- functions
- loops and conditionals
- nested lists
- print formatting
- external scripts
- user input handling
- error handling
Implement getEmptyBoard()
to return an empty 3-by-3 board, a list of lists filled with dots. The inner lists are rows.
- A list of lists is returned that represents a list of rows.
- Every cell of the returned value is
.
. - The rows of the returned value are independent, changing one row does not affect the others.
- Printing the result of the
getEmptyBoard()
function shows the following in the terminal.
[ [ '.', '.', '.' ], [ '.', '.', '.' ], [ '.', '.', '.' ] ]
Implement getPlayerMove()
which asks the user to make a valid move.
- If the user chooses an empty tile, the tile will be filled with an
X
or andO
. - The user can't mark an occupied tile.
When the user has chosen a tile, the program switch to another player.
- If the player starts with an
X
, after the move the program switch to0
and vice versa.
Implement getWinningPlayer()
that returns X
or O
based on the winning player has three of their marks in a horizontal, vertical, or diagonal row on board
.
- The
getWinningPlayer()
function returnsX
ifX
has a three-in-a-row onboard
. - The
getWinningPlayer()
function returns `` if there is no three-in-a-row on theboard
Implement isBoardFull()
that returns true
if the board is full.
- The
isBoardFull()
function returnstrue
if there are no empty cells on the board. - The
isBoardFull()
returnsfalse
if there are empty cells on the board.
The game shows if X
or O
or no one has won the game
- If player
X
wins, "X has won!" is displayed. - If player
0
wins, "0 has won!" is displayed. - If nobody wins, "It's a tie!" is displayed.
Implement all the functions so that the game will run successfully
- Player X starts the game.
- Players alternate their moves (
X
,0
,X
,0
...). - The board is displayed before each move, and at the end of game.
- The game ends when someone wins or the board is full.
- The game handles bad input (wrong coordinates) without crashing.
When the user has chosen a coordinate, that place is marked on the board.
- If the cell at
row
andcol
is empty (contains a dot .), it is marked withplayer
. - Out-of-bounds coordinates are not interpreted as moves.
- Coordinates of already occupied cells are not interpreted as moves.
Implement displayBoard()
that prints the board to the screen.
- Players are indicated with
X
and0
. Empty fields are indicated with dots (.
). - Coordinates are displayed around the board.
- The board is displayed in the following format:
1 2 3
A . | . | .
---+---+---
B . | . | .
---+---+---
C . | . | .
Allow players to quit the game anytime by typing quit
in any form of capitalization (quit and QuIt both work).
- Typing
quit
instead of coordinates results in the program exiting.
Implement player-against-AI mode. The AI can drive one of the players, and the game is fully playable against the computer.
- When the user selects the game mode
again AI
the function,getRandomAiCoordinates()
orgetUnbeatableAiCoordinates()
is called instead ofgetHumanCoordinates()
when it's Player0
turn. - When the user selects the game mode
again AI 'AI-HUMAN'
,getRandomAiCoordinates()
orgetUnbeatableAiCoordinates()
when it's Player0
turn. - Function
getUnbeatableAiCoordinates()
andgetRandomAiCoordinates()
returns a valid move (if possible) without asking for any input. - Function
getRandomAiCoordinates()
andgetUnbeatableAiCoordinates()
returns""
if the board is full. - Function
getMenuOption()
is implemented as a menu for between choosing 2-player mode and against-AI mode by pressing 1 or 2, respectively.
AI is capable of recognizing the opportunity to win the game with one move.
- Function
getUnbeatableAiCoordinates()
picks the winning move if there is one on the board.
AI is capable of recognizing if its enemy could win the game with the next move, and (supposing there is no direct winning move) moves against it.
- Function
getUnbeatableAiCoordinates()
(when there is no winning move in one step) picks a move which prevents a certain winning move for its enemy. - When there is a direct winning move, function
getUnbeatableAiCoordinates()
still picks that. - When there are multiple one-step options for the enemy,
getUnbeatableAiCoordinates()
tries to prevent one of them.
AI is unbeatable in all cases.
- There is no strategy or combination of steps that can win the game against the AI.
AI can play against itself
- When the menu option is called with the argument
'AI-AI'
, it callsgetUnbeatableAiCoordinates()
orgetRandomAiCoordinates()
for both players. - The game ends without any user input.
- There is a one second delay between moves to make gameplay easier to follow.
- You don't have to come up with an AI strategy. You can search the internet for strategy descriptions. Do not use external code; implement written instructions instead.
- You don't have to implement a general playing strategy. Tic-Tac-Toe has a rather easy unbeatable strategy that can be expressed as a sequence of conditionals.
- Ideal team size is 3-4.