Siz tutorials || Tic-Tac-Toe in python|| by @aqsaejaz

in Steem Infinity Zone3 years ago (edited)

20% to siz-official

Tic-Tac-Toe using python

Assalamualaikum! Hello everyone, i hope y'all are fine and great. I thought and researched various topics to post and somehow came up with this tutorial. When i first cracked the code for this game, it was quite fun and I really enjoyed writing this code. This program will clear all your misunderstandings and will help you to create more games and write different programs. So lets get started,

image.png
Source

Step 1:

First of all, we'll create a board or pattern for the game with the help of dictionary.

board = {"1": " ", "2": " ", "3": " ", "4": " ", "5": " ", "6": " ", "7": " ", "8": " ", "9": " "}
board_key = []
player = 1
totalmoves = 0
endcheck = 0

Step 2:

Now we'll create a function. In this function we will create a loop for the board and the loop will break after total moves.

def tictactoe():
    totalmoves = 0
    player = 1

    r1 = ("1|2|3")
    r2 = "4|5|6"
    r3 = ("7|8|9")

    print(r1)
    print("-+-+-")
    print(r2)
    print("-+-+-")
    print(r3)

    while True:
        print(board["1"] + "|" + board["2"] + "|" + board["3"])
        print("-+-+-")
        print(board["4"] + "|" + board["5"] + "|" + board["6"])
        print("-+-+-")
        print(board["7"] + "|" + board["8"] + "|" + board["9"])

        endcheck = check()
        if totalmoves == 9 and endcheck == 0:
            print("Game Draw :( ")
            break

        if totalmoves == 9 or endcheck == 1:
            break

Step 3:

Now, in this step we'll create a loop for player's turn using an if else condition.

        while True:
            if player == 1:
                player1_input = input("Its your turn player1. Move to which place? ")
                if player1_input in board and board[player1_input] == " ":
                    board[player1_input] = "X"
                    player = 2
                    break
                else:
                    print("Invalid input")
                    continue
            else:
                player2_input = input("Its your turn player2. Move to which place? ")
                if board[player2_input] == " ":
                    board[player2_input] = "O"
                    player = 1
                    break
                else:
                    print("Invalid input")
                    continue
        totalmoves += 1


tictactoe()

Step 4:

In this step , first of all ,we will create another function and then in the function we will check player's 1 moves. If the condition fulfills then player 1 will be the winner.

def check():
    # checking moves of player1
    if board["1"] == board["2"] == board["3"] == "X":
        print("*player1 won*")
        return 1
    if board["4"] == board["5"] == board["6"] == "X":
        print("*player1 won*")
        return 1
    if board["7"] == board["8"] == board["9"] == "X":
        print("*player1 won*")
        return 1
    if board["1"] == board["4"] == board["7"] == "X":
        print("*player1 won*")
        return 1
    if board["2"] == board["5"] == board["8"] == "X":
        print("*player1 won*")
        return 1
    if board["3"] == board["6"] == board["9"] == "X":
        print("*player1 won*")
        return 1
    if board["1"] == board["5"] == board["9"] == "X":
        print("*player1 won*")
        return 1
    if board["3"] == board["5"] == board["7"] == "X":
        print("*player1 won*")
        return 1

image.png
Source

Step 5:

We'll check player's 2 moves now,

    if board["1"] == board["2"] == board["3"] == "O":
        print("*player2 won*")
        return 1
    if board["4"] == board["5"] == board["6"] == "O":
        print("*player2 won*")
        return 1
    if board["7"] == board["8"] == board["9"] == "O":
        print("*player2 won*")
        return 1
    if board["1"] == board["4"] == board["7"] == "O":
        print("*player2 won*")
        return 1
    if board["2"] == board["5"] == board["8"] == "O":
        print("*player2 won*")
        return 1
    if board["3"] == board["6"] == board["9"] == "O":
        print("*player2 won*")
        return 1
    if board["1"] == board["5"] == board["9"] == "O":
        print("*player2 won*")
        return 1
    if board["3"] == board["5"] == board["7"] == "O":
        print("*player2 won*")
        return 1
    return 0

If the condition fulfills then player 2 will be the winner.

Step 6:

Now if user want to play again, so for that we'll create a while loop and ask the user whether he/she wanna restart or not using an if else condition.

while True:
    restart = input("Do you want to play again? (y/n) ").lower()
    if restart == "y":
        board = {"1": " ", "2": " ", "3": " ", "4": " ", "5": " ", "6": " ", "7": " ", "8": " ", "9": " "}
        tictactoe()
    elif restart == "n":
        print("Thanks for playing :)")
        break
    else:
        print("Invalid Choice")

That was all for this game, hope you enjoyed this tutorial. Show some love if you liked this post then I'll post more tutorials
Thank you very much :)
Divider 2.png

Steem Infinity Zone Team
@cryptokraze | @arie.steem | @qasimwaqar | @vvarishayy | @suboohi

Footer.png

Click Here to Join Official SIZ Discord Channel

Discord
Twitter
Facebook

Divider 2.png

Sort:  
 3 years ago 

Good one tutorial dear friend you make a very good post thanks for sharing a good information with us my best wishes for you.

Regards, Faran Nabeel

 3 years ago 

Your post has been picked as the best pick of the day, Congrats

 3 years ago 

Thank you so much :)

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 62854.40
ETH 2463.99
USDT 1.00
SBD 2.65