I was making a Console based Tic-tac-toe game. My script is given below:
game_board = [
["_", "_", "_",],
["_", "_", "_",],
["_", "_", "_",],
]
#This variable is made for checking eligible ans
eligible_ans = [0, 1, 2, 3]
#Game and is a variable by which game will continue or not
end = False
#Player list with all values
#player = [row, col, player num , state]
player1 = [0, 0, 1, False]
player2 = [0, 0, 2, False]
#Shows game board:
def show_gameboard():
for num in range(3):
print("|" + game_board[num][0] + "|" + game_board[num][1] + "|" + game_board[num][2] + "|")
#Takes eligible input from player
def get_move(player):
x = 4
y = 4
while x not in eligible_ans and y not in eligible_ans:
x = int(input("Enter your row: ")) - 1
y = int(input("Enter your colum: ")) - 1
player[0] = x
player[1] = y
#Switches on the state item on player list
def switch_on_state(player):
player[3] = True
def switch_off_state(player):
player[3] = False
#Checks if the slot is empty
def check_slot(player):
if game_board[player[0]][player[1]] == "_":
switch_on_state(player)
else:
while not player[3]:
get_move(player)
if game_board[player[0]][player[1]] == "_":
switch_on_state(player)
#Places the move
def move(player):
if player[3] == True:
if player[2] == 1:
game_board[player[0]][player[1]] = "X"
else:
game_board[player[0]][player[1]] = "O"
switch_off_state(player)
#Checks row for winner
def check_row():
if game_board[0][0] == game_board[0][1] and game_board[0][1] == game_board[0][2]:
end = True
elif game_board[1][0] == game_board[1][1] and game_board[1][1] == game_board[1][2]:
end = True
elif game_board[2][0] == game_board[2][1] and game_board[2][1] == game_board[2][2]:
end = True
#Check column for winner
def check_col():
if game_board[0][0] == game_board[1][0] and game_board[1][0] == game_board[2][0]:
end = True
elif game_board[0][1] == game_board[1][1] and game_board[1][1] == game_board[2][1]:
end = True
elif game_board[0][2] == game_board[1][2] and game_board[1][2] == game_board[2][2]:
end = True
else:
pass
#Check diagonal for winner
def check_diagonal():
#Down to up check
if game_board[2][0] == game_board[1][1] and game_board[1][1] == game_board[0][2]:
end = True
#Up to down check
elif game_board[0][0] == game_board[1][1] and game_board[1][1] == game_board[2][2]:
end = True
#Checks for whole winner
def check_winner():
check_row()
check_col()
check_diagonal()
#The main workarround
def main():
i = 0
while not end and i < 3:
show_gameboard()
print("Player1=> \n")
get_move(player1)
check_slot(player1)
move(player1)
check_winner()
if end:
print("Player 1 won")
break
print(end)
show_gameboard()
print("Player2=> \n")
get_move(player2)
check_slot(player2)
move(player2)
check_winner()
if end:
print("Player 2 won")
break
print(end) #*****The problem is here******
i += 1
if __name__ == "__main__":
try:
main()
except:
print("\nProgram Failed.\nTry again!!!\n")
My question is why the print(end) shows the value of end as False even though somebody wins?
print(end) is in the main() function where "The problem is here" written
Even though someone wins and by the function check_winner(), the end variable should be True. But why still the end variable shows/contains False as value.
end is a local variable. try to add:
global end
in the first line of check_row(), check_col() and in check_diagonal() and in the main().
for example:
#Check column for winner
def check_col():
global end
if game_board[0][0] == game_board[1][0] and game_board[1][0] == game_board[2][0]:
end = True
elif game_board[0][1] == game_board[1][1] and game_board[1][1] == game_board[2][1]:
end = True
elif game_board[0][2] == game_board[1][2] and game_board[1][2] == game_board[2][2]:
end = True
else:
pass
You can do this in another way, try to return the 'end' from these functions and receive the return value in main().