First time poster here, so bear with me. I'm also really new at C++. I am coding this game based on the dice game called Pig. It's close to working right, but I'm having some problems. The point of the game is to reach 100 points but I'm not sure how to code this. I've tried several ways, like while loops or if statements, but none of them have worked. I want it to say who wins as soon as the winning die is rolled. What would be the best way to do this?
I was also having problems with it looping correctly. It's supposed to go back to the player's loop after the computer passes, but it wants to just exit. Any help is appreciated!
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int die;
int myScore = 95;
int devilScore = 95;
int turnScore = 0;
int myScoretotal = myScore += turnScore;
int devilScoretotal = devilScore += turnScore;
char choice;
bool change = false;
cout << "Welcome to Devil's Dice! Please select from the following menu: ";
while(change == false){ //&& devilScoretotal < 100 && myScoretotal < 100){
cout << "\nRoll [r], Pass [p], or Quit [q].";
cin >> choice;
if(choice == 'r'){
die=(rand() % 6 + 1); //Number generator for die
if(die > 1){
cout << "You rolled a " << die << "." << endl;
turnScore += die;
cout << "You will add " << turnScore << " points if you pass now. ";}
else{
cout << "You rolled a 1. You lose your points and your turn ends." << endl;
change = true;
turnScore = 0;
}
}
if(choice == 'p')
{myScore += turnScore;
cout << "Your score is now " << myScore << "." << endl;
turnScore = 0;
change = true; } //needs to go to Devil now
if(choice == 'q')
{cout << "\n\tThanks for playing! ";
return 0; }
else if(choice > 'r' || choice < 'p')
{cout << "Please select from the choices listed."; }
}
while(change == true){// && devilScoretotal < 100 && myScoretotal < 100){ //The Devil's actions
if(myScore > devilScore && turnScore < 17 || devilScore > myScore && turnScore < 12 || devilScore > 84){
choice='r';
cout << "The Devil rolls! " << endl;
if(choice == 'r'){
die=(rand() % 6 + 1); //Number generator for die
if(die > 1){
cout << "The Devil rolls a " << die << "." << endl;
turnScore += die;
}else{
cout << "The Devil rolls a 1. He loses his points and now it's your turn!" << endl;
turnScore = 0;
change = false;
}
}else{
cout << "The Devil chooses to pass. ";
devilScore += turnScore;
choice='p';
turnScore = 0;
cout << "He has " << devilScore << " points. " << endl;
cout << "The Devil now has " << devilScore << " points." << endl;
change = false;
}
}
}
}
The looping problem in your code seems to occur because the entirety of the code doesn't loop, resulting in only one iteration of the game to occur. you should put the code from the player's turn to the end of the devil's turn into a loop with a condition something like this
while(hasSomeoneWonYet==false){ code }
this will keep the turns alternating until has someoneWonYet is true
As to how to check win conditions, the way I would do it is to put a statement checking if win conditions have been met at the end of each player's turn. something like the following:
if (winConditionsMet){ hasSomeoneWonYet=true;}
replace win conditions met with the actual conditions and it should work better.
Here is some pseudo code to help you out:
turn = player
winner = false
rolled_1 = false
pass = false
while !winner
if turn == player
show the player options (roll, pass, or quit)
respond to player's choice
else if turn == computer
logic for what computer does for its roll
respond to computer's choice
check for winner
if winner
announce winner
else
if rolled 1 or pass was chosen
if turn == computer
turn = player
else
turn = computer
(winner became true, so the while loop is exited, and the program finishes)
Use the indentation to know where to put the curly brackets. A lot of IDE's have a "format" or "indentation" helper of some sort.
Also because turn is only two values, you could change it to a boolean, such as is_computer_turn
.
Hope that helps.
Problem 1: You have two loops controlling each player's turn, but they are not enclosed in an outer loop that will repeat the turns until a condition is met (the winning condition).
Problem 2: You should check the winning condition when the rolled number + turn score + total score is >= 100. In this case you can simply print the winning statement and return.
A possible solution is (I'm using infinite loops and breaks, not very elegant but it should do) :
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int die;
int myScore = 0;
int devilScore = 0;
int turnScore = 0;
char choice;
bool change = false;
bool weHaveAWinner = false;
cout << "Welcome to Devil's Dice! Please select from the following menu: ";
//Enclosing infinite loop, only can exit on return
while (true) {
//Player turn loop, can exit with breaks to main loop
while (true){
cout << "\nRoll [r], Pass [p], or Quit [q].";
cin >> choice;
if(choice == 'r'){
die=(rand() % 6 + 1); //Number generator for die
if(die > 1){
cout << "You rolled a " << die << "." << endl;
turnScore += die;
if (turnScore + myScore >=100) {
cout << "You win!" << endl;
//Winning condition met. Game over and return.
return 0;
}
cout << "You will add " << turnScore << " points if you pass now. ";}
else{
cout << "You rolled a 1. You lose your points and your turn ends." << endl;
turnScore = 0;
//End turn. Breaks secondary loop.
break;
}
}
if(choice == 'p') {
myScore += turnScore;
cout << "Your score is now " << myScore << "." << endl;
turnScore = 0;
change = true;
//End turn. Breaks secondary loop.
break;
} //needs to go to Devil now
if(choice == 'q')
{cout << "\n\tThanks for playing! ";
return 0; }
else if(choice > 'r' || choice < 'p')
{cout << "Please select from the choices listed."; }
}
while (true){
//Devil's turn loop, can exit with breaks to main loop
if(myScore > devilScore && turnScore < 17 || devilScore > myScore && turnScore < 12 || devilScore > 84){
choice='r';
cout << "The Devil rolls! " << endl;
if(choice == 'r'){
die=(rand() % 6 + 1); //Number generator for die
if(die > 1){
cout << "The Devil rolls a " << die << "." << endl;
turnScore += die;
if (turnScore + devilScore >=100) {
//Winning condition met. Game over and return.
cout << "The Devil wins!" << endl;
return 0;
}
}else{
cout << "The Devil rolls a 1. He loses his points and now it's your turn!" << endl;
turnScore = 0;
change = false;
//End turn. Breaks secondary loop.
break;
}
}
}else{
cout << "The Devil chooses to pass. ";
devilScore += turnScore;
choice='p';
turnScore = 0;
cout << "He has " << devilScore << " points. " << endl;
cout << "The Devil now has " << devilScore << " points." << endl;
change = false;
//End turn. Breaks secondary loop.
break;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.