4

I need some help with my tic-tac-toe game that I created in Python 3. Have a look at my fun program and try it out. After that, please help me creating a while statement in my program. That is while the user choice of square if filled, you should continue to ask them until they choose an empty square. When the choose an empty square, the program continues as before. I am not used to the while statements, please help me on this!

Here is my program:

from turtle import *

def setUp():
#Set up the screen and turtle
    win = Screen()
    tic = Turtle()
    tic.speed(10)
#Change the coordinates to make it easier to tranlate moves to screen coordinates:
    win.setworldcoordinates(-0.5,-0.5,3.5, 3.5)

#Draw the vertical bars of the game board:
    for i in range(1,3):
       tic.up()
       tic.goto(0,i)
       tic.down()
       tic.forward(3)

#Draw the horizontal bars of the game board:
    tic.left(90)    #Point the turtle in the right direction before drawing
    for i in range(1,3):
        tic.up()
        tic.goto(i,0)
        tic.down()
        tic.forward(3)

    tic.up()        #Don't need to draw any more lines, so, keep pen up

#Set up board:
    board = [["","",""],["","",""],["","",""]]

    return(win,tic,board)

def playGame(tic,board):
#Ask the user for the first 8 moves, alternating between the players X and O:
    for i in range(4):
        x,y = eval(input("Enter x, y coordinates for X's move: "))
        tic.goto(x+.25,y+.25)
        tic.write("X",font=('Arial', 90, 'normal'))
        board[x][y] = "X"

        x,y = eval(input("Enter x, y coordinates for O's move: "))                 
        tic.goto(x+.25,y+.25)
        tic.write("O",font=('Arial', 90, 'normal'))
        board[x][y] = "O"

# The ninth move:
    x,y = eval(input("Enter x, y coordinates for X's move: "))
    tic.goto(x+.25,y+.25)
    tic.write("X",font=('Arial', 90, 'normal'))
    board[x][y] = "X"

def checkWinner(board):
    for x in range(3):
        if board[x][0] != "" and (board[x][0] == board[x][1] == board[x][2]):
            return(board[x][0])  #we have a non-empty row that's identical
    for y in range(3):
        if board[0][y] != "" and (board[0][y] == board[1][y] == board[2][y]):
            return(board[0][y])  #we have a non-empty column that's identical
    if board[0][0] != "" and (board[0][0] == board[1][1] == board[2][2]):
        return(board[0][0])
    if board[2][0] != "" and (board[2][0] == board[1][1] == board[2][0]):
        return(board[2][0])   
    return("No winner")

def cleanUp(tic,win):
#Display an ending message: 
    tic.goto(-0.25,-0.25)
    tic.write("Thank you for playing!",font=('Arial', 20, 'normal'))

    win.exitonclick()#Closes the graphics window when mouse is clicked


def main():
    win,tic,board = setUp()   #Set up the window and game board
    playGame(tic,board)       #Ask the user for the moves and display
    print("\nThe winner is", checkWinner(board))  #Check for winner
    cleanUp(tic,win)    #Display end message and close window


main()
unor
  • 92,415
  • 26
  • 211
  • 360
socrates
  • 161
  • 1
  • 2
  • 10
  • 1
    This doesn't work, right? You're asking for X's move in a loop that runs four times, and then you ask for O's move once because it's outside the loop. The nice thing is, if you're X, you're always going to win. – Paul Cornelius Apr 30 '15 at 01:25

3 Answers3

0

You're probably looking for something like this:

x,y = None,None
while x == None or y == None or board[x][y] != "";
  x,y = eval(input("Enter x, y coordinates for X's move: "))

This will keep asking the user for input as long as x and y do not indicate an empty tile on the board.

By the way, you might consider changing the way you process the input. Right now you are using eval, which can be dangerous as any input can be executed. It might be better to process the input manually, something like this:

x,y = map(int,input("Enter coordinates").split(','))

This splits the input at the comma, turning it into a list of strings. map then applies the function int to each element in the list, turning them into integers. These are then unpacked into x and y.

0
for i in range(4):
    while True:
        move = input("Enter x, y coordinates for X's move: ")
        x,y = int(move[0]), int(move[-1]) # don't use eval()
        if board[x][y] not in ('X', 'O') # if valid
            tic.goto(x+.25,y+.25)
            tic.write("X",font=('Arial', 90, 'normal'))
            board[x][y] = "X"
            break # break out of loop after doing stuff
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

You might want to use a validation function taking the whole input string and parse it in the validation function, like the code below:

def isUserInputValid (s):
    cordinate=s.split(',')
    ...... # Your validation logic goes here



while not isUserInputValid(input("Please put your cell cordinate x, y:")):
        print("Your choice is not valid!")
Scormer
  • 62
  • 3