Skip to content

Instantly share code, notes, and snippets.

@CodeWithHarry
Created April 30, 2022 05:36
Show Gist options
  • Save CodeWithHarry/d83fed6958b7626ef51aa87c2d7130de to your computer and use it in GitHub Desktop.
Save CodeWithHarry/d83fed6958b7626ef51aa87c2d7130de to your computer and use it in GitHub Desktop.
Code for the tic tac toe game we created in this video: https://youtu.be/E8fmDDtaHLU on CodeWithHarry channel on YouTube
def sum(a, b, c ):
return a + b + c
def printBoard(xState, zState):
zero = 'X' if xState[0] else ('O' if zState[0] else 0)
one = 'X' if xState[1] else ('O' if zState[1] else 1)
two = 'X' if xState[2] else ('O' if zState[2] else 2)
three = 'X' if xState[3] else ('O' if zState[3] else 3)
four = 'X' if xState[4] else ('O' if zState[4] else 4)
five = 'X' if xState[5] else ('O' if zState[5] else 5)
six = 'X' if xState[6] else ('O' if zState[6] else 6)
seven = 'X' if xState[7] else ('O' if zState[7] else 7)
eight = 'X' if xState[8] else ('O' if zState[8] else 8)
print(f"{zero} | {one} | {two} ")
print(f"--|---|---")
print(f"{three} | {four} | {five} ")
print(f"--|---|---")
print(f"{six} | {seven} | {eight} ")
def checkWin(xState, zState):
wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
for win in wins:
if(sum(xState[win[0]], xState[win[1]], xState[win[2]]) == 3):
print("X Won the match")
return 1
if(sum(zState[win[0]], zState[win[1]], zState[win[2]]) == 3):
print("O Won the match")
return 0
return -1
if __name__ == "__main__":
xState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
zState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
turn = 1 # 1 for X and 0 for O
print("Welcome to Tic Tac Toe")
while(True):
printBoard(xState, zState)
if(turn == 1):
print("X's Chance")
value = int(input("Please enter a value: "))
xState[value] = 1
else:
print("O's Chance")
value = int(input("Please enter a value: "))
zState[value] = 1
cwin = checkWin(xState, zState)
if(cwin != -1):
print("Match over")
break
turn = 1 - turn
@kingbode
Copy link

Added another version with colors and more refactoring

@kingbode
Copy link

added draw condition in case no players wins

@mansi10c
Copy link

There are some of glitches in the given code which i could find:

  1. It goes to infinity when no match is made.
  2. It doesn't shows the the final board and declare directly the winning team.

@muradabbas123
Copy link

Sir i watch many videos in your channel but dnt know why only this video disappointed me to learn

@muradabbas123
Copy link

but you are still best >>

@karanbhdr
Copy link

i dont find i easy to understand and learn like other video of yours

@suvadip2409
Copy link

great work harray vaia

@Komal874u
Copy link

Run code

@ranjitadh
Copy link

you did great harry vai love from NEPAL

@umair-sidd
Copy link

Please code the same game but the opponent should be a computer. So, human vs computer.

@manikraut24
Copy link

Thank you harry bhai :)

@Deepak12-Bisht
Copy link

Hi harry, can you play tic-tac-toe with AI ?
Please code the same game but the opponent should be a computer. So, human vs computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment