Skip to main content

Lab 6: Connect 4 Game

Objective

The objective of this lab is to develop a program in C that plays the connect 4 game. You will practice using 2D arrays, loops, functions, and conditional logic while implementing the game.

It is due March 8, 2025 at 11:59 PM.


Grades

This lab is worth 10 marks, as follows:

  • 4 marks for the public test cases.
  • 3 marks for the hidden private test cases.
  • 3 marks for in-person lab marking.

You can always see your public test cases automarker grades here APS105 after every submission. They are out of 4.

The in-person lab marking is worth 3 marks, and will be marked by a TA in-person. You should go to your assigned lab section, your assigned TA and sit on your assigned workstation. You will find this information on Quercus under Lab Subsection. You must sit in your assigned seat for the TA to grade you. Otherwise, they will mistakenly think you are absent and give you a 0. The in-lab grading is due March 15, 2025 in your practical session. Your assigned TA will ask you questions about your code, and will look at your code to ensure the coding style is neat. For this lab, the coding style involves if you are having at least 3 functions in your program, indenting your code properly, if you are using proper variable names, if you are using proper comments, and if you are using proper spacing.

Instructions

  1. Go to your GitHub repo from lab 0 by following this link: https://github.com/compeng-gg/2025-winter-aps105-<UTORID>, where you'll have to replace <UTORID> with your UTORid.

  2. Open the same codespace you created for lab0. You should not create any other codespaces. Existing codespace in your GitHub repository Fig.1 Existing codespace created in your GitHub repository

  3. In the terminal, type in the following command to transfer (pull) lab 5 material into your codespace:

    pull

    This will do a "pull" (which gets files from the "student" repository, containing starter code). This helper command will make sure you commit all your changes before you can start Lab 6. You should always commit (and push) your changes!

  4. You should see a folder called lab5 in your repository.

    Important! If you're using the Codespace to save the lecture notes, you'll get "You must commit all your changes (check git status)". To resolve this, you may use the following command instead:

    git pull upstream main

    If you get the following error, please press on Ctrl+X for Mac and Windows.

    Merge Fig.2 Merge pull message

    note

    If you are using the Codespace please ignore these steps. For students NOT using the Codespace, first do:

    git remote add upstream git@github.com:compeng-gg/2025-winter-aps105-student.git

    You only need to do this once, then after you'll use:

    git pull

    This command gets the starter code for the lab.

Description

In this lab, you will implement a simplified version of Connect Four using C programming. Connect Four is a two-player game where players take turns dropping colored pieces into a vertical grid. The goal of the game is to connect four of the same color in a row, column, or diagonal before the opponent does.

The board consists of 6 rows and 6 columns, and players alternate turns dropping pieces into a selected column. The piece falls to the lowest available row in the chosen column.

Your implementation should:

  • Print the board after each move
  • Prompt the user for column input between 0-5 to drop a piece.
  • Validate column entered is within bounds of the array and is not full.
  • Check for a winner or a tie, which happens when the board is full and there is no winner.
  • Display the final board when the game ends.

Game Rules

Players and Turns: Two players take turns. Player 1 (Red) represented by 'R', and player 2 (Yellow) represented by 'Y'. The game starts with player 1 (Red) always.

Valid Moves: Players choose a column index (0-5) to drop their piece. Your program should validate that the selected column index is between 0 and 5, and is not full column, since the piece has to fall to the lowest available row. If a player chooses a full column or an index not within the bounds of the array, they must re-enter their choice. To prompt or re-prompt the user, use either Yellow, please enter a valid column number (0-5): or Red, please enter a valid column number (0-5): (not followed by a newline character) depending on which player's turn it is.

Winning Condition: A player wins if they connect four pieces in a row: horizontally (left to right), vertically (top to bottom) and/or diagonally (both directions). The game announces the winner immediately when four pieces are connected, i.e. your code prints Yellow wins! or Red wins! (followed by a new line).

Game Over: The game ends when a player wins or when the board is full (resulting in a tie). If the board is full and no player has won, print It's a tie! (followed by a new line character), then print Final board: (followed by a newline character) and print the board.

Suggested Functions

You are free to define and use any functions in your code. Just don't use the math library functions. We suggest implementing the following four functions to structure your program effectively.

1. void printBoard(char gameBoard[][COLS]);

This function prints the array of cells named gameBoard, where each cell is represented by a dash (-) if it is empty, character R if it has a red piece and character Y if it has a yellow piece. Note that you need to consider printing the higher index rows at the top and lower index rows at the bottom as shown in Fig.3 below. Each row is printed on a separate line, i.e. after each row printed you should print a new line character. The size of the 2D array is known to be 6 by 6. You can define the following macros at the beginning of your code:

#define ROWS 6
#define COLS 6
#define EMPTY '-'
#define RED 'R'
#define YELLOW 'Y'

Connect 4 game with red winning Fig.3 Connect 4 game

2. int getInput(char gameBoard[][COLS], char turn);

This function prompts the player whose turn is stored in turn to enter a valid column number and ensures it is within bounds and not full.

After implementing this function, please test it by calling it in the main function and check it validates the columns correctly.

3. void insertPiece(char gameBoard[][COLS], int columnEntered, char turn);

This function inserts the piece of the player whose turn is stored in turn into the column columnEntered of the gameBoard. The function should edit the gameBoard by making the piece fall to the lowest index available row in the chosen column.

After implementing this function, please test it by calling it in the main function along with getInput and printBoard functions to test if they work correctly. Correcting mistakes after this stage will be challenging, so please don't move ahead without testing.

4. void switchTurn(char* turn);

This function switches the turn of the player stored in what turn points to. If the *turn is R, it should change it to Y and vice versa.

Test this function by calling it in the main function after the insertPiece function. Don't proceed if it is not working.

5. bool checkOneDirection(char gameBoard[][COLS], int row, int col, int rowDir, int colDir);

This function takes as input row and col indices of a cell in the gameBoard and two integers rowDir and colDir representing the direction to check. For example, if (rowDir, colDir) is (+1, +1), it should check four pieces in the upward right direction as shown in Fig.3 above. It returns true if there are four connected pieces in the given direction starting from the cell at row and col indices. Otherwise, it returns false.

6. bool checkWinner(char gameBoard[][COLS]);

This function checks if there is a winner in the current state of the gameBoard. It should call the checkOneDirection function for all possible directions (up/down, left/right, and diagonals) from each cell in the gameBoard. If any of the calls to checkOneDirection return true, the function should return true. Otherwise, it should return false.

You may have other extra functions, for example, to check if the board is full or to initialize the array to - characters.

You can now implement the main function to play the game. The main function should have a loop that continues until there is a winner or a tie. In each iteration, it should call the printBoard function, then the getInput function, then the insertPiece function, then the checkWinner function, and finally the switchTurn function. Once the game ends, the main function should print the result of the game (winner or tie), Final board: (followed by a newline) and the final board.


Sample Output

Here are some example outputs for the Connect Four game.

Example 1: The following output shows a game where the red player wins. The input is shown after the colon : .

------
------
------
------
------
------
Red, please enter a valid column number (0-5): 0
------
------
------
------
------
R-----
Yellow, please enter a valid column number (0-5): 1
------
------
------
------
------
RY----
Red, please enter a valid column number (0-5): 0
------
------
------
------
R-----
RY----
Yellow, please enter a valid column number (0-5): 1
------
------
------
------
RY----
RY----
Red, please enter a valid column number (0-5): 0
------
------
------
R-----
RY----
RY----
Yellow, please enter a valid column number (0-5): 1
------
------
------
RY----
RY----
RY----
Red, please enter a valid column number (0-5): 6
Red, please enter a valid column number (0-5): 2
------
------
------
RY----
RY----
RYR---
Yellow, please enter a valid column number (0-5): 1
Yellow wins!
Final board:
------
------
-Y----
RY----
RY----
RYR---

Example 2: The following output shows a game where the yellow player wins.

------
------
------
------
------
------
Red, please enter a valid column number (0-5): 1
------
------
------
------
------
-R----
Yellow, please enter a valid column number (0-5): 2
------
------
------
------
------
-RY---
Red, please enter a valid column number (0-5): 3
------
------
------
------
------
-RYR--
Yellow, please enter a valid column number (0-5): 1
------
------
------
------
-Y----
-RYR--
Red, please enter a valid column number (0-5): 2
------
------
------
------
-YR---
-RYR--
Yellow, please enter a valid column number (0-5): 3
------
------
------
------
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 4
------
------
------
------
-YRY--
-RYRR-
Yellow, please enter a valid column number (0-5): 1
------
------
------
-Y----
-YRY--
-RYRR-
Red, please enter a valid column number (0-5): 2
------
------
------
-YR---
-YRY--
-RYRR-
Yellow, please enter a valid column number (0-5): 4
------
------
------
-YR---
-YRYY-
-RYRR-
Red, please enter a valid column number (0-5): 3
------
------
------
-YRR--
-YRYY-
-RYRR-
Yellow, please enter a valid column number (0-5): 4
------
------
------
-YRRY-
-YRYY-
-RYRR-
Red, please enter a valid column number (0-5): 4
Red wins!
Final board:
------
------
----R-
-YRRY-
-YRYY-
-RYRR-

Example 3: The following output shows a game where the board is full and there is a tie.

