1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mars1129 [50]
3 years ago
6

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
Engineering
1 answer:
Mariana [72]3 years ago
4 0

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

}

You might be interested in
james wants to qualify for icp are and licensure. Which degree would be required in order to qualify for a two year master of ar
ololo11 [35]

Answer:

A degree in architecture with 60 credit hours.

Explanation:

The requirements need for a student to qualify for a two year master of architecture degree are;

  • 60 credit hours in architecture
  • Complete 60 credit hours in related area of profession such as; planning, landscape architecture ,public health and others.
  • 45 credit hours in architecture course at the level of 500/600
4 0
3 years ago
Technician a says that if a tapered roller bearing is adjusted to loose
Effectus [21]
The technician is true
5 0
2 years ago
What is A roofed structure that is similar to a porch, but is detached from the house.
agasfer [191]

Answer:

a gazebo

Explanation:

6 0
3 years ago
Read 2 more answers
Robomind academy code if hour
Anvisha [2.4K]
Erm what? ......;-;
7 0
3 years ago
A fully braced structural member in a building is subjected to several different loads, including roof loads of D = 5 k and L_r
allochka39001 [22]

Answer: 37.4K

Explanation:

See attachment

7 0
3 years ago
Other questions:
  • At a high school science fair, Connor won first place for his replica of the Golden Gate Bridge. Connor liked the project so muc
    5·1 answer
  • If x &lt; 5 and x &gt;c, give a value of c such that there
    9·1 answer
  • Assuming that the following three variables have already been declared, which variable will store a Boolean value after these st
    14·1 answer
  • There are two questions about SolidWorks.
    9·1 answer
  • An engineer is considering time of convergence in a new Layer 3 environment design. Which two attributes must be considered? (Ch
    15·1 answer
  • Wet steam at 15 bar is throttled adiabatically in a steady-flow process to 2 bar. The resulting stream has a temperature of 130°
    7·1 answer
  • The following displacement (cm), output (V) data have been recorded during a calibration of an LVDT. Displacement (cm), output (
    15·1 answer
  • Determine the reactor volume (assume a CSTR activated sludge aerobic reactor at steady state) required to treat 5 MGD of domesti
    5·1 answer
  • Fixed rate mortgage offer:
    9·2 answers
  • Technician A says when you push the horn button, electromagnetism moves an iron bar inside the horn, which opens and closes cont
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!