0

I made a simple text-based tic-tac-toe game and now I am trying to step it up creating a GUI using the Turtle module. The problem is that when I run my program, the Turtle window draws the board correctly, but then freezes and stops responding. I tried searching this problem everywhere and some people said that it could have to do with the line screen.mainloop(), but I can't figure out what is wrong.

Here is my code:

import warnings
import numpy as np
from turtle import Turtle, Screen

warnings.simplefilter(action='ignore', category=FutureWarning)


def create_board():
    # turtle.hideturtle()
    turtle.speed(0)
    turtle.pensize(5)

    turtle.left(90)
    turtle.penup()
    turtle.setpos(-150, -450)
    turtle.pendown()
    turtle.forward(900)
    turtle.penup()
    turtle.setpos(150, -450)
    turtle.pendown()
    turtle.forward(900)

    turtle.right(90)
    turtle.penup()
    turtle.setpos(-450, -150)
    turtle.pendown()
    turtle.forward(900)
    turtle.penup()
    turtle.setpos(-450, 150)
    turtle.pendown()
    turtle.forward(900)


def enter_data(matrix, user, number):
    if user == 1:
        matrix = np.where(matrix == number, 'X', matrix)
        if is_winner(matrix):
            user = 1
        else:
            user = 2

    elif user == 2:
        matrix = np.where(matrix == number, 'O', matrix)
        if is_winner(matrix):
            user = 2
        else:
            user = 1

    return matrix, user


def show_board(matrix):
    print('-----|-----|-----')
    for i in range(3):
        print(f'  {matrix[i][0]}  |  {matrix[i][1]}  |  {matrix[i][2]}  ')
        print('-----|-----|-----')
    return


def is_winner(matrix):
    for i in range(3):
        if matrix[0][i] == matrix[1][i] == matrix[2][i]:
            return True
        elif matrix[:, 0][i] == matrix[:, 1][i] == matrix[:, 2][i]:
            return True
    if matrix[0][0] == matrix[1][1] == matrix[2][2]:
        return True
    elif matrix[0][2] == matrix[1][1] == matrix[2][0]:
        return True
    else:
        return False


def draw_circle(x, y):
    turtle.penup()
    turtle.goto(x, y + 60)
    turtle.pendown()
    turtle.circle(-60)


def draw_cross(x, y):
    turtle.penup()
    turtle.goto(x - 60, y - 60)
    turtle.pendown()
    turtle.goto(x + 60, y + 60)
    turtle.penup()
    turtle.goto(x - 60, y + 60)
    turtle.pendown()
    turtle.goto(x + 60, y - 60)


screen = Screen()
screen.setup(width=0.65, height=0.95, starty=0)
turtle = Turtle()

create_board()

array = np.array(np.mat('1 2 3; 4 5 6; 7 8 9'), dtype=object)

count = 0
player = 1

show_board(array)

while count < 9 and is_winner(array) is not True:

    choice = int(input(f'\nPlayer {player}, enter the number you want:\n'))

    if choice not in range(1, 10) or choice not in array:
        print('\nThat is invalid. Try again.\n')
        show_board(array)
    else:
        count += 1
        array, player = enter_data(array, player, choice)
        screen.update()
        show_board(array)

if count == 9:
    print("\nThat's a draw.")
else:
    print(f'\nPlayer {player} won! Congratulations!!')

print('Game Over!')

screen.mainloop()

Any help would be very much appreciated.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 2
    `input()` is not intended for use in a GUI - its prompt is going to appear in the terminal you launched the GUI from, which may or may not actually exist. `turtle` has a `numinput()` method for requesting input of a number via a GUI dialog box. Likewise, your `print()`s may or may not actually be visible to the user. – jasonharper Aug 09 '22 at 00:57
  • Also, `draw_cross()` and `draw_circle()` are never called, so you have a fair amount of additional work to do to wire your GUI to your game. `numinput` is nice to know about but probably poor UX for a TTT game. Better to allow the user to click a square, then inform the back-end game engine about the move and update the UI accordingly. There are many TTT Py turtles out there, so skimming [existing questions](https://stackoverflow.com/questions/tagged/tic-tac-toe+turtle-graphics+python) might offer some strategies. – ggorlen Aug 09 '22 at 17:40

0 Answers0