For this project we will be playing Connect 4. Well not actually but close. We will be writing 2 classes that will allow us to play the game. For more information on Connect 4 check out this Wikipedia page.
There are 2 main classes for this project. They are Board and Game. Full details can be found in the API. In general, the Game class is the smallest class and only has two strings and a board as private fields. It has a constructor and one other method, playGame. The playGame method is the method that will handle dropping the checkers, checking to see if the game has been won or if a draw has occurred.
The basic algorithm is as follows:
While the game isn't over
determine whose turn it is
determine what command was issued
The commands are described below in the Command section.
A player's turn is considered over, when they have successfully dropped a checker. Displaying the board, dropping a checker in a full column or dropping a checker in a row that is not on the board does not end their turn.
The Board class is the largest and most complex of the classes. It has methods to allow a checker to be dropped, to tell if there was a win or a draw. There are many ways to tell if there are 4 in a row and you can choose any method you like.
Only the public methods are required for the classes, except where noted below; the private methods, if any, are up to you.
There are two commands, one for dropping a checker and one for displaying the board.
Dropping a checker:
drop in <col>
-- this command will drop a checker into the column with the given column number. The column number should be between 1 and 7. The Game class will need to translate the column number to match the columns in the array, which are 0 to 6. Use the player's avatar as the checker. The avatar is most likely their first letter from their name, but doesn't have to be. So use the avatar as the checker.
display board
-- this command will print the board. It should be like this:
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
This is obviously for an empty board. When there are checkers in the board, you should display the avatar in the space for the checker. So if the avatar is one character, then the same size and shape is retained.
Or, if there are two players whose names are Dave and Kelly, and Dave has won, then the board would look like this:
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| |D| | | | | |
---------------
| |K|K| | | | |
---------------
| |K|D|D|D|D| |
---------------
|D|K|D|K|K|D|K|
---------------
Input
Name: Dave
Avatar: D
drop in 1
display board
drop in 1
display board
drop in 1
display board
drop in 3
display board
drop in 4
display board
drop in 4
drop in 4
drop in 4
Output
When the game is over, display the board and then indicate who has won or if the game was a draw. Note: The output below is not from the input above:
---------------
| | | | | | | |
---------------
| | | | | | | |
---------------
| |D| | | | | |
---------------
| |K|K| | | | |
---------------
| |K|D|D|D|D| |
---------------
|D|K|D|K|K|D|K|
---------------
Game over: Dave won
So when the game ends, you will output the words: Game over: and then if there was a winner, then output the name of the winner and the word won. If no one has won, then say this:
Game over: Draw game
So, here is a file you can use for a main. This has two commented out lines. It uses an input file player2.txt as the input for player two. You can use this file, make your own, or use cin for both players. You can also make another input file for player 1 and use two files. How the input gets to the play game method is how the game will be played.
Zip up your Game.h, Game.cpp, Board.h and Board.cpp and turn them in to Web-CAT.
This is due on Wednesday April 2th Thursday April 3, by 11:55PM