Skip to content

Connect4 Check Win Helper Functions

A Connect 4 board has 69 possible winning lines, and your check_win function has to notice every one of them. Checking them all in one giant function is possible, in the same way that eating a pizza in one bite is possible. Instead, we'll write one small helper function per direction and let check_win call them.


Overview

You'll write four helper functions — one for each way a player can win: horizontal, vertical, and the two diagonals. Each one answers a single, narrow question: is this location the start of a four-in-a-row? Your check_win function can then scan the board asking that question at every location.

Builds on: Connect4 — these helpers become part of your library from that assignment.

You've got it when…

  • is_horiz_win returns True exactly when a horizontal four-in-a-row starts at the given row and column.
  • You have working is_vert_win, is_diag_descending_win, and is_diag_ascending_win functions built the same way.
  • Each function returns False (instead of crashing) when asked about a location too close to the board's edge.

Collaboration & AI

Work: On your own — this code competes in the tournament.

AI — AIAS Level 1, No AI: Adapting one function into three variations is the exercise. Do the adapting yourself. What the levels mean.


The Horizontal Helper

Let's create a helper function to help with the check_win function. This one will check if a given location (row and column) is the first of a horizontal win.

  1. Add this function to your Connect4 library file.

    def is_horiz_win(state, row, col):
        # Check the bounds...don't fall off the board!
        if row < 0 or row > 5:
            return False
        if col < 0 or col > 3:
            return False
    
        # Gather the pieces
        piece1 = state[row+0][col+0]
        piece2 = state[row+0][col+1]
        piece3 = state[row+0][col+2]
        piece4 = state[row+0][col+3]
    
        # If the first piece is blank, no win
        if piece1 == "-":
            return False
    
        # If all four pieces are the same (and they can't be blanks), it is a win.
        return piece1 == piece2 == piece3 == piece4
    

Why does col stop at 3?

A horizontal win reads four pieces to the right of its starting location. Starting at column 4, 5, or 6 would run off the edge of a 7-column board, so those columns can never be the start of a horizontal win. The bounds check refuses them up front, and the row+0/col+0 arithmetic is written out so you can see exactly which direction each helper walks.


Your Turn: Three More Directions

Now copy and paste the helper function above and modify it to create these additional functions:

  1. Create is_vert_win:

    def is_vert_win(state, row, col):
    
  2. Create is_diag_descending_win:

    def is_diag_descending_win(state, row, col):
    
  3. Create is_diag_ascending_win:

    def is_diag_ascending_win(state, row, col):
    
Stuck? Open for a hint.

Only two things change between directions: the bounds that keep the four pieces on the board, and the offsets used to gather them. A vertical win walks down the rows (row+1, row+2, row+3) while the column stays put — so which index needs the tighter bound now? The diagonals change both indexes at once, and the ascending diagonal walks one of them backwards.


Turn It In

  • Nothing separate to submit today. These four helpers go into your Connect4 library file, which you'll submit with the Connect4 assignment.

How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent All four helpers — is_horiz_win, is_vert_win, is_diag_descending_win, is_diag_ascending_win — detect exactly the wins that start at the given location, with bounds adjusted correctly for each direction, and every one returns False near the board's edge instead of crashing. You can show a test board for each direction.
3 — Above Average All four helpers written and mostly working, with one bound off by one — usually the ascending diagonal, the one that walks backwards.
2 — Average The horizontal helper and one other direction work; the diagonals are missing, or crash when asked about a location near the edge.
1 — Below Average Only the horizontal helper from the lab, pasted in unchanged.
0 — Failing Nothing added to your library.