Question: In this lab you will write a that will let two players play the game of Tic Tac Toe. You will write functions to perform

In this lab you will write a that will let two players play the game of Tic Tac Toe. You will write
functions to perform various sub tasks (checking rows, checking column, checking diagonal,
checking for empty cells, and printing grid). Grid will be represented with a nested (2D) list.
Outer list will contain three inner list (each representing a row of the grid). Each inner grid will
contain three elements (one for each column). Elements of the inner list can be an x,o or
blank (). Make sure that you read this lab handout carefully before starting to write your code.
You will implement the following functions:
1. reset_grid (grid): reset the grid by changing all the elements to blank ().
2. check_row (grid, row_num): returns True if all the elements in the given row are x or all
the elements in the given row are o, otherwise it will return False.
3. check_column (grid, col_num): returns True if all the elements in the given column are x
or all the elements in the given column are o, otherwise it will return False.
CSCI 1411 Lab 11 Page 2 of 6
4. check_diagonal (grid, diagonal): returns True if all the elements in the given diagonal are
x or all the elements in the diagonal are o, otherwise it will return False. Note that the
diagonal d1 includes elements [0][0],[1][1] and [2][2] and the diagonal d2 includes
elements [0][2],[1][1] and [2][0].
5. place_entry (grid, row_num, col_num, ch): places the ch into the location given by
row_num and col_num). ch will be either x or o. It will check to make sure that the cell
given by row_num and col_num is blank. If it is blank then it will place the ch in the
given location and return True. If the cell is not empty then it will return False.
6. playable (grid): check to see if there are any empty (blank) elements. It will use nested
loops to check each element in the nested list. If any of the element is empty (blank) then
it will return True, otherwise it will return False.
7. print_grid (grid): prints grid using f string.
main function will use the above set of functions to perform various task. Outline of the main
function is as follows:
1. Create a nested lists as follows:
grid =[[,,],[,,],[,,]]
2. Ask player 1 to enter their name. Player 1 will get x and will go first.
3. Ask player 2 to enter their name. Player 2 will get o and will go second.
4. Loop which iterates till game is over. Game will end when one of the players wins or
there are no more empty cells in the grid.
a. Ask player one to enter the location which they want to mark the location. The player
will enter two integers as x y; where x is the row number and y is the column number.
Example: 2,3. Note that players will count rows and columns as 1,2, and 3 but our
program will use 0,1, and 2 for row number and column number.
i. Check to make sure that the entered row number and column numbers are 1,2, or
3. If they are not then print an error message and player 1 loses their turn.
ii. Use the place_entery function to place an x in the selected location. If place_entry
returns a False then print an error message and player 1 loses their turn.
iii. Print the grid.
iv. Use check_row function to check the selected row for completion. If the
check_row function returns True then print the message that Player 1 won the
game and this will end the game.
v. Use check_colum function to check the selected column for completion. If the
check_row function returns True then print the message that Player 1 won the
game and this will end the game.
vi. If the player placed the mark on the diagonal (d1 or d2) the use the
check_diagonal function to check for completion. If the check_diagonal function
returns True then print the message that Player 1 won the game and this will end
the game.
vii. Use playable function to check if there are any empty cells. If playable function
returns False then there are no empty cells. Print the message that game is draw
and end the game.
CSCI 1411 Lab 11 Page 3 of 6
b. Ask player two to enter the location which they want to mark the location. The player
will enter two integers as x y; where x is the row number and y is the column number.
Example: 2,3. Note that players will count rows and columns as 1,2, and 3 but our
program will use 0,1, and 2 for row number and column number.
i. Check to make sure that the entered row number and column numbers are 1,2, or
3. If they are not then print an error message and player 2 loses their turn.
ii. Use the place_entery function to place an o in the selected location. If place_entry
returns a False then print an error message and player 2 loses their turn.
iii. Print the grid.
iv. Use check_row function to check the selected row for completion. If the
check_row function returns True then print the message that Player 2 won the
game and this will end the game.
v. Use check_colum function to check the selected column for compl

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!