Skip to content Skip to sidebar Skip to footer

Tic Tac Toe WinCheck Def

Hi I'm having some trouble with coding tic tac toe game where the computer plays randomly by choosing numbers between 0 and 1. I tried a code but I get unreal situation like this o

Solution 1:

Code:

import random
board = []
for x in range(9):
    board.append(" ")
def show():
    print(board[0],'|',board[1],'|',board[2])
    print('--------------')
    print(board[3],'|',board[4],'|',board[5])
    print('--------------')
    print(board[6],'|',board[7],'|',board[8])
def check(A, B, C, D):
    if board[B] == A and board[C] == A and board[D] == A :
        return True 
def Winner(G):
    ret = False
    if  check(G, 0, 1, 2):
            ret = True 
    if  check(G, 0, 3, 6):
            ret = True 
    if  check(G, 0, 4, 8):
            ret = True
    if  check(G, 1, 4, 7):
            ret = True             
    if  check(G, 2, 5, 8):
            ret = True         
    if  check(G, 2, 4, 6):
            ret = True     
    if  check(G, 3, 4, 5):
            ret = True 
    if  check(G, 6, 7, 8):
            ret = True
    return ret
def filled():
    ll = 0
    for x in board:
        if x == " ":
            ll = 11 + 1
    if ll > 0:
        return False
    else:
        return True
done = False
turn = "O"
tie = False
while done == False:
    place = random.randint(0, 8)
    if board[place] == " ":
        board[place] = turn
        if Winner(turn) == True:
            done = True
        else:
            if filled() == True:
                done = True
                tie = True
            else:#change turn
                if turn == "X":
                    turn = "O"
                else:
                    turn = "X"
if tie == True:
    print ("The game was a tie. No winner.")
    show()
else:
    print("~~~~~~~ the player '",turn,"' wins~~~~~~~")
    show()

This should complete all your requirements.

Just check for winner() and filled() after each valid turn. have the loop change whos turn it is after every successful loop, to prevent duplication.

NOTE: 'O' goes first, to change that change variable turn to 'X' before the loop begins.


Solution 2:

Your Winner() function doesn't return anything, so by default it implicitly returns False no matter what the inputs are. It should include a return True instead of just True (which does nothing at all), something like:

def Winner(G):
    if check(G, 0, 1, 2) or check(G, 0, 3, 6) or check(G, 0, 4, 8) or check(G, 1, 4, 7) or check(G, 2, 5, 8) or check(G, 2, 4, 6) or check(G, 3, 4, 5) or check(G, 6, 7, 8):
        return True
    else
        return False

The main point is that is should return True. The rest is mainly just style preference - I prefer a single if, with a lot of or conditions. Others may prefer a lot of if statements all with return True.

(Technically, you don't need the else part, as it will effectively return False by default anyhow. But it makes it more readable. You could also remove just the else line, and reverse-indent the return False which makes it a catch all for the entire function rather than for the if.)


Post a Comment for "Tic Tac Toe WinCheck Def"