Python Forum
Tic-Tac-Toe player swap question
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic-Tac-Toe player swap question
#1
Hello,

I am writing a program for tic-tac-toe as an assignment for a class I am taking. At the moment I am trying to create a tic-tac-toe game using the turtle in pycharm. My intention is to have a game where I run the program and it starts by randomly choosing a player to start and then shows the turtle of the player that is chosen, once that player begins they can use the arrow keys to move the turtle around the board, and press space to draw either an X or an O and then it automatically ends their turn and begins the next players turn. This should continue until one or the other has made 3 in a row in which a winner is declared. So far I have created a 3x3 board in turtle, made a player turtle and given it the ability to move into the center of any of the 9 squares, and then on space it draws an x or an o.

My issue starts with switching the player my intention is to have a for loop that will run 5 times (that will give 10 turns and there are only 9 spaces), and within that for loop I wanted to have two while loops (one for each player). I have the player turtles defined so that I only need to invoke player_1 in the while loop and it creates a turtle that can be controlled in my program. Then I planned to have the while loops set on a true or false variable and whenever the space bar was pressed to put down the players mark on the board, the variable would be changed so that the while loop ends and moves to the next player. But I soon found out that the issue with this method is that my player turtle is infinitely recreated as the while command loops, so as the player tries to move the piece it is reset to its original state soon after.



I can see that what needs to happen is I need to find a way to make the while statement hold while it waits for a player to make a decision, but I do not know what sort of command would do this. Does anyone know how I could tell the program to wait on proceeding through the while statement until a choice is made in the turtle?

import turtle


Window = turtle.Screen()
Window.bgcolor()
Window.title("Tic Tac Toe")

# Drawing the Board
def border():
border_outer = turtle.Turtle()
border_outer.speed(0)
border_outer.penup()
border_outer.setposition(-375, -375)
border_outer.pendown()
border_outer.pensize(3)
for num in range(4):
border_outer.forward(750)
border_outer.left(90)
border_outer.hideturtle()

# inside border
border_inner1 = turtle.Turtle()
# border_inner1.color("red")
border_inner1.speed(0)
border_inner1.penup()
border_inner1.setposition(-125, 375)
border_inner1.pendown()
border_inner1.pensize(3)
border_inner1.right(90)
border_inner1.forward(750)
border_inner1.left(90)
border_inner1.forward(250)
border_inner1.left(90)
border_inner1.forward(750)
border_inner1.hideturtle()

border_inner2 = turtle.Turtle()
# border_inner2.color("red")
border_inner2.speed(0)
border_inner2.penup()
border_inner2.setposition(-375, 125)
border_inner2.pendown()
border_inner2.pensize(3)
border_inner2.forward(750)
border_inner2.right(90)
border_inner2.forward(250)
border_inner2.right(90)
border_inner2.forward(750)
border_inner2.hideturtle()

# Player 1 Turtle
def player_1():
# Player 1
global player
player = turtle.Turtle()
player.shape("circle")
player.penup()
player.color("blue")
player.speed()

playerspeed = 250

# Moving the player #1
def move_left():
x = player.xcor()
x -= playerspeed
if x < -375:
x = -250
player.setx(x)

def move_right():
x = player.xcor()
x += playerspeed
if x > 375:
x = 250
player.setx(x)

def move_down():
y = player.ycor()
y -= playerspeed
if y < -375:
y = -250
player.sety(y)

def move_up():
y = player.ycor()
y += playerspeed
if y > 375:
y = 250
player.sety(y)

def set_position():
x = player.xcor()
y = player.ycor()
player.goto(x, y - 50)
player.pendown()
player.pensize(10)
player.circle(50, None, None)
player.penup()
player.goto(x, y)
global flag
flag = False


# Keyboard Bindings for player 1
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
turtle.onkey(set_position, "space")

flag = 'false'

border()

while flag == True:
player_1


turtle.mainloop()
Reply


Forum Jump:

User Panel Messages

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