Question: Python code tic tac toe game: Let's use what you've learned about drawing methods to create a simple tic-tac-toe game. The game rules are simple:

Python code tic tac toe game:

Let's use what you've learned about drawing methods to create a simple tic-tac-toe game.

The game rules are simple:

- players take turns marking cells in a 3 x 3 grid with either X's or O's

- As soon as all cells in a row, column, or diagonal are marked by the same player, that player wins and the game is over.

So your task is to:

create a graphic window with a 3 x 3 grid,

allow players to mark an empty cell by clicking within it

place an X or O in the center of the selected cell (you can assume players alternate turns)

when a player has won (according to the above rules), draw a line through the winning row, column, or diagonal cells

Hint:

You can use a counter variable and a while loop to get mouse clicks until all cells are filled or a player wins,

You can compare the x & y coordinates of each mouse click against coords of grid lines to determine the row and column selected. Because cells are equal size, you know the x coordinate for each vertical line, and the y coordinate for each horizonal line.

You'll need some data structure to track which cells have been marked and by which player. A 2-dimensional list may be most convenient, since it can match the rows and columns of your game. The below code creates a 3x3 list pre-filled with -1 for each item, to indicate the item hasn't been selected yet:

entries = [[-1 for x in range(3)] for y in range(3)] 

Items in a 2-dimensional list can be accessed by index, just like 1-d list items:

entries[0][0] = 2 # player 2 selected 1st row and 1st column # loop through entries in 2nd col for i in range(3): print(entries[i][1]) 

If you don't have the textbook, a full list of graphics.py methods is available at: https://github.com/MrAlex6204/Books/blob/master/Python%20Programming-An%20Introduction%20to%20Computer%20Science%202nd%20edition-John%20Zelle%202010.pdf (Links to an external site.)

Please upload .py file or use word, pdf ...

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 Databases Questions!