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();
}