------
------
------
------
------
------
Red, please enter a valid column number (0-5): 1
------
------
------
------
------
-R----
Yellow, please enter a valid column number (0-5): 2
------
------
------
------
------
-RY---
Red, please enter a valid column number (0-5): 3
------
------
------
------
------
-RYR--
Yellow, please enter a valid column number (0-5): 1
------
------
------
------
-Y----
-RYR--
Red, please enter a valid column number (0-5): 2
------
------
------
------
-YR---
-RYR--
Yellow, please enter a valid column number (0-5): 3
------
------
------
------
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 1
------
------
------
-R----
-YRY--
-RYR--
Yellow, please enter a valid column number (0-5): 2
------
------
------
-RY---
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 3
------
------
------
-RYR--
-YRY--
-RYR--
Yellow, please enter a valid column number (0-5): 1
------
------
-Y----
-RYR--
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 2
------
------
-YR---
-RYR--
-YRY--
-RYR--
Yellow, please enter a valid column number (0-5): 3
------
------
-YRY--
-RYR--
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 1
------
-R----
-YRY--
-RYR--
-YRY--
-RYR--
Yellow, please enter a valid column number (0-5): 2
------
-RY---
-YRY--
-RYR--
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 3
------
-RYR--
-YRY--
-RYR--
-YRY--
-RYR--
Yellow, please enter a valid column number (0-5): 1
-Y----
-RYR--
-YRY--
-RYR--
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 2
-YR---
-RYR--
-YRY--
-RYR--
-YRY--
-RYR--
Yellow, please enter a valid column number (0-5): 3
-YRY--
-RYR--
-YRY--
-RYR--
-YRY--
-RYR--
Red, please enter a valid column number (0-5): 0
-YRY--
-RYR--
-YRY--
-RYR--
-YRY--
RRYR--
Yellow, please enter a valid column number (0-5): 0
-YRY--
-RYR--
-YRY--
-RYR--
YYRY--
RRYR--
Red, please enter a valid column number (0-5): 0
-YRY--
-RYR--
-YRY--
RRYR--
YYRY--
RRYR--
Yellow, please enter a valid column number (0-5): 0
-YRY--
-RYR--
YYRY--
RRYR--
YYRY--
RRYR--
Red, please enter a valid column number (0-5): 0
-YRY--
RRYR--
YYRY--
RRYR--
YYRY--
RRYR--
Yellow, please enter a valid column number (0-5): 0
YYRY--
RRYR--
YYRY--
RRYR--
YYRY--
RRYR--
Red, please enter a valid column number (0-5): 4
YYRY--
RRYR--
YYRY--
RRYR--
YYRY--
RRYRR-
Yellow, please enter a valid column number (0-5): 4
YYRY--
RRYR--
YYRY--
RRYR--
YYRYY-
RRYRR-
Red, please enter a valid column number (0-5): 4
YYRY--
RRYR--
YYRY--
RRYRR-
YYRYY-
RRYRR-
Yellow, please enter a valid column number (0-5): 4
YYRY--
RRYR--
YYRYY-
RRYRR-
YYRYY-
RRYRR-
Red, please enter a valid column number (0-5): 4
YYRY--
RRYRR-
YYRYY-
RRYRR-
YYRYY-
RRYRR-
Yellow, please enter a valid column number (0-5): 4
YYRYY-
RRYRR-
YYRYY-
RRYRR-
YYRYY-
RRYRR-
Red, please enter a valid column number (0-5): 5
YYRYY-
RRYRR-
YYRYY-
RRYRR-
YYRYY-
RRYRRR
Yellow, please enter a valid column number (0-5): 5
YYRYY-
RRYRR-
YYRYY-
RRYRR-
YYRYYY
RRYRRR
Red, please enter a valid column number (0-5): 5
YYRYY-
RRYRR-
YYRYY-
RRYRRR
YYRYYY
RRYRRR
Yellow, please enter a valid column number (0-5): 5
YYRYY-
RRYRR-
YYRYYY
RRYRRR
YYRYYY
RRYRRR
Red, please enter a valid column number (0-5): 5
YYRYY-
RRYRRR
YYRYYY
RRYRRR
YYRYYY
RRYRRR
Yellow, please enter a valid column number (0-5): 5
It's a tie
Final board:
YYRYYY
RRYRRR
YYRYYY
RRYRRR
YYRYYY
RRYRRR

Testing and Submission

  1. Create a new file called lab6.c in the lab6 folder.

  2. Edit your code in lab6.c, compile it, and run it to produce the output shown above. You can use the handy buttons shown below.
    Handy Buttons

    Fig.4 Handy buttons to compile, run, and debug your C program.

    You also compile and run your program using the terminal, similarly to the previous assignments.

  3. Once you have the correct output, go to the terminal and test your program against the test cases by typing the following commands:

cd /workspace/lab6
test
  1. If your program passes the test cases, you can submit your code by typing the following commands.
git add lab6.c
git commit -m "Lab 6"
git push