Nick Damore
Nick Damore

Reputation: 19

I need help making a Tic Tac Toe game. Python

Can I get some help making the computer's choice and finishing this program, I'm confused on what I have to do still... maybe a while loop at the end with the functions? I'm very confused on how to make the computer's choice. So far the player's choice, board and the wincheck probably works I just need everything else to work before I can test it out. For some reason when I try using 1 on the board it gives me a value error.

import random
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Computer = [1, 2, 3, 4, 5, 6, 7, 8, 9]
player = []
pc = []
wincheck = []
def WinCheck():
    for i in range(0,9):
        o = player.count[i]
        if o == 1:
            wincheck.append[i]
        else:
            continue

    if wincheck == [1,2,3]:
        print("You Win! The board will reset in 5 seconds!")
        reset()    
    elif wincheck == [4,5,6]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [7,8,9]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [1,4,7]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [2,5,8]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [3,6,9]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [1,5,9]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [3,5,7]:
        print("You Win! The board will reset in 5 seconds!")
        reset()


def draw():

    print ("\n" , "|",board[0],"|",board[1],"|",board[2], "|")
    print ("\n", "--------")
    print ("\n", "|",board[3],"|",board[4],"|",board[5], "|")
    print ("\n", "--------")
    print ("\n", "|", board[6],"|", board[7],"|", board[8], "|" , " \n")


#Resets the board
def reset():
    while True:
        board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        time.sleep(5)
        break

#Choosing a number from the board
def ChooseNum():
    while 1 == 1:
        while True:
            print('Where do you want to place you X')
            choice = input('')
            try:
                choice = int(choice)
            except ValueError:
                print("You didn't enter a number... Try again")
                break

            x = board.index(choice)
            Computer.remove(x)
            board.remove(choice)
            board.insert(x,'X')
            print(Computer)
            draw()




def CompChoice():
    while 1 == 1:
        while True:
            compchoice = random.choice(board)
            o = board.index(compchoice)
            Computer.remove(o)
            board.remove(compchoice)
            board.insert(o,'O')
            print(Computer)
            draw()

draw()
ChooseNum()

Upvotes: 1

Views: 455

Answers (1)

doctorlove
doctorlove

Reputation: 19282

Now you have a way to make a computer move, from my comment, notice this code:

Computer.remove(x)
board.remove(choice)

For one, you use the index x you found out, for the other you use the value.

remove removes "the first item from the list whose value is x. It is an error if there is no such item." This will give you a ValueError if it can't find the value you tell it to look for.

This gives your error. You need to remove the choice from the list:

Computer.remove(choice)
board.remove(choice)

You have a similar problem in the computer choice function. In order to get this to play tic-tac-toe with you you need to make a few changes. To point you on the way: remove the while loops from the functions, and use something like this to play:

draw()
while any(i not in ['X', 'O'] for i in board):
  ChooseNum()
  CompChoice()

You might find it easier if you just have one board - you are trying to maintain the same information in both your lists. You also need to check a move is legal - you can't go to a spot with an 'X' or 'O'. Fianlly, you might want to check if someone has won.

Upvotes: 1

Related Questions