Tic-Tac-Toe with Python

Playing around with Python while reading the “Automate the Boring stuff with Python” book from Al Sweigart, I came down to code the Tic-Tac-Toe game, and part of the code has been provided by the book itself, which is the following part:

boardtable = {'1': ' ', '2': ' ', '3': ' ',
              '4': ' ', '5': ' ', '6': ' ',
              '7': ' ', '8': ' ', '9': ' '}

def printboard(board):
    print(board['1'] + '|' + board['2'] + '|' + board['3'])
    print('-+-+-')
    print(board['4'] + '|' + board['5'] + '|' + board['6'])
    print('-+-+-')
    print(board['7'] + '|' + board['8'] + '|' + board['9'])

turn = 'X'

for i in range(9):
    printboard(boardtable)
    print('Turn for ' + turn + '. Move on which space?')
    move = input()
    boardtable[move] = turn
    if turn == 'X':
        turn = '0'
    else:
        turn = 'X'

This is a good start, which provides the structure of the game, and gives you the possibility to start filling the fields of the board with X (player1) and 0 (player2). However, now you need to provide the logic of whether Player1 or Player2 won, or maybe a draw game.

Here’s the part of the code (Red highlighted) that I wrote myself, and seems to be working fine. Yes, lots of ‘If’ and ‘else’ statements, but it does the job for now.

boardtable = {'1': ' ', '2': ' ', '3': ' ',
              '4': ' ', '5': ' ', '6': ' ',
              '7': ' ', '8': ' ', '9': ' '}

def printboard(board):
    print(board['1'] + '|' + board['2'] + '|' + board['3'])
    print('-+-+-')
    print(board['4'] + '|' + board['5'] + '|' + board['6'])
    print('-+-+-')
    print(board['7'] + '|' + board['8'] + '|' + board['9'])

turn = 'X'
playerX = 'X'
playerO = '0'
counter = 0

for i in range(9):
    printboard(boardtable)
    print('Turn for ' + turn + '. Move on which space?')
    move = input()
    boardtable[move] = turn
    if turn == 'X':
        turn = '0'
    else:
        turn = 'X'
    counter+=1

    if counter >= 3:
        if boardtable['1'] == playerX and boardtable['2'] == playerX and boardtable['3'] == playerX:
            print('Congrats Player ' +playerX+ ', you won')
            break
        elif boardtable['4'] == playerX and boardtable['5'] == playerX and boardtable['6'] == playerX:
            print('Congrats Player ' + playerX + ', you won')
            break
        elif boardtable['7'] == playerX and boardtable['8'] == playerX and boardtable['9'] == playerX:
            print('Congrats Player ' +playerX+ ', you won')
            break
        elif boardtable['1'] == playerX and boardtable['4'] == playerX and boardtable['7'] == playerX:
            print('Congrats Player ' + playerX + ', you won')
            break
        elif boardtable['2'] == playerX and boardtable['5'] == playerX and boardtable['8'] == playerX:
            print('Congrats Player ' + playerX + ', you won')
            break
        elif boardtable['3'] == playerX and boardtable['6'] == playerX and boardtable['9'] == playerX:
            print('Congrats Player ' + playerX + ', you won')
            break
        elif boardtable['1'] == playerX and boardtable['5'] == playerX and boardtable['9'] == playerX:
            print('Congrats Player ' + playerX + ', you won')
            break
        elif boardtable['3'] == playerX and boardtable['5'] == playerX and boardtable['7'] == playerX:
            print('Congrats Player ' + playerX + ', you won')
            break
        elif boardtable['1'] == playerO and boardtable['2'] == playerO and boardtable['3'] == playerO:
            print('Congrats Player ' +playerO+ ', you won')
            break
        elif boardtable['4'] == playerO and boardtable['5'] == playerO and boardtable['6'] == playerO:
            print('Congrats Player ' + playerO + ', you won')
            break
        elif boardtable['7'] == playerO and boardtable['8'] == playerO and boardtable['9'] == playerO:
            print('Congrats Player ' +playerO+ ', you won')
            break
        elif boardtable['1'] == playerO and boardtable['4'] == playerO and boardtable['7'] == playerO:
            print('Congrats Player ' + playerO + ', you won')
            break
        elif boardtable['2'] == playerO and boardtable['5'] == playerO and boardtable['8'] == playerO:
            print('Congrats Player ' + playerO + ', you won')
            break
        elif boardtable['3'] == playerO and boardtable['6'] == playerO and boardtable['9'] == playerO:
            print('Congrats Player ' + playerO + ', you won')
            break
        elif boardtable['1'] == playerO and boardtable['5'] == playerO and boardtable['9'] == playerO:
            print('Congrats Player ' + playerO + ', you won')
            break
        elif boardtable['3'] == playerO and boardtable['5'] == playerO and boardtable['7'] == playerO:
            print('Congrats Player ' + playerO + ', you won')
            break

That is quite a long code, and I’m looking forward to change it by creating a Function with the necessary logic, at the end we will have a cleaner and shorter lines of code.



Categories: Python

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: