C++ Tutorial : Connect 4
Ahhh, another day and another tutorial. As usual, I'm kind of too lazy to do much explaining, so I'll just give you a quick run down of what you need to get your connect 4 game running.
First: we need to draw the board on screen. Just draw a
basic 6x7 board using simple cout statements
HEIGHT 6, WIDTH 7
Second: make an array called board[6][7]
Third: we need to get input from the user: Select (1-7) using a simple cin statement
Fourth: update the array by
placing the number at the end of the array
For example, we select 1 on our first
move. Place the 1 at the end of the array on the first colum which is
array[0][5]
Fifth: check for a winning
combination
Winning Combinations |
If we were to drop our piece on
18, how can we determine if we have a winning combination? First we will make a counter starting at 1 because we already have 1/4 of the combos If we can add up 4 in any direction, then we win For example, if 18+19+20+21 = 4 combos then we win Lets make us 'X' and the computer 'O' If 18 = X, 19 = X, 20 = X, 21 = X, then you win. Counter
= 1+1+1+1 Please note, on an array 18 is actually [3][2] and 19 is [4][2]...etc |
By the way, this game doesn't have any Computer opponent so just play with a friend. If you like to add a Computer player, you can simply make the computer choose a random number for its move. Anyways, that's all for this tutorial ^_^;
Source Code |