Python Forum

Full Version: Got error in Tic Tac Toe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I somehow created Tic tac toe (not finished yet). when I place 'X' it place 'X' properly, and when I place 'O' it place 'O' but it also change 'X' to 'O' and then it continue to change.
import pygame
import random

pygame.init()

#Window dimention
width = 600
hight = 600

#color
black = (0, 0, 0)
white = (250, 250, 250)

#block and line position and variables
mouse_pos = (700,700)
linex1 = 200
linex2 = 400
liney1 = 200
liney2 = 400
block1 = False
block2 = False
block3 = False
block4 = False
block5 = False
block6 = False
block7 = False
block8 = False
block9 = False


display = pygame.display.set_mode((width, hight))
num = 1

def draw(x_pos, y_pos):
    global num
    if num % 2 == 0:
        pygame.draw.circle(display, black, (x_pos, y_pos), 75, 15)
        num += 1
    elif num % 2 != 0:
        a = x_pos + 70
        b = y_pos - 70
        c = x_pos - 70
        d = y_pos + 70
        pygame.draw.line(display, black, (a, b), (c, d), 15)
        a = x_pos - 70
        b = y_pos - 70
        c = x_pos + 70
        d = y_pos + 70
        pygame.draw.line(display, black, (a, b), (c, d), 15)
        num += 1

game_over = False
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if event.type == pygame.MOUSEBUTTONUP:           #to get mouse positon when click
            mouse_pos = pygame.mouse.get_pos()
            if mouse_pos[0] <= linex1 and mouse_pos[1] <= liney1:
                print('Block 1')
                block1 = True
            elif mouse_pos[0] >= linex1 and mouse_pos[0] <= linex2 and mouse_pos[1] <= liney1:
                print('Block 2')
                block2 = True
            elif mouse_pos[0] >= linex2 and mouse_pos[1] <= liney1:
                print('Block 3')
                block3 = True
            elif mouse_pos[0] <= linex1 and mouse_pos[1] >= liney1 and mouse_pos[1] <= liney2:
                print('Block 4')
                block4 = True
            elif mouse_pos[0] >= linex1 and mouse_pos[1] >= liney1 and mouse_pos[0] <= linex2 and mouse_pos[1] <= liney2:
                print('Block 5')
                block5 = True
            elif mouse_pos[0] >= linex2 and mouse_pos[1] >= liney1 and mouse_pos[1] <= liney2:
                print('Block 6')
                block6 = True
            elif mouse_pos[0] <= linex1 and mouse_pos[1] >= liney2:
                print('Block 7')
                block7 = True
            elif mouse_pos[0] >= linex1 and mouse_pos[1] >= liney2 and mouse_pos[0] <= linex2:
                print('Block 8')
                block8 = True
            elif mouse_pos[0] >= linex2 and mouse_pos[1] >= liney2:
                print('Block 9')
                block9 = True


    display.fill(white)
    #lines
    line1 = pygame.draw.line(display, black, (200, 0), (200, 600), 2)
    line4 = pygame.draw.line(display, black, (400, 0), (400, 600), 2)
    line3 = pygame.draw.line(display, black, (0, 200), (600, 200), 2)
    line4 = pygame.draw.line(display, black, (0, 400), (600, 400), 2)

    if block1 == True:
        draw(100, 100)
    if block2 == True:
        draw(300, 100)
    if block3 == True:
        draw(500, 100)
    if block4 == True:
        draw(100, 300)
    if block5 == True:
        draw(300, 300)
    if block6 == True:
        draw(500, 300)
    if block7 == True:
        draw(100, 500)
    if block8 == True:
        draw(300, 500)
    if block9 == True:
        draw(500, 500)

    pygame.display.update()


pygame.quit()
quit()
I'm not able to interpret what is issue but if you run this code you will know. does anyone know how to fix this problem
You have a few bad habits as well as over-complicated processes.

bad habits:
You should not be using global keyword. You are asking for bugs.

over complicated:
It would be better to have 0 as O and 1 as X as a boolean, or whatever. Even better than that it would be better to have a dictionary or list of the positions and set them and then display them in their position for whatever the position's content is.

problem:
Your draw function is ran every iteration of the loop. This, in conjunction with you using global, means it flips back and forth for everyone, instead of how you intend it. Instead, once you set one save it in a dictionary/list, then simply just draw the entire dictionary/list.

fix with dictionary:
Which if it was a dictionary... key can be position while the value identifies it to be X or O. Set all the keys to be the positions with values set to None in the beginning. Draw the entire dictionary. Those that do not have an X or O value do not draw anything, but still retain the position in advance. When someone clicks on a square assign that position to be X or O only if it has none.