HOW TO CREATE TIC TAC TOE GAME USING PYTHON

HOW TO CREATE TIC TAC TOE GAME USING PYTHON


Tic-tac-toe, noughts and crosses, or Xs and Os/“X’y O’sies”, is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a diagonal, horizontal, or vertical row is the winner. 


To create tic tac toe game using python follow the following step:-

open any python ide

type foolowing code:

#implementation of two player tic-tac toe game in python

#we will make the board using dictionary

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

board_keys = []

for key in theBoard:
    board_keys.append(key)

#we have to print the updated board after every move in the game

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

#Now we'll write the main function which has all the gameplay functionality

def game():

    turn = 'X'
    count = 0

    for i in range(10):
        printBoard(theBoard)
        print('It is your turn,'+ turn + '.Move to which place?')

        move = input()

        if theBoard[move]==' ':
            theBoard[move]=turn
            count+=1
        else:
            print('That place is already filled. /nMove to which place?')
            continue

        #Now we will check if player X or O has won ,for every move after 5 moves
        if count>= 5:
            if theBoard['7'] == theBoard['8'] == theBoard['9']:         #across the top
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['4'] == theBoard['5'] == theBoard['6']:         #across the middle
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['1'] == theBoard['2'] == theBoard['3']:         #across the bottom
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['1'] == theBoard['4'] == theBoard['7']:         #down the left side
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['2'] == theBoard['5'] == theBoard['8']:         #down the middle side
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['3'] == theBoard['6'] == theBoard['9']:         #down the last side
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['7'] == theBoard['5'] == theBoard['3']:         #diagonal
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break
            elif theBoard['1'] == theBoard['5'] == theBoard['9']:         #diagonal
                printBoard(theBoard)
                print('\nGame Over.\n')
                print('****' +turn + 'won.****')
                break

        #if neither X nor O wins and the board is full, declare the result as 'tie'
        if count == 9:
            print('\nGame Over.\n')
            print('It is a Tie!!')

        #Now we have to change the player after every move.
        if turn == 'X':
            turn= 'O'
        else:
            turn = 'X'

        # Now we will ask if player wants to restart the game or not.
    restart = input('Do you want to play again?(Y/N)')
    if restart == 'y' or restart == 'Y':
        for key in board_keys:
            theBoard[key]== ''

        game()

if __name__ == '__main__':
    game()
            
    To download full python file click here

    To watch full tutorial video click here
    
    






Comments

Popular posts from this blog

MAKING CALCULATOR USING PYTHON PROGRAMMING

IPHONE 14 FULL REVIEW