for a school assignment I need to make tic tac toe, I've got pretty much everything ready, except for 1 issue, I don't know how to make the game know when it's a draw and then quit
in the function check_tie() you can still see I have some attempts left over.
The spots in the tic tac toe board have to be numbers from 1-9 so I can't replace them with empty spaces. Meaning I can't do a check to see if there are no empty spots left
thanks in advance
Here's the full code
print ("*** Welcome to Tic Tac Teen ***")
gameboard = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
game_running = True
winner = None
player = 'X'
marker = ['X', 'O']
player_list = []
name1 = input("Fill in the name of player 1: ")
player_list.append(name1)
name2 = input("Fill in the name of player 2: ")
player_list.append(name2)
print("These are the players:", player_list)
print("These are their markers:", marker)
# Function to print gameboard
def print_gameboard(gameboard):
print(" -----------")
print(" | " gameboard[0] " | " gameboard[1] " | " gameboard[2] " | ")
print(" -----------")
print(" | " gameboard[3] " | " gameboard[4] " | " gameboard[5] " | ")
print(" -----------")
print(" | " gameboard[6] " | " gameboard[7] " | " gameboard[8] " | ")
print(" -----------")
print()
#Checks if the game is over by checking for a win or tie:
def check_if_game_over():
check_win()
check_tie()
def check_win():
global winner
winner_row = check_row()
winner_column = check_column()
winner_diagonal = check_diagonal()
#Gets the winer
if winner_row:
winner = winner_row
elif winner_column:
winner = winner_column
elif winner_diagonal:
winner = winner_diagonal
else: winner = None # / Tie?
#checks rows for a win, by comparing if the elements in the row are the same
def check_row():
global game_running
row1 = gameboard[0] == gameboard[1] == gameboard[2]
row2 = gameboard[3] == gameboard[4] == gameboard[5]
row3 = gameboard[6] == gameboard[7] == gameboard[8]
if row1 or row2 or row3:
game_running = False
if row1:
return gameboard[0]
elif row2:
return gameboard[3]
elif row3:
return gameboard[6]
def check_column():
global game_running
column1 = gameboard[0] == gameboard[3] == gameboard[6]
column2 = gameboard[1] == gameboard[4] == gameboard[7]
column3 = gameboard[2] == gameboard[5] == gameboard[8]
if column1 or column2 or column3:
game_running = False
if column1:
return gameboard[0]
elif column2:
return gameboard[1]
elif column3:
return gameboard[2]
def check_diagonal():
global game_running
diag1 = gameboard[0] == gameboard[4] == gameboard[8]
diag2 = gameboard[2] == gameboard[4] == gameboard[6]
if diag1 or diag2:
game_running = False
if diag1:
return gameboard[0]
elif diag2:
return gameboard[2]
def check_tie():
return
global game_running
if not position.isdigit():
game_running = False
# global game_running
# if moves == 9:
# game_running = False
def switch_player():
global player
#Switches player depending on who's turn it is.
if player =="X":
player = "O"
elif player == "O":
player = "X"
#function that runs the whole game
def play_game():
while game_running:
print("This is the current gameboard:")
moves = 0
print_gameboard(gameboard)
player_turn(player)
check_if_game_over()
switch_player()
if winner == "X":
print(player_list[0] " won!")
elif winner == "O":
print(player_list[1] " won!" )
elif winner == None:
print("It's a tie!")
def player_turn(player):
position = input("Choose a spot from 1-9: ")
while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
position = input("That's not a number between 1-9, try again: ")
position = int(position) - 1
# if gameboard[position] = taken (how do you do this?, )
#print(Position is already taken, please pick another spot)
gameboard[position] = player
print_gameboard(gameboard)
play_game()
CodePudding user response:
Well the only way it can be a draw is when there is no winner after 9 moves right? After 9 moves just check if there is a winner; if there still isnt then it is a draw.
So in play_game() i would loop for 9 times, in each loop I would show the board like you did, make the move, check for winner, if winner then return, if not keep looping. If I happen to exit out of the loop then that means I've used up all of my moves and that there is no winner so it must be a tie.
Edit: Thats just how I would approach the problem. Your method works just as well. There is no right or wrong answer
CodePudding user response:
Your code works okay as is, just replace the check_tie()
function with this
def check_tie():
global game_running
game_running = any(tile.isdigit() for tile in gameboard)
You had the right idea, just didn't loop through the tiles.
For clarity, any()
is a function that returns True
if one of the evaluated values is True
. Otherwise returns False
. isdigit()
is a method belonging to string
which checks if all the characters included are digits or not.