The Tic-Tac-Toe Game in Python

It's always fun to implement simple games and small projects after learning the basics of any programming language. Following is the implementation of the TicTacToe game in python.


theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,

            '4': ' ' , '5': ' ' , '6': ' ' ,

            '1': ' ' , '2': ' ' , '3': ' ' }


board_keys = []


for key in theBoard:

    board_keys.append(key)



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'])


# The main function 

def game():


    turn = 'X'

    count = 0



    for i in range(10):

        printBoard(theBoard)

        print("It's 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


        # To ckeck 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

                printBoard(theBoard)

                print("\nGame Over.\n")                

                print(" **** " +turn + " won. ****")

                break

            elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right 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's a Tie!!")


        # To change the player after every move.

        if turn =='X':

            turn = 'O'

        else:

            turn = 'X'        

    

    # To restart the game or not.

    restart = input("Do want to play Again?(y/n)")

    if restart == "y" or restart == "Y":  

        for key in board_keys:

            theBoard[key] = " "


        game()


if __name__ == "__main__":

    game()



Example of Output

 | | 
-+-+-
 | | 
-+-+-
 | | 
It's your turn,X.Move to which place?
8
 |X| 
-+-+-
 | | 
-+-+-
 | | 
It's your turn,O.Move to which place?
9
 |X|O
-+-+-
 | | 
-+-+-
 | | 
It's your turn,X.Move to which place?
5
 |X|O
-+-+-
 |X| 
-+-+-
 | | 
It's your turn,O.Move to which place?
3
 |X|O
-+-+-
 |X| 
-+-+-
 | |O
It's your turn,X.Move to which place?
2
 |X|O
-+-+-
 |X| 
-+-+-
 |X|O

Game Over.

 **** X won. ****
Do want to play Again?(y/n)n

Comments

Popular posts from this blog

Study of Differenet Network Types and Different Types of Network Cables and Practically Implement the Cross-Wired cable using Clamping Tool.

Error Detection and Correction Techniques

Challenges in e-governance