Skip to content

Connect4 Check Move Hints

Your check_move function is the referee's whistle: in the tournament, it's what catches an opponent's illegal move and wins you the game on the spot. The good news is that a Connect 4 move can only be illegal in a few ways, and each one is a quick check.


Overview

This is a fairly straightforward function with a signature that looks like this:

def check_move(piece, column, state):

We're checking whether a proposed move is legal. In other words, if someone holding piece wants to drop it in column and the game board looks like state, is that allowed?

Builds on: Connect4 Check Win Helper Functions — same library, next function.

You've got it when…

  • check_move rejects columns that don't exist on the board.
  • check_move rejects moves into full columns.
  • check_move rejects moves that would break the piece-count balance.
  • Every other move comes back True.

Collaboration & AI

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

AI — AIAS Level 1, No AI: Turning these hints into working code is the assignment. What the levels mean.


The Checks

Let's go through the logic for some checks:

  1. At any time, there are only 7 possible moves (columns 0 through 6). If column is less than 0 or greater than 6, the proposed move is illegal and we can return False immediately.

  2. If column is full, the proposed move is not legal. A shortcut to check if a column is full is to simply check if row 0 (the top row) has a blank "-" in the desired column. If not, the column must be full and the move is illegal; we can return False immediately.

    Tip

    Gravity does the bookkeeping for you. Pieces stack from the bottom, so the top row is always the last space to fill — if it's occupied, everything below it must be too. One comparison replaces a loop.

  3. There should always be either an equal number of each piece, or the count of each piece should differ by not more than one:

    1. Count all the red pieces.
    2. Count all the yellow pieces.
    3. Add piece to either the red or yellow count (whichever it is).
    4. If the absolute value of the difference between the red and yellow count is greater than one, the proposed move would be illegal and we can return False immediately.
    if abs(red_count - yellow_count) > 1:
    

    Why count pieces at all?

    Players alternate turns, so the piece counts can never drift more than one apart. A proposed move that would put red three pieces ahead means somebody is playing out of turn — and your referee should say so.

  4. Finally, if we can't find any of the above problems with the proposed move, it is legal, and we can return True.


Turn It In

  • Nothing separate to submit today. Your check_move function goes 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 check_move rejects all three illegal cases — nonexistent columns, full columns, and moves that break the piece-count balance — returns True for everything else, and you can show the board state you used to prove each rejection.
3 — Above Average Bounds and full-column checks are right; the count check has a slip, usually forgetting to add the proposed piece before comparing.
2 — Average Some checks work — but the function accepts moves into full columns, or rejects moves that are perfectly legal.
1 — Below Average The function returns True for everything, or crashes on ordinary inputs.
0 — Failing No check_move in your library.