coding tic tac toe

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

A way of telling the computer how to check for the winner by looking at the rows, columns, and diagonals, if there's not a winner, winner = None. global winner just means the winner can be either player X or O.

def display_board(): print("\n") print(board[0] + " | " + board[1] + " | " + board[2] + " 1 | 2 | 3") print(board[3] + " | " + board[4] + " | " + board[5] + " 4 | 5 | 6") print(board[6] + " | " + board[7] + " | " + board[8] + " 7 | 8 | 9") print("\n")

Defining display_board() is assigning numbers to places on the board for the computer to label and how to finish the details on the board for the tic tac toe game

def play_game(): display_board() while game_still_going: handle_turn(current_player) check_if_game_over() flip_player()

Defining play_game() is how to make all the things you want to happen in the code, actually happen. Like displaying the board, handle turns, check if the game is over, and flip player. **while loop - game_still_going is a repeated task doing each function called in order

def check_if_game_over(): check_for_winner() check_for_tie()

Telling the computer to check for the winner, ties, and if the game is over.

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

They used this list because it's easier than printing (tic tac toe board) line by line

def check_columns(): global game_still_going column_1 = board[0] == board[3] == board[6] != "-" column_2 = board[1] == board[4] == board[7] != "-" column_3 = board[2] == board[5] == board[8] != "-" if column_1 or column_2 or column_3: game_still_going = False if column_1: return board[0] elif column_2: return board[1] elif column_3: return board[2] else: return None

This is a way of telling the computer how to check each of the columns for a winner, the ! = "-" means NOT a -, because if the column was full then it would have either Xs or Os filled up. If any of the columns are full, then the game is over. Return board means to check the columns starting at 0, 3, or 6. Return none is if none of the boards are full of X's or O's

def check_diagonals(): global game_still_going diagonal_1 = board[0] == board[4] == board[8] != "-" diagonal_2 = board[2] == board[4] == board[6] != "-" if diagonal_1 or diagonal_2: game_still_going = False if diagonal_1: return board[0] elif diagonal_2: return board[2] else: return None

This is a way of telling the computer how to check each of the diagonals for a winner, the ! = "-" means NOT a -, because if the diagonals was full then it would have either Xs or Os filled up. If any of the diagonals are full, then the game is over. Return board means to check the diagonals starting at 0, 4, or 8. Return none is if none of the boards are full of X's or O's

if winner == "X" or winner == "O": print(winner + " won.") elif winner == None: print("Tie.")

We need to use the if and elif commands because we need a way to show who the winner is or if the game ended in a tie

def handle_turn(player): print(player + "'s turn.") position = input("Choose a position from 1-9: ") valid = False while not valid: while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]: position = input("Choose a position from 1-9: ") position = int(position) - 1 if board[position] == "-": valid = True else: print("You can't go there. Go again.") board[position] = player display_board()

it's a way of telling the player if the spot is already taken and to choose another spot if it's already been used, or to continue if it's a spot that is available (telling the player if it's a valid input and if the spot is open) position = int(position) - 1 Because to display the board, the numbers must be 0 through 8 so you have to subtract 1 from 1 through 9 (in line 62)

def check_rows(): global game_still_going row_1 = board[0] == board[1] == board[2] != "-" row_2 = board[3] == board[4] == board[5] != "-" row_3 = board[6] == board[7] == board[8] != "-" if row_1 or row_2 or row_3: game_still_going = False if row_1: return board[0] elif row_2: return board[3] elif row_3: return board[6] else: return None

its telling the computer how to check each of the rows for a winner, the ! = "-" means NOT a -, because if the row was full then it would have either Xs or Os filled up. If any of the rows are full, then the game is over. Return board means to check the rows starting at 0, 3, or 6. Return none is if none of the boards are full of X's or O's


Conjuntos de estudio relacionados

Prep U MS3 Ch. 12 Oncologic Management

View Set

GCSE: Answer Smash Vocab: Animals

View Set

P36 Earth and Physical Science Teas 5

View Set