computer language C++ (Connect 4 game)( this is all the info that was givin no input or solution) I used the most recent version of Microsoft visual studio for this program)Your solution must have these classes and at least these fields / methods. Your program must use the methods described in a meaningful fashion. You can create additional fields or methods but these the below should be core methods in your solution.Checkers classfields:colormethods:Constructorint getColor()string toString() - returns an attractive string representation of itselfBoard classfields:2D array of checkers - represents the game board (6 row by 7 column)methods:Constructorstring toString() - returns an attractive string representation of itselfThis needs to be an attractive table representation of the game board with aligned rows / columns with characters representing blank cells, cells with a red checkers in it and cells with a gold checkers in it.int dropChecker(Checker, int) - drops a checker object down a particular slot. Returns an int indicating success or not.int isWinner() - returns code number representing no winner or player number of winnerPlayer classfields:namecolormethods:Constructorget / set methods for fieldsstring toString() - string representation of itself.Example String representation of the board (X means blank, R means red checker, G means gold checker)X X X X X X XX X X X X X XX X X X X G XX X X X X R XX X X X X R XX R G G G R GMainYour program should create two Players and a Board. It should represent the initial Board to the screen.You should then alternate between player 1 and player 2 and randomly drop checkers down slots in the board. You should represent the board after each checker drop.This should stop if there is a winner or if the board fills up.You should be turning in a Checker.h, Checker.cpp, Board.h, Board.cpp, Player.h, Player.cpp and a main.cpp

Respuesta :

Answer:

C++ code explained below

Explanation:

#include "hw6.h"

//---------------------------------------------------

// Constructor function

//---------------------------------------------------

Connect4::Connect4()

{

ClearBoard();

}

//---------------------------------------------------

// Destructor function

//---------------------------------------------------

Connect4::~Connect4()

{

// Intentionally empty

}

//---------------------------------------------------

// Clear the Connect4 board

//---------------------------------------------------

void Connect4::ClearBoard()

{

// Initialize Connect4 board

for (int c = 0; c < COLS; c++)

for (int r = 0; r < ROWS; r++)

board[r][c] = ' ';

// Initialize column counters

for (int c = 0; c < COLS; c++)

count[c] = 0;

}

//---------------------------------------------------

// Add player's piece to specified column in board

//---------------------------------------------------

bool Connect4::MakeMove(int col, char player)

{

// Error checking

if ((col < 0) || (col >= COLS) || (count[col] >= ROWS))

return false;

// Make move

int row = count[col];

board[row][col] = player;

count[col]++;

return true;

}

//---------------------------------------------------

// Check to see if player has won the game

//---------------------------------------------------

bool Connect4::CheckWin(char player)

{

// Loop over all starting positions

for (int c = 0; c < COLS; c++)

for (int r = 0; r < ROWS; r++)

if (board[r][c] == player)

{

// Check row

int count = 0;

for (int d = 0; d < WIN; d++)

if ((r+d < ROWS) &&

(board[r+d][c] == player)) count++;

if (count == WIN) return true;

 

// Check column

count = 0;

for (int d = 0; d < WIN; d++)

if ((c+d < COLS) &&

(board[r][c+d] == player)) count++;

if (count == WIN) return true;

 

// Check first diagonal

count = 0;

for (int d = 0; d < WIN; d++)

if ((r+d < ROWS) && (c+d < COLS) &&

(board[r+d][c+d] == player)) count++;

if (count == WIN) return true;

 

// Check second diagonal

count = 0;

for (int d = 0; d < WIN; d++)

if ((r-d >= 0) && (c+d < COLS) &&

(board[r-d][c+d] == player)) count++;

if (count == WIN) return true;

}

return false;

}

//---------------------------------------------------

// Print the Connect4 board

//---------------------------------------------------

void Connect4::PrintBoard()

{

// Print the Connect4 board

for (int r = ROWS-1; r >= 0; r--)

{

// Draw dashed line

cout << "+";

for (int c = 0; c < COLS; c++)

cout << "---+";

cout << "\n";

// Draw board contents

cout << "| ";

for (int c = 0; c < COLS; c++)

cout << board[r][c] << " | ";

cout << "\n";

}

// Draw dashed line

cout << "+";

for (int c = 0; c < COLS; c++)

cout << "---+";

cout << "\n";

// Draw column numbers

cout << " ";

for (int c = 0; c < COLS; c++)

cout << c << " ";

cout << "\n\n";

}

//---------------------------------------------------

// Main program to play Connect4 game

//---------------------------------------------------

int main()

{

  int choice;

  int counter = 0;

  srand (time(NULL));

  Connect4 board;

  cout << "Welcome to Connect 4!" << endl << "Your Pieces will be labeled 'H' for human. While the computer's will be labeled 'C'" << endl;

  board.PrintBoard();

  cout << "Where would you like to make your first move? (0-6)";

  cin >> choice;

  while (board.MakeMove(choice,'H') == false){

  cin >> choice;

  }

  counter++;

  while (board.CheckWin('C') == false && board.CheckWin('H') == false && counter != 21){

  while (board.MakeMove(rand() % 7, 'C') == false){}

  board.PrintBoard();

  cout << "Where would you like to make your next move?" << endl;

  cin >> choice;

  board.MakeMove(choice,'H');

  while (board.MakeMove(choice,'H') == false){

  cin >> choice;

  }

  counter++;

  }

 

  if (board.CheckWin('C')){

  cout << "Computer Wins!" << endl;}

  else if (counter == 21){cout << "Tie Game!" << endl;}

  else {cout << "Human Wins!" << endl;}

  board.PrintBoard();

}