Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ Write a class called CFBoard that represents a game board for playing

ID: 3895046 • Letter: U

Question

Using C++

Write a class called CFBoard that represents a game board for playing Connect Four.. It should have:

a 7x6 array of char as a data member, which will store the locations of the players' pieces.

a variable called gameState  that holds one of the four following values: X_WON, O_WON, DRAW, or UNFINISHED - use an enum type for this, not string (the enum definition should go in CFBoard.hpp, before the class, not inside it).

a default constructor that initializes all elements of the array to being empty (you can use whatever character you want to represent that a square is empty) and initializes the gameState to UNFINISHED.

a method called makeMove that takes the column of the move (1-7, see the example below) and which player's turn it is ('x' or 'o') as parameters. If the gameState is not UNFINISHED, or the selected column is already full, then makeMove should just return false. Otherwise, makeMove should record the move, call the updateGameState method (described next), and return true. The piece that is played should "fall" to the "lowest" unoccupied square of that column (see the example below).

a method called updateGameState. This method takes the array indices of the piece that was just played and updates the gameState if the game has now been won or drawn. The first parameter will be a row index (0-5) and the second parameter will be a column index (0-6).  The game has been won when either player has four pieces in a row either orthogonally or diagonally. The game has been drawn when the board is completely full and neither player has won. Since this method is just for internal use, it would normally be private, but I want you to make it public so I can test it separately.

a method called getGameState that just returns the value of gameState.

optional: a method called print, which just prints out the current board to the screen - this is not required, but will very likely be useful for debugging.

Here's a sample excerpt of a game in progress:

Next x plays in column 6:

Next o plays in column 5:

Et cetera.

Whether you think of the array indices as being [row][column] or [column][row] doesn't matter as long as you're consistent. Also, whether the zero row is the "top" or "bottom" doesn't matter as long as you're consistent (same for "left" and "right").

Your class only represents the board, it doesn't actually allow two players to play the game. Other code (that you don't have to write) would use your Board class to make that happen.

The files must be named: CFBoard.hpp and CFBoard.cpp.

Explanation / Answer

Answer:

Here, I have following code as shown below with sample output:

CFBoard.hpp:

// define a structure

struct playerInfo

{

char playerName[81];

char playerID;

};

int PlayerDrop( char board[][10], playerInfo activePlayer );

void Checks ( char board[][10], playerInfo activePlayer, int dropChoice );

void PrintBoard ( char board[][10] );

int CheckFours ( char board[][10], playerInfo activePlayer );

int Fully_Board( char board[][10] );

void PlayerWin ( playerInfo activePlayer );

int Restart ( char board[][10] );

CFBoard.cpp:

#include <iostream>

// define a header file

#include "CFBoard.h"

using namespace std;

// main function

int main()

{

// create an object

playerInfo pOne, pTwo;

// declare array of char

char board[9][10];

int Choices, wit, full, more;

// print

cout << "Let's Play Connect 4" << endl << endl;

cout << "Player One, Enter your Name: ";

// get user input

cin >> pOne.playerName;

pOne.playerID = 'X';

cout << "Player Two, Enter your Name: ";

cin >> pTwo.playerName;

pTwo.playerID = 'O';

full = 0;

wit = 0;

more = 0;

// calls the methods

PrintBoard( board );

do

{

// assign

Choices = PlayerDrop( board, pOne );

Checks( board, pOne, Choices );

PrintBoard( board );

wit = CheckFours( board, pOne );

// checks

if ( wit == 1 )

{

// calls

PlayerWin(pOne);

more = Restart(board);

// checks

if (more == 2)

{

break;

}

}

// calls and assign

Choices = PlayerDrop( board, pTwo );

Checks( board, pTwo, Choices );

PrintBoard( board );

// calls and assign

wit = CheckFours( board, pTwo );

if ( wit == 1 )

{

PlayerWin(pTwo);

more = Restart(board);

// check and compared

if (more == 2)

{

break;

}

}

full = Fully_Board( board );

if ( full == 7 )

{

cout << "The board is full, it is a draw!" << endl;

more = Restart(board);

}

}while ( more != 2 );

return 0;

}

// define a method

int PlayerDrop( char board[][10], playerInfo ActivePlayer )

{

int Choices;

// loop

do

{

cout << ActivePlayer.playerName << "'s Turn ";

cout << "Please enter a number between 1 and 7: ";

cin >> Choices;

// using loop

while ( board[1][Choices] == 'X' || board[1][Choices] == 'O' )

{

cout << "That row is full, Enter a new row: ";

cin >> Choices;

}

}while ( Choices < 1 || Choices > 7 );

return Choices;

}

// define a method

void Checks ( char board[][10], playerInfo aPlayer, int Choices )

{

int length, turn;

length = 6;

turn = 0;

do

{

// checks and compared

if ( board[length][Choices] != 'X' && board[length][Choices] != 'O' )

{

board[length][Choices] = aPlayer.playerID;

turn = 1;

}

else

--length;

}while ( turn != 1 );

}

// define a method

void PrintBoard ( char board[][10] )

{

int Rows = 6, Columns = 7, i, ix;

for(i = 1; i <= Rows; i++)

{

cout << "|";

// use loop

for(ix = 1; ix <= Columns; ix++)

{

// checks

if(board[i][ix] != 'X' && board[i][ix] != 'O')

board[i][ix] = '*';

cout << board[i][ix];

}

cout << "|" << endl;

}

}

// define a method

int CheckFours ( char board[][10], playerInfo aPlayer )

{

// declare variables

char XO;

int win;

XO = aPlayer.playerID;

win = 0;

// using loop

for( int i = 8; i >= 1; --i )

{

for( int ix = 9; ix >= 1; --ix )

{

// checks and compare

if( board[i][ix] == XO &&

board[i-1][ix-1] == XO &&

board[i-2][ix-2] == XO &&

board[i-3][ix-3] == XO )

{

win = 1;

}

if( board[i][ix] == XO &&

board[i][ix-1] == XO &&

board[i][ix-2] == XO &&

board[i][ix-3] == XO )

{

win = 1;

}

// checks and compare

if( board[i][ix] == XO &&

board[i-1][ix] == XO &&

board[i-2][ix] == XO &&

board[i-3][ix] == XO )

{

win = 1;

}

// checks and compare

if( board[i][ix] == XO &&

board[i-1][ix+1] == XO &&

board[i-2][ix+2] == XO &&

board[i-3][ix+3] == XO )

{

win = 1;

}

// checks and compare

if ( board[i][ix] == XO &&

board[i][ix+1] == XO &&

board[i][ix+2] == XO &&

board[i][ix+3] == XO )

{

win = 1;

}

}

}

return win;

}

// Define a method

int Fully_Board( char board[][10] )

{

int full;

full = 0;

// use loop

for ( int i = 1; i <= 7; ++i )

{

// checks and compare

if ( board[1][i] != '*' )

++full;

}

return full;

}

// define a method

void PlayerWin ( playerInfo aPlayer )

{

// print

cout << endl << aPlayer.playerName << " Connected Four, You Win!" << endl;

}

// define a method

int Restart ( char board[][10] )

{

int Restart;

cout << "Would you like to Restart? Yes(1) No(2): ";

cin >> Restart;

// checks and compare

if ( Restart == 1 )

{

for(int i = 1; i <= 6; i++)

{

for(int ix = 1; ix <= 7; ix++)

{

board[i][ix] = '*';

}

}

}

else

cout << "Bye.!!" << endl;

return Restart;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote