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]
2 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]2 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
Tesla Is the best ELECTRIC car brand, Change my mind
pochemuha

Answer:You are correct, no need to change.

Explanation:

5 0
3 years ago
Read 2 more answers
Which best explains Susan B Anthony purpose in her speech “Women’s rights to the suffrage”
Furkat [3]

Answer:

She wrote and delivered a speech in 1873, which came to be known as the “Women's Rights to the Suffrage” speech. In her address, she lets the audience know of her “crime” of voting. She reminds the listener that the Constitution of the United States says “we the people” and does not exclude women as people

6 0
2 years ago
Read 2 more answers
A pipeline (NPS = 14 in; schedule = 80) has a length of 200 m. Water (15℃) is flowing at 0.16 m3/s. What is the pipe head loss f
dangina [55]

Answer:

Head loss is 1.64

Explanation:

Given data:

Length (L) = 200 m

Discharge (Q) = 0.16 m3/s

According to table of nominal pipe size , for schedule 80 , NPS 14,  pipe has diameter (D)= 12.5 in or 31.8 cm 0.318 m

We know, head\ loss  = \frac{f L V^2}{( 2 g D)}

where, f = Darcy friction factor

V = flow velocity

g = acceleration due to gravity

We know, flow rate Q = A x V

solving for V

V = \frac{Q}{A}

    = \frac{0.16}{\frac{\pi}{4} (0.318)^2} = 2.015 m/s

obtained Darcy friction factor  

calculate Reynold number (Re) ,

Re = \frac{\rho V D}{\mu}

where,\rho = density of water

\mu = Dynamic viscosity of water at 15 degree  C = 0.001 Ns/m2

so reynold number is

Re = \frac{1000\times 2.015\times 0.318}{0.001}

            = 6.4 x 10^5

For Schedule 80 PVC pipes , roughness (e) is  0.0015 mm

Relative roughness (e/D) = 0.0015 / 318 = 0.00005

from Moody diagram, for Re = 640000 and e/D = 0.00005 , Darcy friction factor , f = 0.0126

Therefore head loss is

HL = \frac{0.0126 (200)(2.015)^2}{( 2 \times 9.81 \times 0.318)}

HL = 1.64 m

7 0
3 years ago
Type the correct answer in the box. Spell all words correctly.
Rudik [331]

Answer:ii dant overstand

Explanation:

5 0
2 years ago
What is the resistance of a circuit with three 1.5 v batteries and running at a current of 5A?
adoni [48]

Answer:

0.3 Ω

Explanation:

Resistance (R) = V/I

R = 1.5/5

R = 15/50

= 3/10

= 0.3Ω

hope it helps :)

3 0
2 years ago
Other questions:
  • g Consider a thin opaque, horizontal plate with an electrical heater on its backside. The front end is exposed to ambient air th
    11·1 answer
  • The screw of shaft straightener exerts a load of 30 as shown in Figure . The screw is square threaded of outside diameter 75 mm
    5·1 answer
  • The engine of a 2000kg car has a power rating of 75kW. How long would it take (seconds) to accelerate from rest to 100 km/hr at
    10·1 answer
  • The velocity of a point mass that moves along the s-axis is given by s' = 40 - 3t^2 m/s, where t is in seconds. Find displacemen
    7·1 answer
  • Cold water at 20 degrees C and 5000 kg/hr is to be heated by hot water supplied at 80 degrees C and 10,000 kg/hr. You select fro
    14·1 answer
  • Is an ideal way for a high school student to see what an engineer does on a typical day but does not provide a hands-on experien
    9·2 answers
  • 7. Sockets internal designs come in what sizes?
    5·1 answer
  • Diffrerentiate y=cos^{4} (3x+1)
    5·1 answer
  • A front wheel drive vehicle with four wheel disc brakes is pulling to the left. Tech A says an external kink or internal restric
    13·1 answer
  • Why might many general contractors begin their careers as construction workers?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!