I've just started a self-learning "course" on Python practical programming for beginners. I found this book online, and decided to go through the chapters.

The book had a half-complete implementation of a Tic-Tac-Toe game, and I was supposed to finish it as an exercise. This is my code so far:

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

def printBoard(board):
    """ This function prints the board after every move """

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

def checkWin(board):
    This functions checks if the win condition has been
    reached by a player
    flag = False
    possibleWins = [['top-L', 'top-M', 'top-R'],
                    ['mid-L', 'mid-M', 'mid-R'],
                    ['low-L', 'low-M', 'low-R'],
                    ['top-L', 'mid-L', 'low-L'],
                    ['top-M', 'mid-M', 'low-M'],
                    ['top-R', 'mid-R', 'low-R'],
                    ['top-L', 'mid-M', 'low-R'],
                    ['top-R', 'mid-M', 'low-L']]

    for row in range(len(possibleWins)):
        temp = board[possibleWins[row][0]]
        if temp != ' ':
            for position in possibleWins[row]:
                if board[position] != temp:
                    flag = False
                    flag = True
            if flag:
                return True

    return False           

turn = 'X'
for i in range(9):
    print('Turn for ' + turn + '. Move on which space?')
    while True:
        move = input()
        if move in theBoard:
            if theBoard[move] != ' ':
                print('Invalid move. Try again.')
            print('Invalid move. Try again.')
    theBoard[move] = turn
    if checkWin(theBoard):
        print('Player ' + turn + ' wins!')
    if turn == 'X':
        turn = 'O'
        turn = 'X'

My checkWin function is very "stupid", it detects a win based on predetermined scenarios, and may not be very efficient in that regard as well. What if the board was of an arbitrary size nxn? Is there an algorithm to determine the victory condition without having to rewrite the entire game?

  • 3
    \$\begingroup\$ Something's wrong with your formatting... I tried to fix it, but there is a return False right in the middle that I'm not 100% sure how to resolve. Please review how your code is formatted, because as it is, it's not valid Python \$\endgroup\$
    – janos
    Jul 13 '15 at 20:15
  • \$\begingroup\$ re-formatted, let me know if there is anything wrong again \$\endgroup\$ Jul 13 '15 at 21:40

For a 3x3 tic-tac-toe board it hardly provides any real advantage, but if you where to play on an nxn board, with n consecutive same player marks determining a winner, you could do the following...

class TicTacToe(object):
    def __init__(self, n=3):
        self.n = n
        self.board = [[0 for j in range(n)] for k in range(n)]
        self.row_sum = [0 for j in range(n)]
        self.col_sum = [0 for j in range(n)]
        self.diag_sum = 0
        self.diag2_sum = 0

    def add_move(self, player, row, col):
        assert player in (0, 1)
        assert 0 <= row < self.n and 0 <= col < self.n
        delta = [-1, 1][player]
        winner = None
        self.board[row][col] = delta
        self.row_sum[row] += delta:
        if self.row_sum[row] = delta * self.n:
            winner = player
        self.col_sum[col] += player:
        if self.col_sum[col] = player * self.n:
            winner = player
        if col == row:
            self.diag += delta
            if self.diag == delta * self.n:
                winner = player
        if row == self.n - row - 1:
             self.diag2 += delta
             if self.diag2 == delta* self.n:
                 winner = player
        return winner

You basically initialize the board to all zeros, use -1 for one player and 1 for the other, and keep track of the sum of values in each row, column and diagonal. Whenever you add a new move, you can update all your data in constant time, and check in constant time if the player has won.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.