200k views
4 votes
Write a program for TIC TAC TOE in PYTHON

User MucaP
by
5.7k points

2 Answers

6 votes

Answer:hehe, sorry im a year late

board = ["-", "-", "-",

"-", "-", "-",

"-", "-", "-"]

current_player = 'X'

running = True

WINNER = None

def display_board():

print(board[0] + ' | ' + board[1] + ' | ' + board[2])

print(board[3] + ' | ' + board[4] + ' | ' + board[5])

print(board[6] + ' | ' + board[7] + ' | ' + board[8])

def play():

display_board()

while running:

handle_turns(current_player)

switch_turns()

check_if_game_over()

if WINNER == 'X':

print("The Play X won!")

elif WINNER == 'O':

print("The play O won!")

def handle_turns(player):

position = int(input('Choose a location from 1-9: ')) - 1

board[position] = player

display_board()

def switch_turns():

global current_player

if current_player == 'X':

current_player = 'O'

else:

current_player = 'X'

# Check to see if the game should be over

def check_if_game_over():

check_for_winner()

check_for_tie()

# Check if there is a tie on the board

def check_for_tie():

# figure out the conditions for a tie

global running

if '-' not in board:

running = False

# Check to see if somebody has won

def check_for_winner():

global WINNER

row_winner = check_rows()

column_winner = check_columns()

diagonal_winner = check_diagonals()

if row_winner:

WINNER = row_winner

elif column_winner:

WINNER = column_winner

elif diagonal_winner:

WINNER = diagonal_winner

else:

WINNER = None

# Check the rows for a win

def check_rows():

global running

if board[0] == board[1] == board[2] != '-':

running = False

return board[0]

elif board[3] == board[4] == board[5] != '-':

running = False

return board[3]

elif board[6] == board[7] == board[8] != '-':

running = False

return board[6]

else:

return None

# Check the columns for a win

def check_columns():

global running

if board[0] == board[3] == board[6] != '-':

running = False

return board[0]

elif board[1] == board[4] == board[7] != '-':

running = False

return board[1]

elif board[2] == board[5] == board[8] != '-':

running = False

return board[2]

else:

return None

# Check the diagonals for a win

def check_diagonals():

global running

if board[0] == board[4] == board[8] != '-':

running = False

return board[0]

elif board[6] == board[4] == board[2] != '-':

running = False

return board[6]

else:

return None

play()

User TWest
by
5.7k points
1 vote

Answer: much too long to write here: see attached

Step-by-step explanation:

Write a program for TIC TAC TOE in PYTHON-example-1
Write a program for TIC TAC TOE in PYTHON-example-2
User Osnoz
by
5.2k points