Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic Tac Toe victory
#1
So i have been making slow progress on my tic tac toe im still missing some stuff bot I can do that by myself but I dont know how to make a victory.
So I need some help with writing Victory if 3 rectangles or 3 O are connected.

import tkinter
canvas=tkinter.Canvas(width=700, height=700)
canvas.pack()
def board(): #definuje hraciu plochu
    canvas.create_line(50,150,350,150)
    canvas.create_line(150,50,150,350)
    canvas.create_line(250,50,250,350)
    canvas.create_line(50,250,350,250)
    canvas.create_rectangle(50,50,350,350)

def get_coordinates (x, y): #zistí polohu kliknutia a položí dany predmet do stredu štvorca 
    if x > 49 and x < 350 and y > 49 and y < 350 :
        x = ((x - 150) // 100 * 100) + 170 
        y = ((y - 150) // 100 * 100) + 170 
        return x, y
    else :
        return 0, 0#ak sa klikne mimo hracej plochy tak nevykreslí nič

def kruh(sur):
    x, y = get_coordinates (sur.x, sur.y) 
    if x > 0 :
        canvas.create_oval(x,y,x+60,y+60, fill='blue')
 

def stvorec(sur):
    x, y = get_coordinates (sur.x, sur.y) 
    if x > 0 :
        canvas.create_rectangle(x,y,x+60,y+60,fill='red')


def resetAll():
    canvas.delete('all')
    board()

def start():
    canvas.delete('all')
    canvas.bind('<Button-3>', stvorec)
    canvas.bind('<Button-1>', kruh)
    board()
    reset=tkinter.Button(text='RESET', command=resetAll)
    reset.pack()
    start.pack_forget()

start=tkinter.Button(text='START', command=start)
start.pack()
Reply
#2
To identify a winner you need to record what markers are in what spots. If you have that it is easy to identify a winning move because there are only 8 winning board combinations; 3 rows, 3 columns, 2 diagonals.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020