---
title: Implementing a Four-Way Scan Algorithm to Detect Winners in a 6x7 Connect 4 Game Grid
created: 2026-03-24
updated: 2026-03-24
type: summary
tags: ["algorithm", "game", "notes", "python", "telegram"]
external: http://go/ui/posts/2254/
status: inbox
---

Since we are moving from a 3x3 grid to a larger 6x7 grid (which sounds exactly like the game **Connect 4**!), the "scorecard" trick from Tic-Tac-Toe gets a bit tricky because we aren't just looking for a full row—we are looking for **any** 4 pieces in a row.

Imagine you are a detective with a magnifying glass. To find a winner, you have to look at every single cell and check if it’s the "start" of a winning line of 4.

### The Strategy: "The Four-Way Scan"

For every cell that isn't empty, we check in four directions to see if there are 3 more matching pieces following it:
1.  **Right** (Horizontal)
2.  **Down** (Vertical)
3.  **Down-Right** (Diagonal $\searrow$)
4.  **Down-Left** (Diagonal $\swarrow$)



### The Python Solution

```python
def check_winner(grid):
    rows = 6
    cols = 7

    for r in range(rows):
        for c in range(cols):
            player = grid[r][c]
            
            # If the cell is empty, skip it!
            if player is None:
                continue
            
            # 1. Check Horizontal (to the right)
            # We only check if there are at least 3 columns left to the right
            if c + 3 < cols:
                if player == grid[r][c+1] == grid[r][c+2] == grid[r][c+3]:
                    return player

            # 2. Check Vertical (downwards)
            # We only check if there are at least 3 rows left below
            if r + 3 < rows:
                if player == grid[r+1][c] == grid[r+2][c] == grid[r+3][c]:
                    return player

            # 3. Check Diagonal (Down-Right)
            if r + 3 < rows and c + 3 < cols:
                if player == grid[r+1][c+1] == grid[r+2][c+2] == grid[r+3][c+3]:
                    return player

            # 4. Check Diagonal (Down-Left)
            if r + 3 < rows and c - 3 >= 0:
                if player == grid[r+1][c-1] == grid[r+2][c-2] == grid[r+3][c-3]:
                    return player

    # If we checked the whole board and found nothing...
    return None
```

---

### Why we use `+3` and `-3`:
* **Boundary Safety:** If you are at the very last column (Column 6), you can't look 3 spaces to the right, or you'll fall off the edge of the board! The `if c + 3 < cols` part is like a safety railing to make sure we stay on the grid.
* **The "Starting Point" Logic:** By only checking Right, Down, and the two Down-Diagonals, we cover every possible line without checking the same line twice (like checking Left or Up).

### How to test it:
If you want to see it in action, you can create a simple grid:
```python
my_grid = [[None for _ in range(7)] for _ in range(6)]
my_grid[5][0] = 'x'
my_grid[5][1] = 'x'
my_grid[5][2] = 'x'
my_grid[5][3] = 'x' # Four in a row!

print(check_winner(my_grid)) # This should print 'x'
```

Would you like me to show you how to turn this into a playable game where you can type in your moves?
