Description
For this program, you will be writing a simulation for the game, Tic-Tac-Toe. Two people play Tic
Tac Toe with paper and pencil. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of their marks on the board in a row, column or one of the two diagonals, they win. When the board fills up with neither player winning, the game ends in a draw.
This program has two parts:
- Two Players
- Play with Computer
Part One: Two Players
- Take user input (for two players)
- Store user inputs into a 3×3 Tic-Tac-Toe board
- Check for validity of inputs (no overriding, proper cell value, etc.)
- Check for a win every turn
- Print the board every turn
Part Two: With Computer
- Randomly pick a player (you or computer)
- Take input (for one players)
- Computer or you will generate the next
- Store user inputs into a 3×3 Tic-Tac-Toe board
- Check for validity of inputs (no overriding, proper cell value, etc.)
- Check for a win every turn
- Print the board every turn
Getting Player Input:
- Each player will input a number (1-9) which corresponds to the following cells.
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
- Location 0 in list corresponds with number 1
- Players’ input will be stored in a list.
- Players will take turns.
Checking For a Win:
- A player wins if there is a sequence of 3 of the same letter in the same row, column, or diagonal.
Printing Output:
- You should use for loops to output a 3×3 board.
General Notes:
- You should implement your functions in a file, py Checking for a win requires multiple operations.
Functions:
- Welcome
- Welcomes user to the game and explain the game rules.
- You will provide your own Welcome information
- Ask user to decide if playing with computer (pass 1) or another player (pass 2)
- No parameter to pass
- Return 1 if play with computer and 2 if plays with another player
- createBoard
- You will use a list to store the data
- List has 9 items starting from index 0 to 9 which holds the values (X or O)
- This function has no parameter to pass and it return a list
- Your game board at first should look like the following:
1 | 2 | 3
—————
4 | 5 | 6
————— 7 | 8 | 9
- printBoard
- The printBoard function will print the game. Remember that the board is represented as a list of nine strings, where the string at index 0 is the mark on space 1 on the Tic Tac Toe board, and so on.
- Passing a list as the board to the function.
- The first call to the print will show an empty board game:
| |
—————
| |
—————
| |
- pickLetter
- This function ask user to pick ‘X’ or ‘O’
- No parameter to pass
- This will return the letter of player’s choice
- If user enters the wrong letter you need to ask user to reenter the letter
- This function return letter
- getInput
- This function takes the letter and board as parameters
- Ask user to enter the letter of his/her choice in the one of the locations (1-9) on the board
- Where do you like to place your letter (pick in range of 1-9)
- If user repeat the location or pass value outside range (1-9) for placing the letter:
- Send a message:
- Invalid move! Location is already taken. Please try again.
- This function will return the board after entering the letter
- checkRows
- takes the board as parameter
- returns True and the letter if a row is filled with the same Letter
- checkCols
- takes the board as parameter
- returns True and the letter if a column is filled with the same Letter
- checkDiags
- takes the board as parameter
- returns True and the letter if any of diagonals are filled with the same Letter
- boardFull
- takes the board as parameter
- returns True if the board is full
- checkWin
- takes the board
- if any of checking (rows, columns, diagonals ) returns True
- announce the winner
- if board is filled and no winner announce draw!
- turnCount
Use this function to show, which turns the game is. Example: It’s Player 2’s (O’ )turn!
- takes count of turns
- increment counter
- return counter
Resources:
You can download the following files from PolyLearn:
- py, at least 2 test cases for each function
- py
- incol (test file for two player)
- inrow (test file for two player)
- india (test file for two player)
For testing your program with these files:
python3 tictactoe.py < incol python3 tictactoe.py < inrow
python3 tictactoe.py < india



