TIC-TAC-TOE using python

Hello everyone, here’s my version of tic-tac-toe using python, there are a few issues. Be brutal and provide your feedback. Also I am working on adding MINMAX algorithm to this game, so that there could be an AI that tackles the human.

import random

player = 'X'
player2 = 'O'
winner = None
gameRunning = True
winstatus = False
board = ['-','-','-',
#first setup the board 
def board_(board):
    # board = ['-','-','-',
    #          '-','-','-',
    #          '-','-','-']
    #board[6] = '-'
    print(board[0] + ' | ' + board[1] + ' | ' + board[2])
    print(board[3] + ' | ' + board[4] + ' | ' + board[5])
    print(board[6] + ' | ' + board[7] + ' | ' + board[8])
    #board[6] = '-'
    return board


#take the input
def input_(board):
    inp = int(input("Enter the position : "))
    board = board_(board)
    if inp >= 1 and inp <= 9:
        if board[inp-1] == '-':
            board[inp-1] = 'X'

        print('Please check the input value before playing the game')


#check winning situations for players
def checkHorizontal(board):
    if board[0] == board[1] == board[2] and board[0] != '-':
        print('YaY you won!!!')
        winstatus = True
        gameRunning = False
        #return True
    elif board[3] == board[4] == board[5] and board[3] != '-':
        winstatus = True
        gameRunning = False
    elif board[6] == board[7] == board[8] and board[6] != '-':
        winstatus = True
        gameRunning = False

def checkVertical(board):
    if board[0] == board[3] == board[6] and board[0] != '-':
        winstatus = True
        gameRunning = False
    elif board[1] == board[4] == board[7] and board[1] != '-':
        winstatus = True
        gameRunning = False
    elif board[2] == board[5] == board[8] and board[2] != '-':
        winstatus = True
        gameRunning = False

def checkDiagonal(board):
    if board[0] == board[4] == board[8] and board[0] != '-':
        winstatus = True
        gameRunning = False

    elif board[2] == board[4] == board[6] and board[2] != '-':
        winstatus = True
        gameRunning = False

def checkTie(board):
    for i in range(0,0):
        if board[i] != '-':
            print('Its a TIE')
            gameRunning = False

def switchPlayer(board):
    rand_ = random.randint(0,9)
    #board[rand_] = 'O'
    if board[rand_] != 'X' and board[rand_] == '-':
        board[rand_] = 'O'

while gameRunning:

I guess it’s a decent start.
You can definitly improve on conditions and loops though.
A rule of thumb is, if you write the same code 3 times, it propably should be a loop instead.
Basically all you win-statements are identical. But you wrote them out every single time.

Given the only actual difference is the index you check, you could put all important index-combinations into another array and have just one function check them and change the winstatus if necessary.
Like, imagine you want to improve this and add change the win-message - right now you have to change like 9 lines I guess?

Seems like you already got bored of that because the very first message is different.

Then ofcourse that’s some advanced stuff, but turning it all into an object would be a lot cleaner just by virtue of having a game-object instead of a bunch of functions and global variables floating around.

Your conditions could use some trimming. While my loop-example would obviously trim it all down - if you combined conditions with “or” instead of “if-elif”-chains, you’d also save yourself repeated code. Plus take the following snippet:

The first condition is redundant. If it is identical to " - " then it’s obviously different from " X ".

Also I might be wrong, but you don’t actually switch players, do you? It’s just adding a random O somewhere.

Finally, though that’s another step up: Take a look into the Pygame library. It’s a nice and extensive tool to make basic games and allows much more things to do with very little effort - including making an actual playing-field where users could click to add their symbols.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.