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

connect4.cpp: #include \"connect4.h\" Connect4::Connect4() { ClearBoard(); } Con

ID: 3912878 • Letter: C

Question

connect4.cpp:
#include "connect4.h"


Connect4::Connect4()
{
   ClearBoard();
}


Connect4::~Connect4()
{
   // Intentionally empty
}


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


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


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


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 << " ";

      // Draw board contents
      cout << "| ";
      for (int c = 0; c < COLS; c++)
         cout << board[r][c] << " | ";
      cout << " ";
   }

   // Draw dashed line
   cout << "+";
   for (int c = 0; c < COLS; c++)
      cout << "---+";
   cout << " ";

   // Draw column numbers
   cout << " ";
   for (int c = 0; c < COLS; c++)
      cout << c << "   ";
   cout << " ";
}

Explanation / Answer

A squestion stated game AI searches random numbers for making a move i have compiled this code in codeblocks and it works fine.

Hope it helps!!

#include <iostream>
#include<stdlib.h>
const int WIN=4;
const int ROWS=6;
const int COLS=7;
using namespace std;
class Connect4{
public:
    Connect4();
    ~Connect4();
    void ClearBoard();
    bool MakeMove(int col,char player);
    bool CheckWin(char player);
    void PrintBoard();

private:
    char board[ROWS][COLS];
    int count[COLS];

};

Connect4::Connect4()
{
   ClearBoard();
}


Connect4::~Connect4()
{
   // Intentionally empty
}


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


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


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


void Connect4::PrintBoard()
{
   // Print the Connect4 board
   for (int r = ROWS-1; r >= 0; r--)
   {
      // Draw dashed line
      std::cout << "+";
      for (int c = 0; c < COLS; c++)
         std::cout << "---+";
      std::cout << " ";

      // Draw board contents
      std::cout << "| ";
      for (int c = 0; c < COLS; c++)
         std::cout << board[r][c] << " | ";
      std::cout << " ";
   }

   // Draw dashed line
   std::cout << "+";
   for (int c = 0; c < COLS; c++)
      std::cout << "---+";
   std::cout << " ";

   // Draw column numbers
   std::cout << " ";
   for (int c = 0; c < COLS; c++)
      std::cout << c << "   ";
   std::cout << " ";
}
int main(){
int col;
int i=0;
int f=0;
Connect4 c4;
while(1){
c4.ClearBoard();
c4.PrintBoard();
cout<<"Player 1 is x Computer is o ";
while(f!=42){       //condition when all the blocks are filled
if(i==0){
    cout<<"Player 1 Turn";
    do{
        cin>> col;
    }while(!c4.MakeMove(col,'x')); //checking for valid move
    if(c4.CheckWin('x')){           //
        cout<<"Player X Won!!";
        break;
    }
    i=1;
}
else{
    cout<<"Computer Turn :";
     do{
        col=rand()%7;           //Used to find random numbers
    }while(!c4.MakeMove(col,'o'));
    cout<<col;
    cout<<' ';
    if(c4.CheckWin('o')){
        cout<<"Computer Won!!";
        break;
    }
    i=0;
}
f+=1;
c4.PrintBoard();
}
if(f==42){
    cout<<"Draw ";
}
}
return 0;

}