TicTacToe Python Check for Winner
<p>I'm new to programming and I'm having an issue with checking for a winner in a TicTacToe game.</p>

<p>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.</p>

<p>(there are 2 files, one called ticTacToeRunner &amp; ticTacToe, the first one is just to run the 2nd one).</p>

<p>here's what I currently have, any help is greatly appreciated:</p>

<pre><code>#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'])
</code></pre>

<p>Here's where I'm having problems, checking for the winner:</p>

<pre><code>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
</code></pre>

<p>And here is the loop for the game:</p>

<pre><code>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)
</code></pre>