Practical One COMP200 - Practicals 21 February 2023
COMP200: Practical 1
Connect Four is a two-player game played with discs of two colours (red and blue). The two players select
a color, then take turns dropping their coloured discs from the top into a vertically suspended grid. The
pieces fall straight down, occupying the next available space within the column. The object of the game is
to get four of the same coloured discs next to each other vertically or horizontally or diagonally before one’s
opponent can do so.
Figure 1: Connect Four
In this computer version, the grid is represented by a two-dimensional array with 7 columns and 6 rows.
The program prompts two players to drop a red or blue disk alternately. Whenever a disk is dropped, the
program re-displays the board on the screen and determines the status of the game:
• Win: if a player has four of his discs next to each other either vertically, horizontally or diagonally.
• Draw: neither player has won and no more discs can be dropped because all cells in the grid are full.
• Continue: neither player has won and the game can continue.
Here is a sample run of the game:
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
---------------
E n t e r a c o l u m n (0 -6) to d r o p a r e d d i s k : 0
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | | | | |
---------------
E n t e r a c o l u m n (0 -6) to d r o p a b l u e d i s k : 3
| | | | | | | |
COMP200 1 2022
Practical One COMP200 - Practicals 21 February 2023
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | |B| | | |
---------------
E n t e r a c o l u m n (0 -6) to d r o p a r e d d i s k : 0
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | | | | |
|R| | |B| | | |
---------------
// more moves o m i t t e d :
E n t e r a c o l u m n (0 -6) to d r o p a b l u e d i s k : 6
| | | | | | | |
| | | | | | | |
| | | |R| | | |
| | | |B|R|B| |
|R| |R|B|B|B|B|
|R|B|R|B|R|R|R|
---------------
Blue player wins .
F o u r c o n s e c u t i v e d i s c s in r o w 4.
The program consists of two classes: ConnectFour and ConnectFourApp. The class ConnectFour has one field:
a two-dimensional array of integers to represent the grid. Only the integers 0, 1 and -1 are stored in the
array. A 0 means that the cell in the array is empty (does not contain any discs), while 1 represents a red
disc and -1 represents a blue disc.
1. Complete the methods in the ConnectFour class:
• public void print() that prints out the grid as shown in the sample output.
• public static void dropDisc (int disc, int col) that drops the disc in the first available row
of the column specified.
• public boolean isDraw() that returns true if the game is a draw, false otherwise. The game is a
draw if there are no moves left (i.e. all columns are full).
• public int checkWin() that returns 1 if the red player has won, -1 if the blue player has won, 0 if
neither player has won. A player has won if she has four of the same colored discs next to each
other vertically or horizontally or diagonally.
2. Add code to ConnectFourApp: it has a main method to run the game (alternate turns between players
until the game ends, then display the result) by using the class ConnectFour and its methods.
COMP200 2 2022