Menu

(Solved) : 1 Objectives Objective Project Create Connect Four Game C Complete Given C Code Necessary Q34191446 . . .

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

2. Project description
Connect Four game is a game of strategy. Each player tries tocreate a range of four pieces aligned
horizontally, vertically or diagonally while preventing hisopponent to do same.
Setup:
1. Follow the illustrated instructions to assemble the plasticparts (not done here)
2. Place the grid between the two players and each player selects acolor
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 willbe the second player during a
second game.
2. It is a two-player game, each player drops one of his coloredpieces into the grid.
3. The game continues until one of the players have a continuousalignment of his four pieces for
his color. The alignment can be vertical, horizontal ordiagonal.
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 andseven 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 spacescannot contain coins (whether the coins
are red or yellow). Therefore, these supplementary spaces arealways empty.
To conclude,
– a space can be empty, or it is filled with a red disc, or with ayellow disc
– the board grid is initially made up of six rows and seven columnsbut when we add a border, the
board size is increased to eight rows and nine columns (but thesupplementary spaces are always
empty).
Define the type space and the type grid necessary to represent thegrids 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 programthat does this operation.

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

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

654321 0*0*3 1 654321

           Figure 1: Displaying the grid

Hint: Create two sub programs display_column_numbers(), todisplay the top elements and
display_Grid(…), to display the whole grid as shown above.
– One may call display_column_numbers() in the sub programdisplay_Grid(…) to
display the first horizontal line and the last horizontal line ofelements.
– Find a way to display the middle set of elements inside the samesub 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 agiven 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 typeBoolean 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 thetop of a grid column, that piece keeps falling
in the grid column until it is held by another piece or by thefloor of the grid.
Write a sub program that realizes this operation: it determines thenew 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 subprogram should insert this disc on top of
the existing ones in the column (if any). Therefore, we shouldidentify the height of fall of the disc for a
given column (this can be done by creating a sub programheight_of_fall(…)) and after, we
create the sub program drop_disc(…) that makes sure the column isnot full already, identifies the
height of fall and then assigns to the corresponding entry on theboard the disc color.

654321 o**7 *0006 4*000**4 1 654321

        Figure 2: The redshave played column 5

Exercise 6 : Counting the discs aligned
Write a sub program that indicates the length of the longestalignment of a given coin (identified by its
column number and its row number). The alignments should besearched 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 analignment 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 thegrid?

Exercise 7 : Recommending to play on a column
Write a sub program such that when a grid is given, it recommends acolumn 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 numberthat allows the longest alignment.
Remark: If a winning move exist, the recommended column shall leadto a win.
Example: According to figure 2, the yellows will be recommended toplay column 5. This will provoke
the alignment of 4 pieces and therefore a win.

654321 0*0*3 1 654321 654321 o**7 *0006 4*000**4 1 654321 Show transcribed image text

Expert Answer


Answer to 1 Objectives Objective Project Create Connect Four Game C Complete Given C Code Necessary Q34191446 . . .

OR