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

Write a program for a game called \"connect four\" (in C++) ***I\'m not sure how

ID: 3804143 • Letter: W

Question

Write a program for a game called "connect four" (in C++)

***I'm not sure how to even begin writing this.

Any help provided would be greatly appreciated!***

***Please use The following libraries: iostream, cstdlib, cmath, string, ctime, etc.***

Please do not use these: cstdio.h, time.h, stdlib.h, cstring, etc. I have not learned these libraries yet.

This program:

1. Lays out a Connect 4 board (6 high x 7 long) and assigns each space a number.

2. Allows each player to select a space to play, filling that space with either an ‘R’ or ‘B’ for Red or Black.

3. Uses a random number generator to determine which player goes first.

4. One additional unique component.

Program must include:

- Functions

- Array

- Loop

- Branching

- Random number generator

Assumptions:

- The player can only fill in from the bottom of the board and on top of other plays.

Creative Liberties:

- Symbols used for the board.

- Symbols used to indicate each player.

Explanation / Answer

#include<iostream>
#include <string>
using namespace std;

// Represeting the Board array dimensions
char gameBox[6][7];


void showBoard();
bool test(int , int );

int drop(int , char );


int main()
{

int x=0,y=0;
int save_state;
int save_state2 = 0;
int placed_loc = 0;
bool win = false;

while(x<=5)
{
while(y<=6)
{
gameBox[x][y] = ' '; // Empty boxes
y++;
}
x++;
}                      
showBoard();

char player = 15;//start as player 2 will change back 2 player 1
  
   //Game Loop untill soomeone WINS
   while(!win)
   {
       if(save_state2 != -1)
       {
           if(player == 15)
           {
               cout<<"Enter Player1 drop box number";
               player = 254;
           }
           else{
               cout<<"Enter Player2 drop box number";
               player = 15;
           }
       }
       while(true)
       {
           if(placed_loc == 42)
           break;
          
           //User Input loc
           cin>>save_state;
          
           save_state--;
          
           //Out of range state for the BOX
           if(save_state <=6 && save_state>= 0)
           break;
           else
           cout<< " Enter values between 1&7 for the current boxes on board";
          
           if (cin.fail())  
           {                      
               cin.clear();      
               char c;          
               cin>>c;      
           }                  

       }
       if(placed_loc == 42)
       break;
      
       // Drop the square in box location for particular player
       save_state2 = drop(save_state,player);
      
       if(save_state2 == -1)  
       cout<<" This column is FULL , please select another coloumn ";
       else
       {
           // Check after filling the box what is the state of GAME
           win = test(save_state2,save_state);
           placed_loc ++;
           system("cls");
           showBoard();
       }
   }
   system("cls");//this clears the screen
   if(placed_loc == 42){//if draw
       cout<<"No winner, Game was draw ";
       system("pause");
       return 0;
   }
   if(player == 15)
   cout<<"Player2 WINS ";
   else
   cout<<"Player1 WINS ";
  
  
   system("pause");
  
  
   //Exit MAIN
   return 0;
}
void showBoard(){
   cout<<" 1 2 3 4 5 6 7 ";
   for(int x = 0; x<= 5; x++)
   {
       for(int y =0; y <= 6; y++)
       cout<<char(218)<<char(196)<<char(191)<<" ";
      
       cout<<' ';
      
       for(int y =0; y <= 6; y++)
       cout<<char(179)<<gameBox[x][y]<<char(179)<<" ";
      
       cout<<' ';
      
       for(int y =0; y <= 6; y++)
       cout<<char(192)<<char(196)<<char(217)<<" ";
      
       cout<<' ';
   }
}
bool test(int p, int q)
{
  
   char player = gameBox[p][q];
   int x;
   int y;
  
   int ver_y = 1; // "|"
   int hor_x = 1; // "_"
  
   int dia1 = 1; // ""
   int dia2 = 1; // "/"
  

  
   for(x = p+1; x <= 5 && gameBox[x][q] == player ; x++, ver_y++);
   for(x = p-1; x >= 0 && gameBox[x][q] == player ;x--,ver_y++);
  
   if(ver_y >= 4)
   return true;
  

   for(y = q-1;y >= 0 && gameBox[p][y] == player ;y--,hor_x++);
   for(y = q+1;y <= 6 && gameBox[p][y] == player;y++,hor_x++);
  
   if(hor_x >= 4)
   return true;
  

   for(x = p-1, y= q -1;x>=0 && y >=0 && gameBox[x][y] == player ; dia1++, x--, y--);
   for(x = p +1, y = q+1;x<=5 && y <=6 && gameBox[x][y] == player ;dia1++, x++, y++);
  
if(dia1 >= 4)
return true;
  

   for(x = p -1, y= q +1;x>=0 && y <= 6 && gameBox[x][y] == player ; dia2++, x--, y++);
   for(x = p +1, y= q -1;x<=5 && y >=0 && gameBox[x][y] == player ; dia2++, x++, y--);
  
   if(dia2 >= 4)
   return true;
  
  
return false;
}
int drop(int b, char player){
   if(b >=0 && b<= 6)
   {
       if(gameBox[0][b] == ' '){
           int i;
           for(i = 0;gameBox[i][b] == ' ';i++)
               if(i == 5){gameBox[i][b] = player;
           return i;}
           i--;
           gameBox[i][b] =player;
           return i;

       }
       else
           return -1;
   }
   else
       return -1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote