Siz tutorials || Tic-Tac-Toe in python|| by @aqsaejaz
20% to siz-official
Tic-Tac-Toe using python
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
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 :)
@cryptokraze | @arie.steem | @qasimwaqar | @vvarishayy | @suboohi
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
Your post has been picked as the best pick of the day, Congrats
Thank you so much :)