Assigned Date: Friday, Apr. 3, 2015
Due Date: Monday, Apr. 13, 2015
Due Time: 12:20pm
Last modified on April 09, 2015, at 09:04 PM (see updates)
Write a program that simulates a game of Tic-Tac-Toe. In our version of the game, two players, human and computer, play in turns, placing X's and O's, respectively, on the 3x3 square board. The human player always goes first. The game goes on until either there is a winner (3 X's or 3 O's in a vertical, horizontal, or diagonal line), or the game is a draw (all squares are occupied and no one has won).
The program should display the current board configuration (via print statements) after each move, e.g.,
X | |
O | |
X | O |
and prompt the user (via an input statement) to enter a move (a number from 1 to 9).
The human player always goes first. Squares are numbered consecutively, from 1 to 9, where 2 is the middle square in the top row. The program should be as robust as possible. When the game is over it should output a message describing the outcome.
This is a solo assignment (you must work alone).
- To design expressions using relational and logical operators
- To design selection statements
- To design repetition statements
- To design simple data structures using lists
- To design functions
- To apply top-down design in program development
Input and Output
The input to the program is the human player's moves - one at a time - using a number (1-9). If the user makes mistakes, you should output corresponding error messages and allow them to try again.
The output (after every human move) should be the updated board (showing the newly placed 'X'), followed by the computer move (e.g., "Computer moves to square 3!"), followed by the updated board.
To simplify automated testing, for now, the computer should choose the next available (non-empty) square starting from 1.
Later we will use some Artificial Intelligence (AI) code to make the game more interesting.
In general, you should comment any variable, obscure statement, block of code, etc. you create.
Your comments should reflect the "why" something is there (or being done), as opposed to the "how". The "how" is reflected in the code. The "why" usually comes from the thought you had, as to what you were trying to do, before you wrote the corresponding Python code.
So, since that info is in your head first (i.e., before you write the code), it is much easier to type it in your program (before you figure out the corresponding code that does what needs to be done).
Conversely, once you have already written the code, it is very hard to go back and recreate the original thought (thus comments written after the code tend to focus on the "how" something is being done, and not the "why" - the latter being what we need.)
The goal of comments is to gently introduce someone accurately into the big picture of our code - the forest view - while the code itself represents the tree view.
Additionally, your code should always have header documentation that explains what the program does, in general terms.
Follow the documentation instructions from Homework 1.
You will submit your assignment via OAKS. Your submission consists of:
- Your Python program. Call it, ticTacToe.py.
- A printout of your program - to be submitted in class on the due date.
- Be ready to play (demonstrate) your game in class.
Your grade will be based on how well you followed the above instructions.