⭕❌⭕ The Tic Tac Toe Referee¶
Scenario:
You're a famous Tic Tac Toe referee, and you've been called in to resolve a dispute between two players. Multiple games have been played but no one can remember exactly how they played out, and checking all the games by hand would take a lot of time.
Exercise:
You've been given the moves for each game, and you need to figure out the winner of each game where 1 represents player 1, 2 represents player 2, and 0 represents an empty space.
In [1]:
Copied!
import numpy as np
# Initialize an array of zeros with the dimensions of your games
num_games = 1000
games = np.zeros((num_games, 3, 3), dtype=int)
# Fill the array with random moves
for game in games:
# Randomly choose your moves
num_your_moves = np.random.randint(1, 5)
your_moves = np.random.choice(9, num_your_moves, replace=False)
np.put(game, your_moves, 1) # Fill your moves into the game grid
# Randomly choose the opponent's moves
num_opponent_moves = np.random.randint(1, 9 - num_your_moves)
opponent_moves = np.random.choice(list(set(range(9)) - set(your_moves)), num_opponent_moves, replace=False)
np.put(game, opponent_moves, 2)
import numpy as np
# Initialize an array of zeros with the dimensions of your games
num_games = 1000
games = np.zeros((num_games, 3, 3), dtype=int)
# Fill the array with random moves
for game in games:
# Randomly choose your moves
num_your_moves = np.random.randint(1, 5)
your_moves = np.random.choice(9, num_your_moves, replace=False)
np.put(game, your_moves, 1) # Fill your moves into the game grid
# Randomly choose the opponent's moves
num_opponent_moves = np.random.randint(1, 9 - num_your_moves)
opponent_moves = np.random.choice(list(set(range(9)) - set(your_moves)), num_opponent_moves, replace=False)
np.put(game, opponent_moves, 2)
Your Tasks:
- Create a function that checks if a player has won a game along the rows.
In [2]:
Copied!
def check_row(row: np.ndarray) -> int:
"""Check if a row is a winning row.
Args:
row (np.ndarray): A row of a tic-tac-toe game. Shape (3,).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
def check_row(row: np.ndarray) -> int:
"""Check if a row is a winning row.
Args:
row (np.ndarray): A row of a tic-tac-toe game. Shape (3,).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
- Create a function that checks if a player has won a game along the rows using the previous function.
In [3]:
Copied!
def check_rows(grid: np.ndarray) -> int:
"""Check if any row is a winning row.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
def check_rows(grid: np.ndarray) -> int:
"""Check if any row is a winning row.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
- Create a function that checks if a player has won a game along the columns.
In [4]:
Copied!
def check_columns(grid: np.ndarray) -> int:
"""Check if any column is a winning column.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
def check_columns(grid: np.ndarray) -> int:
"""Check if any column is a winning column.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
- Create a function that checks if a player has won a game along the diagonals.
In [5]:
Copied!
def check_diagonals(grid: np.ndarray) -> int:
"""Check if any diagonal is a winning diagonal.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
def check_diagonals(grid: np.ndarray) -> int:
"""Check if any diagonal is a winning diagonal.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if the row is a winning row for the player 1,
2 if the row is a winning row for the player 2,
0 otherwise.
"""
pass
- Create a function that checks if a player has won a game using the previous three functions.
In [6]:
Copied!
def check_win(grid: np.ndarray) -> int:
"""Check if any row, column, or diagonal is a winning row, column, or
diagonal.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if a row, column, or diagonal is a winning row, column, or
diagonal for the player 1, 2 if it is a winning row, column, or
diagonal for the player 2, 0 otherwise.
"""
pass
def check_win(grid: np.ndarray) -> int:
"""Check if any row, column, or diagonal is a winning row, column, or
diagonal.
Args:
grid (np.ndarray): A tic-tac-toe game. Shape (3, 3).
Returns:
int: 1 if a row, column, or diagonal is a winning row, column, or
diagonal for the player 1, 2 if it is a winning row, column, or
diagonal for the player 2, 0 otherwise.
"""
pass
- Compute how many games you've won/lost/drawn in the dataset.
In [7]:
Copied!
Player 1 wins: 117 Player 2 wins: 309 Draws: 574
๐ Congratulations! You've just created a Tic Tac Toe referee that can check the winner of any game! ๐