Jump to content
  • Advertisement

Recommended Posts

I'm coding a game of Connect Four for my C++ class. For the most part, it works. But I'm having trouble with the single player version of the game. It seems to not properly generate the random number between 1 through 7, which causes it to spam the same phrase when running the code.

Here's my code:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include <time.h>

using namespace std;
void display();
bool check(int a, int b);
int drop(int b, char player);
char place [6][7];

int main()
{

int choice;
char choice2;

cout << " Hello! Welcome to...\n";
cout << " _______ _______ __ __ __ __ _______ _______ _______\n";
cout << "| _____|| ___ || | | || | | || _____|| _____||__ __|\n";
cout << "| | | | | || || || || || |____ | | | |\n";
cout << "| | | | | || || || || || ____| | | | |\n";
cout << "| |_____ | |___| || | | || | | || |_____ | |_____ | |\n";
cout << "|_______||_______||_| |__||_| |__||_______||_______| |_|\n";
cout << " _______ _______ __ __ _______\n";
cout << " | _____|| ___ || | | || ___ |\n";
cout << " | |____ | | | || | | || |___| |\n";
cout << " | ____| | | | || | | || __ _|\n";
cout << " | | | |___| || |_| || | | |\n";
cout << " |_| |_______||_______||_| |_|\n";
cout << "Enter either a '1' or '2' to show how many players there are!\n" << flush;

cin >> choice;

switch (choice)
{
case 1:
cout << "You've chosen single player! Is this correct?\nPlease answer 'y' for yes, and 'n' for no.\n";
cin >> choice2;
if (choice2 == 'y')
{
system ("CLS");
cout << "Alright! Starting a single player game!\n" << flush;
}
else if (choice2 == 'n')
{
system ("CLS");
cout << "You need to restart the program and select the proper amount of players!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << flush;
return 0;
}
else
{
cout << "That's not a valid answer! Please try again!\n";
return 0;
}
break;
case 2:
cout << "You've chosen two players! Is this correct?\nPlease answer 'y' for yes, and 'n' for no.\n";
cin >> choice2;
if (choice2 == 'y')
{
system ("CLS");
cout << "Alright! Starting a two player game!\n" << flush;


}
else if (choice2 == 'n')
{
system ("CLS");
cout << "You need to restart the program and select the proper amount of players!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << flush;
return 0;
}
else
{
cout << "That's not a valid answer! Please try again!\n";
return 0;
}
break;
}
{
for(int a =0;a <= 5; a++)
{ //fill place with whitespace
for(int b = 0; b<=6; b++)
{
place[a] = ' ';
}
}
display();//Displays for first time so players can see the board
int hold;//Will house user row choice
int hold2 = 0;//will hold drop value
int charsPlaced = 0;//Number of pieces dropped so can end game if a draw
bool gamewon = false;//Will be changed to true when game is won and will exit while loop
char player;//start as player 2 will change back 2 player 1
int turncount = 0;
while(!gamewon){//will stop when game is won, ! means NOT makes the opposite be checked
if(hold2 != -1)
{//check if there was a error in the last drop
if (turncount == 0)
{
player = 79;
cout << "Where would you like to drop, Player 1?: ";
turncount++;
}
else
{
if (choice == 2)
{
player = 88;
cout << "Where would you like to drop, Player 2?: ";
turncount--;
}
else
{
player = 88;
hold = 3;
turncount--;
}

}
}
while(true)
{//will run until 'break;'
if(charsPlaced == 42) break;//if draw

if (choice == 2)
{
cin >> hold;
}

hold--;//take off 1 to account for arrays starting at 0 not 1
if(hold <=6 && hold>= 0) break;//if within valid range stop loop
else cout<< "\nplease enter a value between 1 and 7 : ";//ask for input and loop again
if (cin.fail()) //catch a non number
{ //
cin.clear(); //Stops cin trying to put its value in to hold
char c; //Try entering a non number without this, 2 see what this does
cin>>c; //
} //Catch a non number

}
if(charsPlaced == 42) break;//if draw
hold2 = drop(hold,player);//drop the player store the row in hold2
if(hold2 == -1) cout<<"Column is full\nPlease enter another number between 1 and 7:";//if error -1 row is full
else{
gamewon = check(hold2,hold);//check if game is run
charsPlaced ++;//another character has been successfully placed
//This clears the screen works with windows, not necessary to run game
display();//displayed updated board
}
}


//this clears the screen
if(charsPlaced == 42){//if draw
cout<<"No winner, Game is a draw.\n";

return 0;
}
if(player == 15)//if won by player 2
cout<<"Player 2 has won the game!\n";
else cout<<"Player 1 has won the game!\n";//Else won by player 1
//pauses before exit so players can see who won, works with windows
return 0;//Exit application
}
}

void display()
{
system("CLS");
cout<<" 1 2 3 4 5 6 7\n";
for(int a = 0; a<= 5; a++)
{
for(int b =0; b <= 6; b++) cout<<char(218)<<char(196)<<char(191)<<" ";
cout<<'\n';
for(int b =0; b <= 6; b++) cout<<char(179)<<place[a]<<char(179)<<" ";
cout<<'\n';
for(int b =0; b <= 6; b++) cout<<char(192)<<char(196)<<char(217)<<" ";
cout<<'\n';
}
}

bool check(int a, int b)
{
int vertical = 1;//(|)
int horizontal = 1;//(-)
int diagonal1 = 1;//(\)
int diagonal2 = 1;//(/)
char player = place[a];
int i;//vertical
int ii;//horizontal
//check for vertical(|)
for(i = a +1;place == player && i <= 5;i++,vertical++);//Check down
for(i = a -1;place == player && i >= 0;i--,vertical++);//Check up
if(vertical >= 4)return true;
//check for horizontal(-)
for(ii = b -1;place[a][ii] == player && ii >= 0;ii--,horizontal++);//Check left
for(ii = b +1;place[a][ii] == player && ii <= 6;ii++,horizontal++);//Check right
if(horizontal >= 4) return true;
//check for diagonal 1 (\)
for(i = a -1, ii= b -1;place[ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
for(i = a +1, ii = b+1;place[ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right
if(diagonal1 >= 4) return true;
//check for diagonal 2(/)
for(i = a -1, ii= b +1;place[ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
for(i = a +1, ii= b -1;place[ii] == player && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left
if(diagonal2 >= 4) return true;
return false;
}
int drop(int b, char player)
{
if(b >=0 && b<= 6)
{
if(place[0] == ' ')
{
int i;
for(i = 0;place == ' ';i++)
if(i == 5)
{
place = player;
return i;
}
i--;
place =player;
return i;

}
}
}
 

Share this post


Link to post
Share on other sites
Advertisement

That's because it's not there. It's supposed to be in the "hold" variable, but I have 3 assigned to it right now as a place holder.

14 hours ago, 3pic_F4il_FTW said:

Hi

Only had a quick glance at it but I can't see you assigning a random value to hold anywhere.

That's because it's not there. It's supposed to be in the "hold" variable, but I have 3 assigned to it right now as a place holder.

Share this post


Link to post
Share on other sites
On 5/11/2018 at 3:22 AM, Trace Collins said:

for my C++ class

Talk to your teacher. We generally don't help with homework or similar here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!