CONNECT 4
(2D Array)
OVERVIEW
Connect 4 is a two-player game in which the players first choose a colour and then take turns dropping their coloured discs from the top into a seven-column, six-row verticallysuspended grid.
Each discs fall straight down, occupying the next available space within the column. The object of the game is to connect four of one's own discs of the same colour next to each other vertically, horizontally, or diagonally before one's opponent can do so. There are many variations on the board size, the most commonly used being 6 7.
char place[6][7]; This creates a char 2d array called place where we will store whats in each slot on the board, we will put it here rather then in main() so all functions can access it. Because the board is 6 by 7 we have used place[6][7] what this does is the same as creating 6 normal arrays of place[7] but all in one.
EXPLANATION ON CODING
This line puts the edges on the squares and displays the value of place inside.
place[a][ b] a is equal to the row and b is equal to the column, because a is taken from the first for loop and b is in another for loop, so place[a][ b] is the value in the
row and column the square is being drawn for. The last line puts the bottom on the square..
This runs in the same way as display() with its for loops but instead of displaying values it is assigning place[a][ b] to ' '.
Next, void display() function will display the board of 6x7 table.
From The first row cout<<" 1 2 3 4 5 6 7\n"; puts numbers on the columns so that people using the game know what the columns are. Notice the \n at the end this is like return/enter it moves down a line, this has the same basic effect of cout<<endl;, notice endl is not inside "" but "\n" is.
The next line for(int a = 0; a<= 5; a++){...} loops adding one on to a each time(a++) until i is above five, so the code inside runs 6 times, each time it makes another row down for the six rows of the board. The code inside makes the six columns each slot is a square which will have the player piece inside, The first line will loop 7 times once for each column, the combination of char(218), char(196) and char(191) puts the top on the square and the " " at the end places a space between it and the next column.
Each of those special characters draw 42 rectangulars that consists of 6 rows and 7 columns.
Drop function will put the piece above the first full square or failing that at the bottom.
It return a int so that it can tell the rest of the program at what row it stopped at(-1 is error value returned). The if(b >=0 && b<= 6) checks the value b given is a vaild number between 0 and 6. If b is greater than or equal to zero and b is smaller than or equal to six it will run, if it isnt the last else statement will run and return a error value of -1.
if (place[0][ b] == ' '), row 0 is the top line so this makes sure that the top is empty, i.e there is somewhere for the players piece to go in the column b (the column the piece is being dropped down).
If its not the second to last else run and returns a error value -1
Now this code will check to see if someone won.
It takes the parameters a and b as integers, a is the column b is the row. We will give it the location of the piece added because only a row of four including the piece added can possibly be made. Each for loops will run checking that if the line is 4 or above it will return true otherwise move to the next pair of loops, at the end if none are 4 or above it will return false. For example, on line 10, for(i = a +1;place[b] == player && i <= 5; i++,vertical++); it adds 1 to a each time therefore checking below the piece dropped, if it is equal to player(the piece dropped) It will run again and add 1 to vertical (used to count number of pieces in vertical line) each time..
Now in main function, we link all the functions;
At loop while (!gamewon), the loop will continues until the game won. It will checks for the hold2 value if there was an error in the last drop. Character 254 and 15 will represent each piece. Character 254 will display and character 15 will display
To determine whose turn, it will check if the last drop is player 2. If it is then it is player 1s turn .
Loop while(true) will stop if its break if the charsPlaced is 42 which means that all columns and rows are full that is 6x7=42. This is to check whether the game is draw.
It gets user input which stores in variable hold which house the user row choice. Its value decremented by 1 to ensure that the array is in ranged index. At if(hold == -1), if hold2 receive -1 from drop function which indicates the column is full therefore it will prompt for another input.
charsPlaced will be increased by 1 shows that another player piece is placed. The system(cls) will clear the board each time and display() will call the display function to show the updated board.
Player 2 will win if the value of player is 15 which has been previously checked in the check function.