Hey guys.
I'm a college student who just took a introduction to programming course for general credit. We are learning how to program with python. I am having real trouble with creating a graphical tic tac toe game. I am just a beginner and any help would be appreciated. This is what we have to do:

You will write a graphical Tic Tac Toe game. Your game should open a window, display a grid, and allow an "O" player and an "X" player to alternately click the mouse on the grid. Your program will then draw the appropriate symbol, check for victory, and then allow the other player to play. Your game will end when either player has won, or the whole board is filled up.

Steps
These are the steps you can follow:
1. Create the variables. All square variables should be set to " " and the player to "X".
2. Import graphics, make a GraphWin, and draw the four lines to make the grid.
3. Loop steps 3 through 11 using a while True: statement
4. Wait on a mouse click. Get the x and y position of the mouse.
5. Use the x and y position of the mouse to figure out which square (1 through 9) was clicked.
6. Check if the square is empty, and only do steps 311
if it is.
7. Based on player, draw an X or O in the correct square. An X should be draw with two lines, and
an O with an oval.
8. Set the appropriate square variable to "X" or "O"
9. If the player is "X", change it to "O". If the player is "O", change it to "X".
10. Check if a player has won. There are eight winning combinations (three rows, three columns,
and two diagonals). Check if any of these have happened. If so, set a won variable to the
winning player, and call break to leave the while loop.
11. Check if all squares are filled (not " "). If so, set won to nobody, and call break to leave the
while loop.
12. Draw a Text line in the middle of the screen. Using the won variable, print "X Wins", "O
Wins", or "Tie".

Thank You.

Recommended Answers

All 3 Replies

Is this a "do-my-homework" request ?
Please show us what you already came up with ...

Edit:: There are just so many TicTacToe examples floating around on the internet just use Google ...

This is what I have right now, i'm still trying to figure things out.

import graphics
import random

win=graphics.GraphWin("Tic Tac Toe",500,500)

win.setCoords(0.0,0.0,3.0,3.0)

line1=graphics.Line(graphics.Point(1,0), graphics.Point(1,3)).draw(win)
line2=graphics.Line(graphics.Point(2,0), graphics.Point(2,3)).draw(win)

line3=graphics.Line(graphics.Point(0,1), graphics.Point(3,1)).draw(win)
line4=graphics.Line(graphics.Point(0,2), graphics.Point(3,2)).draw(win)

while True:
click=win.getMouse()

print "("+str(click.getX())+","+str(click.getY())+")"

x=click.getX()
y=click.getY()

radius=random.randint(5,20)

topleft = graphics.Point(x-radius,y-radius)
bottomright = graphics.Point(x+radius,y+radius)

circle = graphics.Oval(topleft,bottomright)

circle.setFill("red")

circle.draw(win)

OK, but your first step should actually be to draw the playing field, then you program the mouse events (when a place on the field is clicked), and after that the actual game :)

By the way, please post using code tags ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.