Question: * `ConnectFour.cpp` is provided, but is empty. You will need to implement the out of class (OOC) constructor and all methods in this file. Don't
* `ConnectFour.cpp` is provided, but is empty. You will need to implement the "out of class" (OOC) constructor and all methods in this file. Don't forget to use the name of the class and the scope resolution operator appropriately. * You may NOT use the "using namespace std" statement in in `ConnectFour.cpp` class. `(-2 points if you do)`.
- Your `ConnectFour` constructor should initialize all private member variables * It must use a constructor initializer list for all private members except the game board `(-2 points if you fail to use a constructor initializer list)` * Note that the constructor will indicate how many players want to play based on the number of unique player tokens it receives as its 2nd argument. * Your constructor can use a for-loop to initialize the game board, by considering the 'initTokens' argument of the constructor. The vector 'initTokens' is optionally specified to allow any game to be started from an existing board of valid player tokens (you can assume that any initTokens vector you are given is in a valid state that does not break any of the rules of the game). * Your constructor prototype will provide defaults for all arguments, so that you can instance your game in multiple ways (recall that the defaults live in the `ConnectFour.h` file, so you do not need to worry about the defaults for the constructor or methods.
- Your `printInstructions()` method should print out a brief description about how to play the game.
- Your `printBoard()` method should print out the current state of the game board, as shown above
- Your `nextPlayer()` method should increment the private `currentPlayerId` member of the class (from zero to `playerTokens.size()` ) , but should start over at 0, after the last player has had a turn. (connect four is usually played with 2 players, but it should support an arbitrary number of players as long as you use the size of the `playerTokens` vector to help you determine the total number of players in the game.
- Your `getCurrentPlayerToken()` method should simply return the character that represents the `currentPlayerId` from within the private `playerTokens` vector
- Your `takeTurn()` method is responsible for communicating who's turn it is, and for collecting input from the current player * It should prompt the current player for a column number, and then process their turn. * It should test to ensure that the number received is a valid column number from 1 to 7, otherwise return `false`. * It should test that column chosen has space for a token to be "dropped in from the top" of the column, otherwise return `false` * If the column has space for a token, it should place the current player's token in the column at its "lowest" empty space on the screen (like the game) * It should NOT advance to the next player ( the `play()` method will do that by calling `nextPlayer()` at the right time). * It should return `true` after placing the current player's token on the game board, (and `false` otherwise, per above).
- Your `isWin()` method will check ONLY if the CURRENT player has won the game * It should set the integer value for the `winningPlayerId` if the current player has won the game (otherwise it should not change the winningPlayerId) * It should return `true` if the current state of the board represents a win for the current player, otherwise `false`
- Your `isTie()` method should determine if the game cannot be won by either player regardless of the number of moves. * If there are no moves left on the board (no empty spaces), then return `true` (a tie), else `false`
- Your game must support 3 or more players, which should be easy to achieve if you have followed the above directions. Test your code by simply calling your constructor from `main()` using more than 2 player tokens
- Your `getWinningPlayerId()` method must return the ID of the value of the winningPlayerId data member of the class (NOT the TOKEN of the winning player). Recall that your winningPlayerId member must be set by the isWin() method.
Here's the code that was provided for me already. connectfour.cpp and we need to implement it using the above instructions.
#include
void ConnectFour :: play(){ bool isGoodMove = false; this->printInstructions(); // print instructions this->printBoard(); // print the board while( !this->isWin() && !this->isTie()) { // keep playing as long as the game is not won or tied isGoodMove = this->takeTurn(); // current player takes a turn if (isGoodMove) { this->printBoard(); // print the board if that was a good move if (this->isWin()) break; // if that was a winning move, quit the game if(this->isTie()) break;// else if the game is tied, quit the game this->nextPlayer(); // else, advance to the next player in the game. } // else that was an illegal move } // end while: game over if (this->getWinningPlayerId() == -1) std::cout << "Sorry - It looks like the game was a tie." << std::endl; else std::cout << "Congratulation: Player #" << this->getCurrentPlayerToken() << " has won the game!" << std::endl; std::cout << "Goodbye!" << std::endl; } // end play
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
