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.
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][b] = ' ';
}
}
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][b]<<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][b];
int i;//vertical
int ii;//horizontal
//check for vertical(|)
for(i = a +1;place[i][b] == player && i <= 5;i++,vertical++);//Check down
for(i = a -1;place[i][b] == 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[i][ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
for(i = a +1, ii = b+1;place[i][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[i][ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
for(i = a +1, ii= b -1;place[i][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][b] == ' ')
{
int i;
for(i = 0;place[i][b] == ' ';i++)
if(i == 5)
{
place[i][b] = player;
return i;
}
i--;
place[i][b] =player;
return i;
Fro thr random number generation, are you using the standard ANSI C functions (srand() and rand()) or the C++11 generators?
...nope!
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.
literally means this:
Hello! Welcome to...
_______ _______ __ __ __ __ _______ _______ _______
| _____|| ___ || | | || | | || _____|| _____||__ __|
| | | | | || || || || || |____ | | | |
| | | | | || || || || || ____| | | | |
| |_____ | |___| || | | || | | || |_____ | |_____ | |
|_______||_______||_| |__||_| |__||_______||_______| |_|
_______ _______ __ __ _______
| _____|| ___ || | | || ___ |
| |____ | | | || | | || |___| |
| ____| | | | || | | || __ _|
| | | |___| || |_| || | | |
|_| |_______||_______||_| |_|
Enter either a '1' or '2' to show how many players there are!
You've chosen single player! Is this correct?
Please answer 'y' for yes, and 'n' for no.
Alright! Starting a single player game!
1 2 3 4 5 6 7
▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ
▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒
▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒
▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ
▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒
▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒
▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ
▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒
▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒
▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ
▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒
▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒
▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ
▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒
▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒
▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ ▒Ŀ
▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒
▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒
Where would you like to drop, Player 1?:
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
please enter a value between 1 and 7 :
(repeats forever and ever and ever)
1 2 3 4 5 6 7 8
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
...the way I read it:
if cin hasn't failed yet, it never bothers to read a number. so it'll just keep asking, forever. Why? Because it didn't fail when the user did the previous step (selecting the number of players). It asks for input about Player one dropping, but it never listens for an answer with another cin statement:
1 2 3 4 5 6
if (turncount == 0)
{
player = 79;
cout << "Where would you like to drop, Player 1?: ";
turncount++;
}