In this video I will tell you the easiest method in which you can make a tic-tac-toe game in python in just 50 lines of code. This is the easiest method you are ever gonna see. Enjoy the video!!!
Code in the Video :
chance = ' X '
next_chance = ' O '
board = [' - ',' - ',' - ',
' - ',' - ',' - ',
' - ',' - ',' - ',]
def show_board():
print(board[0] , board[1] , board[2])
print(board[3], board[4], board[5])
print(board[6], board[7], board[8])
def take_chance():
global chance , next_chance
value = input('Enter numbers from 1 to 9')
try:
value = int(value)
except:
take_chance()
if value in range(1,10):
if board[value-1] == ' - ':
board[value-1] = chance
chance, next_chance = next_chance, chance
else:
take_chance()
else:
take_chance()
def check_draw():
if ' - ' not in board:
show_board()
print("Match Draw")
quit()
def check_win():
winning_pos = [
[1,2,3],[4,5,6],[7,8,9],
[1,4,7],[2,5,8],[3,6,9],
[1,5,9],[3,5,7]
]
for i in winning_pos:
v1,v2,v3 = i[0]-1 , i[1]-1, i[2]-1
if board[v1] == ' X ' and board[v2] == ' X ' and board[v3] == ' X ':
show_board()
print(" X won the game")
quit()
if board[v1] == ' O ' and board[v2] == ' O ' and board[v3] == ' O ':
show_board()
print(" O won the game")
quit()
while True:
show_board()
take_chance()
check_draw()
check_win()
If this video helped you out then don't forget to like the video.
Ask any doubts in the comments and I will surely answer them.