board = [[""] * 3 for i in range(3)]
def initialise_array():
for row in range(0, 3):
for column in range(0, 3):
board[row][column] = "."
def input_value(current_player): #no keyword in python for by ref so
row_position = int(input(f"{current_player}, Please enter the row number where you want to place
your symbol: "))
while (row_position < 0 or row_position > 2):
print("Invalid row position. Please enter a number from 0-2 inclusive")
row_position = int(input(f"{current_player}, Please enter the row number where you want to place
your symbol: "))
column_position = int(input(f"{current_player}, Please enter the column number where you want to
place your symbol: "))
while (column_position < 0 or column_position > 2):
print("Invalid column position. Please enter a number from 0-2 inclusive")
column_position = int(input(f"{current_player}, Please enter the column number where you want to
place your symbol: "))
while (board[row_position][column_position] == "X" or board[row_position][column_position] ==
"O"):
print("Current position is taken. Please try again.")
print("")
row_position = int(input(f"{current_player}, Please enter the row number where you want to place
your symbol: "))
column_position = int(input(f"{current_player}, Please enter the column number where you want to
place your symbol: "))
if current_player == Player1:
board[row_position][column_position] = "X"
elif current_player == Player2:
board[row_position][column_position] = "O"
current_board()
return board #no by ref so any changes made to variable will have to be done using return
def initial_board():
print("[0][0] | [0][1] | [0][2]")
print("-------------------------")
print("[1][0] | [1][1] | [1][2]")
print("-------------------------")
print("[2][0] | [2][1] | [2][2]")
print("")
def current_board():
print("")
for row in range(0, 3):
for column in range(0, 3):
print(board[row][column], end = " ")
print()
print()
def check_status():
# there will be a winner if horizontal, vertical or diagonal matches
for row in range(0, 3):
if (board[row][0] == board [row][1] == board [row][2]) and (board[row][0] != "."):
#vertical check
return True #vertical win
for column in range(0, 3):
if (board[0][column] == board[1][column] == board[2][column]) and (board[0][column] != "."):
#horizontal check
return True #horizontal win
if ((board[0][0] == board[1][1] == board[2][2]) and (board[0][0] != ".")) or ((board[2][0] == board[1][1]
== board[0][2]) and (board[0][2] != ".")):
#diagonal check
return True #diagonal win
#checking for a draw
for row in range(0, 3):
for column in range(0, 3):
if board[row][column] == ".":
return False # Still moves left, game continues
return "Draw" #if none of the continditions are fulfilled and the game board is full but no winner
def take_turns():
current_player = Player1 # Start with Player1
status = False
while status == False:
initial_board()
input_value(current_player)
#current board is shown inside input value procedure
status = check_status() # Check game status
# If the game ended, break before switching player
if status != False:
break
# Switch current player
if current_player == Player1:
current_player = Player2
else:
current_player = Player1
if status == True:
# The current_player made the winning move.
print(f"Winner: {current_player}")
current_board()
print()
elif status == "Draw":
print("The game is a draw.")
current_board()
print()
def play_game_again():
play_again = str(input("If you want to play again, enter yes, else no: "))
if (play_again == "Yes" or play_again == "yes" or play_again == "Y" or play_again == "y"):
print()
again = True
elif (play_again == "No" or play_again == "no" or play_again == "N" or play_again == "n"):
print()
again = False
print("Exiting Game")
exit()
else:
print("Invalid Answer. Exiting Game.")
exit()
return again
#******************* Main Module ***********************
print("Welcome to Tic-Tac-Toe!")
print()
Player1 = str(input("Please enter first person's name: "))
print(f"{Player1}, you are Player 1.")
print()
Player2 = str(input("Please enter other person's name: "))
while Player2 == Player1 :
print()
print("Both players cannot have the same name. Try again.")
print()
Player2 = str(input("Please enter other person's name: "))
print(f"{Player2}, you are Player 2.")
print()
again = True
while again == True:
initialise_array()
take_turns()
again = play_game_again()