---
# System prepended metadata

title: Connect 4 Worksheet

---

# Connect 4 Worksheet

## What is Connect 4?

Connect 4 is a two-player board game where each player takes turns dropping colored discs into a grid. The objective is to form a horizontal, vertical, or diagonal line of four discs of the same color before the opponent does.

## What are the rules of Connect 4?

- The game is played on a 7x6 grid of empty cells.
- The players have a set of 21 discs each, one red and one yellow.
- The players take turns choosing a column and dropping a disc of their color from the top into the lowest available cell in that column.
- The game ends when either one player forms a line of four discs of their color (horizontally, vertically, or diagonally), or the grid is full and no more moves are possible.

## How can we define the problem for writing a python program for Connect 4?

To write a python program for Connect 4, we need to:

- Define the data structures and variables that will represent the game state, such as the grid, the discs, and the current player.
- Define the functions that will implement the game logic, such as checking for valid moves, dropping discs, switching players, and checking for win conditions.
- Define the user interface that will display the game state and allow the user to interact with the program, such as printing the grid, asking for input, and showing messages.

## What are some examples of data structures and variables for Connect 4?

One possible way to represent the game state is:

- A list of lists called `grid` that will store the values of each cell in the grid. Each sublist will represent a column and each element will represent a row. The values can be `0` for empty cells, `1` for red discs, and `2` for yellow discs. For example:

```
grid = [
    [0, 0, 0, 0, 0, 0], # column 1
    [0, 0, 0, 0, 0, 0], # column 2
    [0, 0, 0, 0, 0, 0], # column 3
    [0, 0, 0, 0, 0, 0], # column 4
    [0, 0, 0, 0, 0, 0], # column 5
    [0, 0, 0, 0, 0, 0], # column 6
    [0, 0, 0, 0, 0, 0] # column 7
]
```

- A variable called `player` that will store the value of the current player. The value can be `1` for red or `2` for yellow. For example:

```
player = 1 # red goes first
```

## What are some examples of functions for Connect 4?

One possible way to implement the game logic is:

- A function called `is_valid_move(column)` that will take a column number as an argument and return `True` if it is a valid move (i.e., the column is not full) or `False` otherwise. For example:

```
def is_valid_move(column):
    # check if the column number is within range
    if column < 1 or column > len(grid):
        return False
    # check if the top cell of the column is empty
    if grid[column -1][len(grid[1]) -1] == " ":
        return True
    else:
        return False
```

- A function called `drop_disc(column)` that will take a column number as an argument and drop a disc of the current player's color into that column. It will also update the grid and switch the player. For example:

```
def drop_disc(column):
    # use the global variable player
    # loop through the rows of the column from bottom to top

    # code comes here
    
    # switch the player
    if player == "X":
        player = "O"
    else:
        player = "X"
```

- A function called `is_winner()` that will check if there is a winner