1. Engineering
2. Computer Science
3. 1 objectives the objective of this project is to create...

# Question: 1 objectives the objective of this project is to create...

###### Question details

1. Objectives
The objective of this project is to create a “Connect Four Game” in C++. Complete the given C++ code where necessary.

2. Project description
Connect Four game is a game of strategy. Each player tries to create a range of four pieces aligned
horizontally, vertically or diagonally while preventing his opponent to do same.
Setup:
1. Follow the illustrated instructions to assemble the plastic parts (not done here)
2. Place the grid between the two players and each player selects a color
Goal of the game:
Become the first player to align four of these pieces horizontally, vertically or diagonally.
Rules of the Game :
1. Select the first player. The player beginning a first game will be the second player during a
second game.
2. It is a two-player game, each player drops one of his colored pieces into the grid.
3. The game continues until one of the players have a continuous alignment of his four pieces for
his color. The alignment can be vertical, horizontal or diagonal.
4. To empty the grid, push the bar below for the pieces to fall. After that, you can begin the game
all over again.

Exercise 1 : Preparation for the game

Connect-Four game is played on a vertical grid of six rows and seven columns with 21 red pieces and 21
yellow pieces. (the piece is also called a disc or a coin).
To easy future calculations, we shall add a border to this grid. Its dimensions will then become eight rows
and nine columns. Hope we agree that these supplementary spaces cannot contain coins (whether the coins
are red or yellow). Therefore, these supplementary spaces are always empty.
To conclude,
- a space can be empty, or it is filled with a red disc, or with a yellow disc
- the board grid is initially made up of six rows and seven columns but when we add a border, the
board size is increased to eight rows and nine columns (but the supplementary spaces are always
empty).
Define the type space and the type grid necessary to represent the grids of the game.
Hint: Use enum and typedef.

Exercise 2 : Empty the grid
To begin each game, we need to empty the grid. Write a sub program that does this operation.

Exercise 3 : Display the grid
By convention, the red colored pieces shall be represented by the asterisk symbol (*) and the yellow pieces
by letter o (in lower case).
To facilitate a space (entry) location on this grid, the row and column numbers will be displayed as in
figure 1 below.

Write a sub program that displays the content of a grid (e.g. as shown in figure 1 below)

Figure 1: Displaying the grid

Hint: Create two sub programs display_column_numbers(), to display the top elements and
display_Grid(…), to display the whole grid as shown above.
- One may call display_column_numbers() in the sub program display_Grid(…) to
display the first horizontal line and the last horizontal line of elements.
- Find a way to display the middle set of elements inside the same sub program
display_Grid(…).

Exercise 4 : Determine if a move is legal or not
Write a sub program that indicates if it is possible to play a given column or not.
Example: on figure 1, it is possible to play on columns 1, 2, 3, 5, 6 or 7. It is not possible to play on
column 4.

Hint: create a function of type Boolean (a function of type Boolean only returns two possible values: 0
to say false or 1 to say true). E.g. in C++, when we do

Exercise 5 : Dropping a coin
When a player drops a disc of his initially selected color at the top of a grid column, that piece keeps falling
in the grid column until it is held by another piece or by the floor of the grid.
Write a sub program that realizes this operation: it determines the new state of the grid when a disc is
dropped from the top of a given column.

Example: According to figure 1, if the reds play on column 5, the new state of the grid is that given by
figure 2.

Hint: when a player drops a disc (for a given column), our sub program should insert this disc on top of
the existing ones in the column (if any). Therefore, we should identify the height of fall of the disc for a
given column (this can be done by creating a sub program height_of_fall(…)) and after, we
create the sub program drop_disc(…) that makes sure the column is not full already, identifies the
height of fall and then assigns to the corresponding entry on the board the disc color.

Figure 2: The reds have played column 5

Exercise 6 : Counting the discs aligned
Write a sub program that indicates the length of the longest alignment of a given coin (identified by its
column number and its row number). The alignments should be searched in any order (horizontally,
vertically and diagonally).
This sub program will then be used to determine the end of the game (when alignment ≥ 4) or to help the
computer select the column to play (exercise 8).
On the example (in figure 2), space (1,1) corresponds to an alignment of three discs at a diagonal; space
(7,1) corresponds to the alignment of two discs vertically; space (6,2) corresponds to three discs aligned
vertically or diagonally, …
Indication: why did we create a supplementary border around the grid?

Exercise 7 : Recommending to play on a column
Write a sub program such that when a grid is given, it recommends a column to play. The column number
should be randomly chosen.
Hint: may use the function rand() present in C++.

Exercise 8 : Ameliorating the recommendation
Let us make the work better. We wish to recommend the column number that allows the longest alignment.
Remark: If a winning move exist, the recommended column shall lead to a win.
Example: According to figure 2, the yellows will be recommended to play column 5. This will provoke
the alignment of 4 pieces and therefore a win.