Coding Concept - Algorithms
Developing Algorithms in Python
# ============================================================
# AP CSP: Developing Algorithms with Tic Tac Toe 🧩
# ============================================================
# Today you'll use algorithmic thinking to model a Tic Tac Toe game!
# We'll use a 2D list to represent the board and write algorithms
# that make moves and check for a winner.
# ============================================================
# -------------------------------
# Popcorn Hack 1: Initialize the 3x3 Board
# -------------------------------
# TODO: Create a 2D list (a list of lists) that represents a 3x3 Tic Tac Toe board.
# Each space should start as "_".
# Example shape:
# [
# ["_", "_", "_"],
# ["_", "_", "_"],
# ["_", "_", "_"]
# ]
board = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"]
] # <-- fill in here
print("Starting board:")
for row in board:
print(row)
# -------------------------------
# Popcorn Hack 2: Make a Move
# -------------------------------
# TODO: Place an "X" in the center of the board (row 1, col 1).
# Then, place an "O" in the top-left corner (row 0, col 0).
# Use 2D indexing: board[row][col] = value
# board[...][...] = ... # <-- fill in here
# board[...][...] = ... # <-- fill in here
board[1][1] = "X"
board[0][0] = "O"
print("\nBoard after two moves:")
for row in board:
print(row)
# -------------------------------
# Popcorn Hack 3: Check for a Win
# -------------------------------
# TODO: Write an algorithm to check if either player ("X" or "O") has won.
# A player wins if:
# ✅ Any row has all three of their symbols
# ✅ Any column has all three of their symbols
# ✅ Either diagonal has all three of their symbols
#
# The row and diagonal checks are given below.
# Your task: fill in the missing code for the COLUMN check section.
def check_winner(board, player):
# --- Check rows ---
for r in range(3):
if board[r][0] == player and board[r][1] == player and board[r][2] == player:
return True
# --- Check columns ---
for c in range(3):
if board[0][c] == player and board[1][c] == player and board[2][c] == player:
return True
# --- Check the first diagonal ---
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
# --- Check the second diagonal ---
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
# No winner found
return False
board = [
["X", "O", "X"],
["O", "X", "_"],
["O", "_", "X"]
]
print("\nTesting winner algorithm:")
for row in board:
print(row)
# Check both players
if check_winner(board, "X"):
print("X wins!")
elif check_winner(board, "O"):
print("O wins!")
else:
print("No winner yet.")
# --- Check columns ---
# TODO: Fill in this section!
# Hint: Use a loop (for c in range(3)) and check if
# board[0][c], board[1][c], and board[2][c] are all equal to player.
# for c in range(3):
# if ...:
# return True
# --- Check the first diagonal ---
# if board[0][0] == player and board[1][1] == player and board[2][2] == player:
# return True
# --- Check the second diagonal ---
# TODO: Fill in this section!
#if board[][] == player and board[][] == player and board[][] == player:
# return True
# No winner found
# return False
# Test your algorithm with a sample board!
board = [
["X", "O", "X"],
["O", "X", "_"],
["O", "_", "X"]
]
print("\nTesting winner algorithm:")
for row in board:
print(row)
# Check both players
if check_winner(board, "X"):
print("X wins!")
elif check_winner(board, "O"):
print("O wins!")
else:
print("No winner yet.")
Starting board:
['_', '_', '_']
['_', '_', '_']
['_', '_', '_']
Board after two moves:
['O', '_', '_']
['_', 'X', '_']
['_', '_', '_']
Testing winner algorithm:
['X', 'O', 'X']
['O', 'X', '_']
['O', '_', 'X']
X wins!
Testing winner algorithm:
['X', 'O', 'X']
['O', 'X', '_']
['O', '_', 'X']
X wins!