Overview: For this project, you are going to create a program to play Connect 4.
Goal: The goal of the project is to work with 2D arrays and create your own classes.
Description: There are two parts to the project, Part A and B. In Part A you will build a Board class that will allow two human players to play Connect 4. In Part B you will be given a Board class to use and you will create a Player class that will be a computer player of Connect 4. After the Board class code for Part B has been posted on Canvas in the last week of class, no late submissions for Part A will be accepted.
Project 5 Part A:
Connect 4 is played on a 7 column by 6 row board. For Part A you will create a Board class that maintains a 2D array of chars representing the board. You will have an X player and an O player and empty spaces should be represented using e or . (Warning: be consistent about using the letter O not the number 0, otherwise youre likely to end up with a bug that is very hard to track down.)
Your Board class should have a default constructor which initializes the board to be empty (filled with the letter e). In terms of general functions, you will probably need at least the following:
printBoard: This function should print the Connect 4 board to the screen in something like the following manner: see image.
checkMove (int col): This function should check to make sure that the move being proposed is a valid move and return true or false appropriately. A move is invalid if the column number selected is outside of bounds (less than 0 or greater than 6) or the column selected is already full. Note: were forcing the user to use array style numbering: 0-6 not 1-7.
makeMove (char player, int col): This function will update the board to reflect a new move by either player X or O. The column number is passed in, but in which row to place the char is something you will need to calculate.
isFull: checks to see if the board is completely full. This will be used in detecting a stalemate.
hasWon(char player): This function will detect whether the player in question has won. It returns true if they have and false if they have not. You should use at least one loop to test for the win; you really wouldnt want to simply hardcode each possible winning combination. (refer to the solution from the Tic-Tac-Toe lab)
hostGame: This function will actually run a game of Connect 4. It should take turns getting moves from the two users, test whether the moves are valid and detect when the game has terminated, either due to a win or a stalemate. You should print an appropriate message at the end of the game indicating the outcome. If the user enters an invalid move, they should be re-prompted until they enter a correct one. You can assign player X to always go first and player O to always go second. You should reprint the board after each move so that the user can decide where to make their move.
Others: you may need to implement other functions either as part of your class or outside of it, to complete your project. Your main ( ) method should be very simple, involving little more than creating a Board object and calling the hostGame function.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.