Tic Tac Toe

This forum is a place to ask questions about the python programming language.
Post Reply
User avatar
Andrew
New User
Posts: 2
Joined: 23 May 2018, 14:34

Tic Tac Toe

Post by Andrew » 05 Sep 2018, 21:50

My code won't let me fill in the last spot in the tic tac toe. I'm pretty sure it has to do with the while statement, but I'm also not sure how else to make sure the computer won't pick a number it has already picked before. Or one that the player has picked for that matter. Any suggestions?

Code: Select all

import random

# initialization stuff here: 

board = [1,2,3,4,5,6,7,8,9]
computerSpaces = []
humanSpaces =[]
game = 1

def drawPieces(position):
    if position in humanSpaces:
        return "X"
    elif position in computerSpaces:
        return "O"
    else:
        return position



# this function draws the board
def drawBoard():
    print("")
    print(" ",drawPieces('1')," |   ",drawPieces('2'),"    | ",drawPieces('3')," ")
    print("     |          |     ")
    print("----------------------")
    print(" ",drawPieces('4')," |   ",drawPieces('5'),"    | ",drawPieces('6')," ")
    print("     |          |     ")
    print("----------------------")
    print(" ",drawPieces('7')," |   ",drawPieces('8'),"    | ",drawPieces('9')," ")
    print("     |          |     ")
    return

# this function manages player moves
#doesn't do anything
def playerMove(move):
    return

# this function manages computer moves
#doesn't do anything
def computerMove(computerMove):
    return

while True:
    print(drawBoard())
    move = input("Please choose a move (99 to quit): ")
    if move == "99":
        break
    elif str(move) in computerSpaces or str(move) in humanSpaces:
        print("this is a invalid move")
    elif int(move) > 9 and int(move) < 99:
        print("you must pick a number from 1 to 9")
    else:
        humanSpaces.append(move)
        computerMove = random.randint(1,9)
        # computer picks its move
        while str(computerMove) in computerSpaces or str(computerMove) in humanSpaces:
                    computerMove = random.randint(1,9)
                    # this while statement checks for repeat moves and prevents them, it also prevents the player from making their last move
        computerSpaces.append(str(computerMove))
        print(computerSpaces)
2 x

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Tic Tac Toe

Post by Mr. MacKenty » 10 Sep 2018, 20:01

Andrew wrote:
05 Sep 2018, 21:50
My code won't let me fill in the last spot in the tic tac toe. I'm pretty sure it has to do with the while statement, but I'm also not sure how else to make sure the computer won't pick a number it has already picked before. Or one that the player has picked for that matter. Any suggestions?

Code: Select all

import random

# initialization stuff here: 

board = [1,2,3,4,5,6,7,8,9]
computerSpaces = []
humanSpaces =[]
game = 1

def drawPieces(position):
    if position in humanSpaces:
        return "X"
    elif position in computerSpaces:
        return "O"
    else:
        return position



# this function draws the board
def drawBoard():
    print("")
    print(" ",drawPieces('1')," |   ",drawPieces('2'),"    | ",drawPieces('3')," ")
    print("     |          |     ")
    print("----------------------")
    print(" ",drawPieces('4')," |   ",drawPieces('5'),"    | ",drawPieces('6')," ")
    print("     |          |     ")
    print("----------------------")
    print(" ",drawPieces('7')," |   ",drawPieces('8'),"    | ",drawPieces('9')," ")
    print("     |          |     ")
    return

# this function manages player moves
#doesn't do anything
def playerMove(move):
    return

# this function manages computer moves
#doesn't do anything
def computerMove(computerMove):
    return

while True:
    print(drawBoard())
    move = input("Please choose a move (99 to quit): ")
    if move == "99":
        break
    elif str(move) in computerSpaces or str(move) in humanSpaces:
        print("this is a invalid move")
    elif int(move) > 9 and int(move) < 99:
        print("you must pick a number from 1 to 9")
    else:
        humanSpaces.append(move)
        computerMove = random.randint(1,9)
        # computer picks its move
        while str(computerMove) in computerSpaces or str(computerMove) in humanSpaces:
                    computerMove = random.randint(1,9)
                    # this while statement checks for repeat moves and prevents them, it also prevents the player from making their last move
        computerSpaces.append(str(computerMove))
        print(computerSpaces)
Andrew,

Is this still a problem?

Warmly,

Mr. MacKenty
0 x

Post Reply