In this assignment you will be implementing a text-based version of the popular tic-tac-toe game!
The game will be played by entering in inputs to the program and printing out the status of the game to the user. When the game first begins, the following message will be printed to the console:
~~~~~~~~~~~~~~~~~~~~~~
Welcome to Tic-Tac-Toe
~~~~~~~~~~~~~~~~~~~~~~
After this, the game will begin, and the X and O players will alternate taking turns, starting with X. At the beginning of each turn, the current status/layout of the board will be printed. At the beginning of the game, the board has no Xs or Os on it, so a blank board status will look like so:
The board currently is:
/-----\
| | | |
-------
| | | |
-------
| | | |
\-----/
After the summer, your program should ask the current player for the row/column that they want to place the player at. This should look like so:
player X enter row: ?
player X enter col: ?
or . . .
player O enter row: ?
player O enter col: ?
The two players (X and O) will continue antering row/column values back and forth until one of the two players has won. In-between each move, the updated board will be printed.
Here’s an example of the first two moves of a full game:
~~~~~~~~~~~~~~~~~~~~~~
Welcome to Tic-Tac-Toe
~~~~~~~~~~~~~~~~~~~~~~
The board currently is:
/-----\
| | | |
-------
| | | |
-------
| | | |
\-----/
player X enter row: 2
player X enter col: 2
The board currently is:
/-----\
| | | |
-------
| |X| |
-------
| | | |
\-----/
player O enter row: 1
player O enter col: 3
The board currently is:
/-----\
| | |O|
-------
| |X| |
-------
| | | |
\-----/
player X enter row:
Notice the way in which the board updates on each turn.
You do not have to worry about the following edge/error cases:
When writing your code,, you should split up the logic into several function, each with a distinct task. Below are the functions you should use:
main()
-
Main should be the starting point of the game.
This function should control the main game logic, such as printing out the welcome message, and repeatedly asking users for row and columns.
All of the other functions will be called from main.print_board()
-
This function has one simple job: print out the text representation of the game board.
If you are using global variables to store the board in, you don’t need any parameters.
But you can use parameters if you want.do_move(row, col, char)
-
This function will handle placing a new X or O on the board.
The row
and col
parameters should be the location of the board to place the X or O on.
The char
should be the X
or O
.player_has_won()
-
This function’s job is to determine if one the players has won.
It should return a value that indicates if a player has one (thus, ending the game).You are not required to follow this pattern. However, you are required to split your logic up into at least 4 functions, some of which should user parameters and returns.
You should also include a header comment, with a detailed description of the game.
Name your program ttt.py
.
This program is due 2/12/2018 at 5:00pm. Turn in the program to the associated D2L dropbox.
Below are several example sequences of a game being played from start-to-finish.
~~~~~~~~~~~~~~~~~~~~~~
Welcome to Tic-Tac-Toe
~~~~~~~~~~~~~~~~~~~~~~
The board currently is:
/-----\
| | | |
-------
| | | |
-------
| | | |
\-----/
player X enter row: 2
player X enter col: 2
The board currently is:
/-----\
| | | |
-------
| |X| |
-------
| | | |
\-----/
player O enter row: 1
player O enter col: 3
The board currently is:
/-----\
| | |O|
-------
| |X| |
-------
| | | |
\-----/
player X enter row: 2
player X enter col: 1
The board currently is:
/-----\
| | |O|
-------
|X|X| |
-------
| | | |
\-----/
player O enter row: 3
player O enter col: 3
The board currently is:
/-----\
| | |O|
-------
|X|X| |
-------
| | |O|
\-----/
player X enter row: 2
player X enter col: 3
The board currently is:
/-----\
| | |O|
-------
|X|X|X|
-------
| | |O|
\-----/
Player X wins!
~~~~~~~~~~~~~~~~~~~~~~
Welcome to Tic-Tac-Toe
~~~~~~~~~~~~~~~~~~~~~~
The board currently is:
/-----\
| | | |
-------
| | | |
-------
| | | |
\-----/
player X enter row: 3
player X enter col: 2
The board currently is:
/-----\
| | | |
-------
| | | |
-------
| |X| |
\-----/
player O enter row: 1
player O enter col: 1
The board currently is:
/-----\
|O| | |
-------
| | | |
-------
| |X| |
\-----/
player X enter row: 2
player X enter col: 2
The board currently is:
/-----\
|O| | |
-------
| |X| |
-------
| |X| |
\-----/
player O enter row: 1
player O enter col: 2
The board currently is:
/-----\
|O|O| |
-------
| |X| |
-------
| |X| |
\-----/
player X enter row: 3
player X enter col: 3
The board currently is:
/-----\
|O|O| |
-------
| |X| |
-------
| |X|X|
\-----/
player O enter row: 1
player O enter col: 3
The board currently is:
/-----\
|O|O|O|
-------
| |X| |
-------
| |X|X|
\-----/
Player O wins!