From 34db14300d98f40c9010646970f1cf5ca193caea Mon Sep 17 00:00:00 2001 From: anudhananjay Date: Tue, 22 Oct 2024 23:58:54 -0700 Subject: [PATCH] Update TicTacToe.py I edited some of the code and added comments at each stage to describe what the code is doing so that it is more readable and understandable. --- Python/TicTacToe.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Python/TicTacToe.py b/Python/TicTacToe.py index f631b898..8a033ab1 100644 --- a/Python/TicTacToe.py +++ b/Python/TicTacToe.py @@ -1,9 +1,9 @@ board = [" "," "," ", " "," "," ", - " "," "," ",] -def print_board(): + " "," "," ",] # the "=" assignment operator assigns the board design to the variable "board" +def print_board(): # defines the function print_board - uses the defined variable "board" to create the board print("-------------") - print("|",board[0],"|",board[1],"|",board[2],"|") + print("|",board[0],"|",board[1],"|",board[2],"|") print("-------------") print("|",board[3],"|",board[4],"|",board[5],"|") print("-------------") @@ -11,41 +11,41 @@ def print_board(): print("-------------") def check_winner(player): - for i in range(3): + for i in range(3): # checking to see if the vertical column has equal values - vertical win if board[i]==board[i+3]==board[i+6]==player : return True - for i in range(0,9,3): + for i in range(0,9,3): # checking to see if the horizontal column has equal values - horizontal win if board[i]==board[i+1]==board[i+2]==player : return True - if board[0]==board[4]==board[8]==player : + if board[0]==board[4]==board[8]==player : # checking for diagonal win from top left to bottom right return True - if board[2]==board[4]==board[5]==player : + if board[2]==board[4]==board[6]==player : # checking for diagnoal win from top right top bottom left return True return False def start_game(): - print("Welcome to TicTacToe") - print_board() + print("Welcome to TicTacToe") # welcome message + print_board() # prints the initial blank board player="X" - while True: - n=int(input("player "+player+" can make a move (Enter the num between 1-9) :"))-1 - if board[n] == " ": + while True: # continues game until there is a winner/tie + n=int(input("player "+player+" can make a move (Enter the num between 1-9) :"))-1 # asking player where they want to mark the grid + if board[n] == " ": # allows player to make move if that spot is blank board[n]=player else : - print("Invalid move, try again!!") + print("Invalid move, try again!!") # won't let player make move if spot is taken continue - if check_winner(player) == True: + if check_winner(player) == True: # uses the defined function "check_winner" to check if player has won print("Congratution player",player,"won the game!!") break - if " " not in board: + if " " not in board: # no empty spaces left, so it's a tie print("It is a tie!!") break - if(player=="X"): + if(player=="X"): # switches from player X to player O player="O" else : player="X" - print_board() -start_game() + print_board() # updates board after each move +start_game() # calls the start_game function to start the game