Coders Packet

Simple Tic-Tac-Toe Game using Python

By Pragya Mukherjee

Here is a simple and quick Tic-Tac-Toe game in python using simple concepts. The core concepts are dictionaries, functions, and loops.

The Tic-Tac-Toe game is one of the most common and popular games that most of us used to play in our childhood days. I have tried my best to simplify the code of the basic tic-tac-toe game. The core concepts used here are dictionaries, loops, and functions. I hope this code will be easy for anyone with basic knowledge of Python programming language, to understand. 

So, let's get started.

Make sure you have your machine prepared with the latest version of Python. You can use any of your favorite editors or IDE for this project.

Let's take a quick look at the dictionary representing the Tic-Tac-Toe board.

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

Here, is the basic structure of our game board. As you know, the Tic-Tac-Toe board looks something like this (refer to the picture below).

board 

Well, I am pretty sure the dictionary is crystal clear to you!! The value of each of these positions has been assigned a value of a space(' '). Later according to the user input, the values will be updated with turns -X/0.

Now we will declare a function for the conditions to be a winner - diagonal match, horizontal match, and vertical match. Take a deep look at the function.

def condition_to_win():

    if board['top-L'] == board['top-M'] and board['top-M'] == board['top-R'] and board['top-M'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['top-L']))
        flag =0
    elif board['mid-L'] == board['mid-M'] and board['mid-M'] == board['mid-R'] and board['mid-M'] != " ":
        
        print("\nplayer with turn {} is the winner!!\n\n".format(board['mid-L']))
        flag =0
    elif board['low-L']== board['low-M'] and board['low-M'] == board['low-R']and board['low-M'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['low-L']))
        flag =0
    elif board['top-L'] == board['mid-L'] and board['mid-L'] == board['low-L']and board['top-L'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['top-L']))
        flag =0
    elif board['top-M'] == board['mid-M'] and board['mid-M'] == board['low-M']and board['top-M'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['top-M']))
        flag =0
    elif board['top-R'] == board['mid-R'] and board['mid-R'] == board['low-R']and board['top-R'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['top-R']))
        flag = 0
    elif board['top-L'] == board['mid-M'] and board['mid-M'] == board['low-R']and board['top-L'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['top-L']))
        flag = 0
    elif board['top-R'] == board['mid-M'] and board['mid-M'] == board['low-L']and board['top-R'] != " ":
        
        print("\nplayer with turn {} is the winner !!\n\n".format(board['top-R']))
        flag =0
    else:
        flag = 1

    return flag

Well this function with some bunch of if-else conditions, is self-explanatory. 

After this, the for loop iterates for 9 times, takes user inputs and evaluates to the final result (as which turn wins->X/0) after checking the necessary winning conditions. Note I took a 'flag' variable to denote 0 value as 'game over' after having some winner, so that we can 'break' out of the loop declaring the winner. I highly recommend you to download the source code and run it in your machine.

And there you go!! Congratulations for having your first Tic-Tac-Toe game with Python !!!

Download project

Reviews Report

Submitted by Pragya Mukherjee (MPragya)

Download packets of source code on Coders Packet