简体   繁体   中英

TicTacToe Python Check for Winner

I'm new to programming and I'm having an issue with checking for a winner in a TicTacToe game.

My problem is the def printBoard(board):, where it does not properly check for a winner and the game keeps running even after someone wins.

(there are 2 files, one called ticTacToeRunner & ticTacToe, the first one is just to run the 2nd one).

here's what I currently have, any help is greatly appreciated:

#ticTacTorRunner file

import ticTacToe

theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

ticTacToe.startGame('X', theBoard)



#ticTacToe file

def printBoard(board):

     print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
     print('-+-+-')
     print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
     print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])

Here's where I'm having problems, checking for the winner:

def checkWinner(board, player): 
 print('Checking if ' + player + ' is a winner...')

#X WIN

if 'top-L' == 'X' and 'top-M' == 'X' and 'top-R' == 'X':
    return True

     elif board == """{'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
            'mid-L': 'X', 'mid-M': 'X', 'mid-R': 'X',
            'low-L': 'O', 'low-M': 'O', 'low-R': ' '}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'X|O| ',
            'mid-L': 'X|O| ', 'mid-M': 'X|O| ', 'mid-R': 'X|O| ',
            'low-L': 'X', 'low-M': 'X', 'low-R': 'X'}""":
         return True


     elif board == """{'top-L': 'X', 'top-M': 'X|O| ', 'top-R': 'X|O| ',
            'mid-L': 'X', 'mid-M': 'X|O| ', 'mid-R': 'X|O| ',
            'low-L': 'X', 'low-M': 'X|O| ', 'low-R': 'X|O| '}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X', 'top-R': 'X|O| ',
            'mid-L': 'X|O| ', 'mid-M': 'X', 'mid-R': 'X|O| ',
            'low-L': 'X|O| ', 'low-M': 'X', 'low-R': 'X|O| '}""":
         return True

     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'X',
            'mid-L': 'X|O| ', 'mid-M': 'X|O| ', 'mid-R': 'X',
            'low-L': 'X|O| ', 'low-M': 'X|O| ', 'low-R': 'X'}""":
         return True


     elif board == """{'top-L': 'X', 'top-M': 'X|O| ', 'top-R': 'XX|O| ',
            'mid-L': 'X|O| ', 'mid-M': 'X', 'mid-R': 'X|O| ',
            'low-L': 'X|O| ', 'low-M': 'X|O| ', 'low-R': 'X'}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'X',
            'mid-L': 'X|O| ', 'mid-M': 'X', 'mid-R': 'X|O| ',
            'low-L': 'X', 'low-M': 'X|O| ', 'low-R': 'X|O| '}""":
         return True



#O WIN

     elif board == """{'top-L': 'O', 'top-M': 'O', 'top-R': 'O',
            'mid-L': 'X|O| ', 'mid-M': 'X|O| ', 'mid-R': 'XX|O| ',
            'low-L': 'X|O| ', 'low-M': 'X|O| ', 'low-R': 'X|O| '}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'X|O| ',
            'mid-L': 'O', 'mid-M': 'O', 'mid-R': 'O',
            'low-L': 'X|O| ', 'low-M': 'X|O| ', 'low-R': 'X|O| '}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'X|O| ',
            'mid-L': 'X|O| ', 'mid-M': 'X|O| ', 'mid-R': 'X|O| ',
            'low-L': 'O', 'low-M': 'O', 'low-R': 'O'}""":
         return True


     elif board == """{'top-L': 'O', 'top-M': 'X|O| ', 'top-R': 'X|O| ',
            'mid-L': 'O', 'mid-M': 'X|O| ', 'mid-R': 'X|O| ',
            'low-L': 'O', 'low-M': 'X|O| ', 'low-R': 'X|O| '}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'O', 'top-R': 'X|O| ',
            'mid-L': 'X|O| ', 'mid-M': 'O', 'mid-R': 'X|O| ',
            'low-L': 'X|O| ', 'low-M': 'O', 'low-R': 'X|O| '}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'O',
            'mid-L': 'X|O| ', 'mid-M': 'X|O| ', 'mid-R': 'O',
            'low-L': 'X|O| ', 'low-M': 'X|O| ', 'low-R': 'O'}""":
         return True


     elif board == """{'top-L': 'O', 'top-M': 'X|O| ', 'top-R': 'XX|O| ',
            'mid-L': 'X|O| ', 'mid-M': 'O', 'mid-R': 'X|O| ',
            'low-L': 'X|O| ', 'low-M': 'X|O| ', 'low-R': 'O'}""":
         return True


     elif board == """{'top-L': 'X|O| ', 'top-M': 'X|O| ', 'top-R': 'O',
            'mid-L': 'X|O| ', 'mid-M': 'O', 'mid-R': 'X|O| ',
            'low-L': 'O', 'low-M': 'X|O| ', 'low-R': 'X|O| '}""":
         return True


     else:
         return False

And here is the loop for the game:

def startGame(startingPlayer, board):

     turn = startingPlayer
     for i in range(9):
         printBoard(board)
         print('Turn for ' + turn + '. Move on which space?')
         move = input()
         board[move] = turn

         if ( checkWinner(board, 'X') ):
             print('X wins!')
             break
         elif ( checkWinner(board, 'O') ):
             print('O wins!')
             break

         if turn == 'X':
             turn = 'O'
         else:
             turn = 'X'

     print (board)

You're comparing the board, which is a python dict , against a string representation of a dict, which will never pass. Remove the triple-quotes from around your if conditions and it may work.

I will add that you've picked a horrendous way to represent a tic-tac-toe board; I'd suggest using a list-of-lists instead, which is a lot more compact, lets you access board positions as board[0][0] to board[2][2] instead, and check your conditions using loops instead of listing out each one separately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM