board = [['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.']]
pieces = ['X', 'O']
movetype = [0, 1, 2]
turn = 0
breakgame = 0

def movecheck():
    if x in movetype:
        if y in movetype:
            if board[x][y] in pieces:
                print 'Invalid move!'
            else:
                board[x][y] = pieces[turn]
                turncheck()
        else:
            print 'Invalid move!'
    else:
        print 'Invalid move!'

def turncheck():
    global turn
    if turn == 0:
        turn = 1
    else:
        turn = 0  



while True:
    move = raw_input('Player '+str(turn+1)+' ('+pieces[turn]+'): ').split()
    x = int(move[0])
    y = int(move[1])
    movecheck()
    if breakgame == 1:
        break
    for i in board:
        ' '.join(i)
        print i

How do I write a check to see if the player has won?

How does the termination condition become realized? Is there limit of maximum number of turns?

You have to have a separate list of winning combinations, so one of the combinations would be ([0,0], [1,0], [2,0]) which would be 3 down if all 3 positions are either "X" or "O". Another way to do this is to number the squares and store each position in a dictionary with the key being the square number. Then you would use a separate list of square numbers to find a winning position. As stated above, you also have to know when all of the squares are filled, without anyone winning, to end the game. Note that you can "and" if statements if you want. Either way is fine.

def movecheck():
    if (x in movetype) and (y in movetype):
        if board[x][y] in pieces:
            print 'Invalid move!'
        else:
            board[x][y] = pieces[turn]  ## does this function know what "turn" is??
            turncheck()
            #----- and turncheck() can be eliminated by
            turn = not turn   ## but this uses "True"/"False" not one or zero
    else:
        print 'Invalid move!'

See here for info on how to use functions.

Edited 6 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.