I have been coding a blackjack game that is almost done it's first stage of development. I am almost done, the only problem is that the random numbers that are generated in this part:
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
is not the same as the numbers generated in the final part:
cout << "You had a total of: " << x.userTotal << endl;
as i keep getting different numbers. I will paste my entire code below.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
void setRand() {
srand(time(0));
}
class Game {
public:
int userCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int dealerCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int userShowCardOne = userCards[1 + (rand()%11)];
int userShowCardTwo = userCards[1 + (rand()%11)];
int dealerShowCard = dealerCards[1 + (rand()%11)];
int dealerHiddenCard = dealerCards[1 + (rand()%11)];
int userShowCards[5] = {userShowCardOne, userShowCardTwo, userShowCardThree, userShowCardFour, userShowCardFive};
int userShowCardThree = 0;
int userShowCardFour = 0;
int userShowCardFive = 0;
int fresh = 1;
int beginningInput;
int hit = 1;
int dealerTotal = dealerShowCard + dealerHiddenCard;
int userTotal = userShowCardOne + userShowCardTwo;
int cardCount = 2;
int runGame = 1;
private:
};
// int a = 1 + rand()%11;
/*int b = 1 + rand()%11;
int c = 1 + rand()%11;
int d = 1 + rand()%11;
int e = 1 + rand()%11;
int f = 1 + rand()%11;
int g = 1 + rand()%11;
int h = 1 + rand()%11;
*/
int startGame();
int main() {
srand(time(0));
Game x;
cout << "Welcome to BlackJack 1.0.0" << endl;
cout << "Press: " <<endl;
cout << "1 ----- New Game" << endl;
cout << "2 ----- Help" << endl;
cin >> x.beginningInput;
if(x.beginningInput == 1){
startGame();
cout << "The dealer had: " << endl;
cout << x.dealerHiddenCard << endl;
cout << x.dealerShowCard << endl;
while(x.dealerTotal <= 16) {
cout << "The dealer has decided to hit" << endl;
int dealerHit = 1 + (rand()%11);
cout << "The dealer has gotten " << dealerHit << endl;
x.dealerTotal += dealerHit;
}
cout << "The dealer has decided to stay" << endl;
cout << "The dealer had a total of " << x.dealerTotal << endl;
cout << "You had a total of: " << x.userTotal << endl;
if (x.dealerTotal > 21 && x.userTotal <= 21) {
cout << "The dealer busted. You won!" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal <= 21) {
cout << "You busted. The Dealer Won" << endl;
}
else if (x.userTotal > 21 && x.dealerTotal > 21) {
cout << "You and the dealer both busted. Tie" << endl;
}
else {
if (x.dealerTotal > x.userTotal) {
cout << "Sorry, you lost to the dealer" << endl;
}
else if (x.dealerTotal < x.userTotal) {
cout << "Congrats, you won!" << endl;
}
else {
cout << "You and the dealer tied. " << endl;
}
}
cout << "Would you like to play again? 1 to play again, 0 to quit" << endl;
cin >> x.beginningInput;
if (x.beginningInput == 1){
startGame();
}
else if (x.beginningInput == 0){
return 0;
}
}
else if (x.beginningInput == 0) {
cout << "Thanks for playing!" << endl;
}
else {
cout << "Here's the help section" << endl;
}
}
int startGame() {
Game x;
srand(time(0));
if (x.fresh != 1){
cout << "NEW GAME\n \n " << endl;
}
while (x.runGame == 1) {
cout << "Dealer: \n" << endl;
cout << "X" << endl;
cout << x.dealerShowCard << endl;
cout << "You: \n" << endl;
cout << x.userShowCardOne << endl;
cout << x.userShowCardTwo << endl;
x.userTotal = x.userShowCardOne + x.userShowCardTwo;
if (x.userShowCardThree != 0) {
cout << x.userShowCardThree << endl;
}
if (x.userShowCardFour != 0) {
cout << x.userShowCardFour << endl;
cout << "You can only hit one more time!" << endl;
}
if (x.userShowCardFive != 0) {
cout << x.userShowCardFive << endl;
}
if(x.cardCount > 5) {
cout << "sorry, there is a 5 card limit.";
}
cout << "Would you like to hit or stay? (1 for hit or 2 for stay)" << endl;
cin >> x.hit;
x.fresh = 2;
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
}
else {
if (x.userShowCardFour == 0) {
x.userShowCardFour = 1 + (rand()%11);
int dealerHit2 = x.userShowCardFour;
x.userTotal += dealerHit2;
x.cardCount++;
}
else {
if (x.userShowCardFive ==0) {
x.userShowCardFive = 1 + (rand()%11);
int dealerHit3 = x.userShowCardFive;
x.userTotal += dealerHit3;
x.cardCount++;
}
}
}
}
if (x.hit == 2) {
x.runGame = 2;
}
}
return 0;
}
Looks like you have 2 instances of the Game object and both are named x
int main() {
srand(time(0));
Game x;
...
cout << "You had a total of: " << x.userTotal << endl;
then in the startGame function
int startGame() {
Game x;
...
if(x.hit == 1) {
if (x.userShowCardThree == 0) {
x.userShowCardThree = 1 + (rand()%11);
int dealerHit1 = x.userShowCardThree;
x.userTotal += dealerHit1;
x.cardCount++;
The x in main() is not the same as x in startGame() as both are separate Game objects.
Related
Maybe I am missing something, but for some reason an overloaded operator= can successfully work with the Fuel variable but not with Capacity, even though they have the exact same attributes.
class CGasStation
{
private:
int Capacity[4];
int Fuel[4];
int PistolSize;
public:
CGasStation(int StartingFuel = 100, int StartingCapacity = 200, int StartingPistolSize = 10);
virtual ~CGasStation();
void SetCapacity(int NewCapacity, int StorageNum);
void SetPistolSize(int);
void AddFuel(int, int StorageNum);
float FuelUpCar(float, int StorageNum);
void FixFuel();
void FixCapacity();
void GetInfo(int StorageNum);
/*__________________________________________________________________________________________________________________________________________________________________*/
CGasStation& operator= (const CGasStation & NewSize);
/*__________________________________________________________________________________________________________________________________________________________________*/
CGasStation& operator= (const CGasStation & NewSize);
#include "GasStation.h"
#include <iostream>
using namespace std;
CGasStation::CGasStation(int StartingFuel, int StartingCapacity, int StartingPistolSize)
{
for (int i = 0; i < 4; i++)
{
Capacity[i] = StartingCapacity;
Fuel[i] = StartingFuel;
}
PistolSize = StartingPistolSize;
}
void CGasStation::FixFuel()
{
for (int i = 0; i < 4; i++)
{
if (Fuel[i] > Capacity[i])
{
Fuel[i] = Capacity[i];
}
}
}
void CGasStation::FixCapacity()
{
for (int i = 0; i < 4; i++)
{
if (Capacity[i] < 50)
{
Capacity[i] = 50;
}
}
}
CGasStation::~CGasStation()
{
}
void CGasStation::GetInfo(int StorageNum)
{
cout << "Capacity: " << Capacity[StorageNum] << endl << "Fuel: " << Fuel[StorageNum] << endl << "Pistol Size: " << PistolSize << endl << "________________________________________________________________________________________________________________________" << endl;
}
void CGasStation::SetCapacity(int NewCapacity, int StorageNum)
{
Capacity[StorageNum] = NewCapacity;
if (Capacity[StorageNum] < 50)
{
Capacity[StorageNum] = 50;
}
}
void CGasStation::SetPistolSize(int NewPistolSize)
{
PistolSize = NewPistolSize;
if (PistolSize < 10)
{
PistolSize = 10;
}
if (PistolSize > 300)
{
PistolSize = 300;
}
}
void CGasStation::AddFuel(int AdditionalFuel, int StorageNum)
{
if (AdditionalFuel < 0)
{
AdditionalFuel = 0;
}
Fuel[StorageNum] = Fuel[StorageNum] + AdditionalFuel;
}
float CGasStation::FuelUpCar(float SoldFuel, int StorageNum)
{
if (SoldFuel < 0)
{
SoldFuel = 0;
}
if (SoldFuel > Fuel[StorageNum])
{
SoldFuel = Fuel[StorageNum];
}
Fuel[StorageNum] -= SoldFuel;
return (SoldFuel / PistolSize);
}
/*__________________________________________________________________________________________________________________________________________________________________*/
CGasStation& CGasStation::operator= (const CGasStation& NewSize)
{
for (int i = 0; i < 4; i++)
{
Capacity[i] = NewSize.Capacity[i];
Fuel[i] = NewSize.Fuel[i];
}
return *this;
}
#include <iostream>
#include "GasStation.h"
using namespace std;
int main()
{
CGasStation GasStation;
int choise, StorageNum, a, b;
while (true)
{
cout << "Menu:" << endl << "1-Get information" << endl << "2-Change capacity" << endl << "3-Change pistol size" << endl << "4-Add fuel to the storage" << endl << "5-Fuel up a car" << endl << "6-Exit" << endl << "7-Set new capacity and fuel amount for all storages" << endl << "8-increase cacity and fuel amount for all storages" << endl << endl;
cin >> choise;
cout << "________________________________________________________________________________________________________________________" << endl;
if (choise != 1 && choise != 2 && choise != 3 && choise != 4 && choise != 5 && choise != 6 && choise != 7 && choise != 8)
{
cout << "Pomilka";
break;
}
if (choise == 1)
{
cout << "Which storage would you like to check?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
GasStation.GetInfo(StorageNum - 1);
}
}
if (choise == 2)
{
cout << "Which storage would you like to change in size?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
cout << "Insert new size value (cannot be less than 50)" << endl;
cin >> a;
cout << endl << "________________________________________________________________________________________________________________________" << endl;
GasStation.SetCapacity(a, StorageNum - 1);
GasStation.FixFuel();
}
}
if (choise == 3)
{
cout << "Insert new pistol size value (10<your size<300)" << endl;
cin >> a;
cout << endl << "________________________________________________________________________________________________________________________" << endl;
GasStation.SetPistolSize(a);
}
if (choise == 4)
{
cout << "Which storage would you like to fill up?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
cout << "How much fuel would you like to add?" << endl;
cin >> a;
cout << endl << "________________________________________________________________________________________________________________________" << endl;
GasStation.AddFuel(a, StorageNum - 1);
GasStation.FixFuel();
}
}
if (choise == 5)
{
cout << "Which storage would you like to use for filling?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
cout << "How much would you like to use up?" << endl;
cin >> a;
cout << endl;
cout << "Time to fill up a car: " << GasStation.FuelUpCar(a, StorageNum - 1) << " minutes" << endl << "________________________________________________________________________________________________________________________" << endl;
}
}
if (choise == 6)
{
break;
if (choise == 7)
{
cout << "What capacity and fuel amount would you like to set?" << endl << endl;
cin >> b;
GasStation = b;
GasStation.FixCapacity();
GasStation.FixFuel();
cout << "________________________________________________________________________________________________________________________" << endl;
}
}
return 0;
}
Changed it to show the entire program, as this is a university project that is requiring me to use the previous project, so everything made before must be kept as it is, and the only part I can change is overloading. And yes, I must overload it, I can't just say that it's fine without it.
Here's a screanshot of the result:
Starting amount for Capacity is 200, and starting amount for Fuel is 100. In the screanshot, Fuel changed just as the program intended, its just that FixFuel got into the work of making sure it doesn't go past the limit, but Capacity didn't change at all, and I don't know why.
I have no idea what's wrong, and this is my first question on this website, so I hope some of you can help me out.
Here's a really short version of the same problem:
class Testing
{
private:
int a[4];
int b[4];
public:
Testing(int startingA = 100, int startingB = 200);
void GetInfo(int Number);
/*__________________________________________________*/
Testing& operator= (const Testing& NewNum);
};
#include "Testing.h"
#include <iostream>
using namespace std;
Testing::Testing(int startingA, int startingB)
{
for (int i = 0; i < 4; i++)
{
a[i] = startingA;
b[i] = startingB;
}
}
void Testing::GetInfo(int Number)
{
cout << "a: " << a[Number] << endl << "b: " << b[Number] << endl << endl;
}
Testing& Testing::operator= (const Testing& NewNum)
{
for (int i = 0; i < 4; i++)
{
a[i] = NewNum.a[i];
b[i] = NewNum.b[i];
}
return *this;
}
#include <iostream>
#include "Testing.h"
using namespace std;
int main()
{
Testing Test;
int choise, Number, NewNum;
while (true)
{
cout << "Menu:" << endl << "1-Get info" << endl << "2-Change info" << endl << "3-exit" << endl << endl;
cin >> choise;
if (choise != 1 && choise != 2 && choise != 3)
{
cout << "error";
break;
}
if (choise == 1)
{
cout << endl << "Which number would you like to check?(1-4)" << endl << endl;
cin >> Number;
cout << endl;
if (Number != 1 && Number != 2 && Number != 3 && Number != 4)
{
cout << "error";
break;
}
else
{
Test.GetInfo(Number - 1);
}
}
if (choise == 2)
{
cout << endl << "Imput new number" << endl << endl;
cin >> NewNum;
Test = NewNum;
cout << endl;
}
if (choise == 3)
{
break;
}
}
return 0;
}
The question is, why does value a change, but value b doesn't? How do I fix that?
I have been receiving an issue that I do not understand. The issue is:
Run-Time Check Failure #3 - The variable 'win' is being used without being initialized.
I am struggling to comprehend where my error is within the code. From what I understand the run time error comes from win not being initialized meaning it hasn't been used or set, however, it clearly is set in the fightScene() function.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Character.h"
#include "Item.h"
#include "Blacksmith.h"
using namespace std;
void startup();
void createCharacter();
void existCharacter();
void secondFighter();
void fightScene();
int fightMoves(int att, int sp, int num);
void roundWon();
void characterLost();
void use_bar();
void newlines();
void changeCase(string &convert);
void bSmith();
Character existingCharacter[4] = { Character("Shabu", 10, 10, 10, 0, "Empty"), Character("Calil", 15, 15, 15, 0, "Empty"), Character("Goltero", 20, 20, 20, 0, "Empty"), Character("Balrogg", 30, 30, 30, 0, "Empty") };
Character cCharacter[2] = { Character(), Character() };
Character cCharacterSave("Empty", 20, 0, 0, 100, "Empty");
Item bars(0, 0, 0);
int timesWon = 0;
int purposeIgnore = 0;
char repeat = 'y';
int main() {
startup();
while (repeat == 'y' || repeat == 'Y') {
fightScene();
if (repeat == 'y' || repeat == 'Y')
secondFighter();
}
return 0;
}
void startup() {
int playerChoice;
cout << "**************************" << endl;
cout << "* Loading Game *" << endl;
cout << "**************************" << endl;
cout << "Which Would You Prefer?" << endl;
cout << "1: New Character..." << endl;
cout << "2: Load Character..." << endl;
do {
cout << "Enter Selection: ";
cin >> playerChoice;
} while (playerChoice <= 0 || playerChoice >= 3);
if (playerChoice == 1)
createCharacter();
else if (playerChoice == 2)
existCharacter();
return;
}
void createCharacter() {
string Name;
string inventory;
int h, att, sp, gc;
cout << "***************************" << endl;
cout << "* Creating New Player *" << endl;
cout << "***************************" << endl;
cout << "What Shall We Call You?" << endl;
cout << "Enter Name Here: ";
cin >> Name;
changeCase(Name);
cout << Name << ", let's set your stats!" << endl;
cout << "Set your ATTACK between 1 - 10" << endl;
do {
cout << "Enter Attack Here: ";
cin >> att;
} while (att <= 0 || att >= 11);
cout << "Set your SPEED between 1 - 10" << endl;
do {
cout << "Enter Speed Here: ";
cin >> sp;
} while (sp <= 0 || sp >= 11);
h = 20;
gc = 100;
cout << "The HEALTH stat wil increase over time, but as of now, " << Name << ", has " << h << " health!" << endl;
cout << Name << " you have been granted " << gc << " gold!" << endl;
cCharacter[0] = Character(Name, h, att, sp, gc, inventory);
cin.ignore();
return;
}
void existCharacter() {
string characterChoice;
cout << "***********************" << endl;
cout << "* Existing Player *" << endl;
cout << "***********************" << endl;
for (int i = 0, j = 1; i >= 0 && i <= 3; i++, j++) {
cout << j << ": " << existingCharacter[i].getName();
cout << "\t" << "Health: " << existingCharacter[i].getHealth();
cout << "\t" << "Attack: " << existingCharacter[i].getAttack();
cout << "\t" << "Speed: " << existingCharacter[i].getSpeed();
}
cout << "Enter Selection: ";
cin.ignore();
getline(cin, characterChoice);
changeCase(characterChoice);
if (characterChoice == "1" || characterChoice == "Shabu") {
cCharacter[0] = existingCharacter[0]; cCharacterSave.setHealth(existingCharacter[0].getHealth());
}
else if (characterChoice == "2" || characterChoice == "Calil") {
cCharacter[0] = existingCharacter[1]; cCharacterSave.setHealth(existingCharacter[1].getHealth());
}
else if (characterChoice == "3" || characterChoice == "Goltero") {
cCharacter[0] = existingCharacter[2]; cCharacterSave.setHealth(existingCharacter[2].getHealth());
}
else if (characterChoice == "4" || characterChoice == "Balrogg") {
cCharacter[0] = existingCharacter[3]; cCharacterSave.setHealth(existingCharacter[3].getHealth());
}
else {
cout << "Program Failure....Invaid Input!" << endl;
}
secondFighter();
return;
}
void secondFighter() {
string cFighter;
cout << "***************" << endl;
cout << "* Fighter *" << endl;
cout << "***************" << endl;
cout << "Choose Who To Fight!" << endl;
for (int i = 0, j = 1; i >= 0 && i <= 3; i++, j++) {
cout << j << ": " << existingCharacter[i].getName();
cout << "\t" << "Health: " << existingCharacter[i].getHealth();
cout << "\t" << "Strength: " << existingCharacter[i].getAttack();
cout << "\t" << "Speed: " << existingCharacter[i].getSpeed();
cout << endl;
}
cout << "Enter Selection: ";
if (purposeIgnore >= 1)
cin.ignore();
getline(cin, cFighter);
changeCase(cFighter);
if (cFighter == "1" || cFighter == "Shabu")
cCharacter[1] = existingCharacter[0];
else if (cFighter == "2" || cFighter == "Calil")
cCharacter[1] = existingCharacter[1];
else if (cFighter == "3" || cFighter == "Goltero")
cCharacter[1] = existingCharacter[2];
else if (cFighter == "4" || cFighter == "Balrogg")
cCharacter[1] = existingCharacter[3];
else { cout << "Invalid Input! Program Failure....Please Close and Restart" << endl; }
return;
}
void fightScene() {
int fight_choice;
int i = 0;
int j = 5;
bool win;
if (timesWon >= 1)
j = 6;
while (cCharacter[0].getHealth() > 0 && cCharacter[1].getHealth() > 0) {
newlines();
cout << "\t\t" << cCharacter[0].getName() << " choose ";
if (i == 0)
cout << "an attack.";
else
cout << "another attack.";
do {
cout << "\n\n\t\t<1. Punch / 2. Kick>\n\t\t<3. Slam / 4. Drop kick>";
if (j == 6)
cout << "\n\t\t<5. Items>";
cout << endl << "\t\t";
cin >> fight_choice;
} while (fight_choice <= 0 || fight_choice >= j);
if (fight_choice == 1) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 1));
cout << "\n\t\tYou Punched " << cCharacter[1].getName() << ", his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (fight_choice == 2) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 2));
cout << "\n\t\tYou Kicked " << cCharacter[1].getName() << ", his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (fight_choice == 3) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 3));
cout << "\n\t\tYou Slamed " << cCharacter[1].getName() << " on the ground, his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (fight_choice == 4) {
cCharacter[1].setHealth(cCharacter[1].getHealth() - fightMoves(cCharacter[0].getAttack(), cCharacter[0].getSpeed(), 4));
cout << "\n\t\tYou Drop Kicked " << cCharacter[1].getName() << ", his health is now: ";
if (cCharacter[1].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[1].setHealth(-1);
}
else
cout << cCharacter[1].getHealth() << endl << endl;
}
else if (timesWon >= 1 && fight_choice == 5) { // Bars
use_bar();
}
// Second fighter attack
srand(time(NULL));
int ran_pick = rand() % 5 + 1;
if (cCharacter[1].getHealth() < 0) {
ran_pick = 0;
win = 1;
}
else if (cCharacter[1].getHealth() > 0) {
cout << "\n\t\t<" << cCharacter[1].getName() << "'s turn>" << endl;
win = 0;
}
if (ran_pick == 0)
continue;
else if (ran_pick == 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 1));
cout << "\n\t\t" << cCharacter[1].getName() << " punched you! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 2) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 2));
cout << "\n\t\t" << cCharacter[1].getName() << " kicked you! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 3) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 3));
cout << "\n\t\t" << cCharacter[1].getName() << " slammed you on the ground! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 4) {
cCharacter[0].setHealth(cCharacter[0].getHealth() - fightMoves(cCharacter[1].getAttack(), cCharacter[1].getSpeed(), 4));
cout << "\n\t\t" << cCharacter[1].getName() << " drop kicked you! Your health is now: ";
if (cCharacter[0].getHealth() <= 0)
{
cout << "0" << endl << endl; cCharacter[0].setHealth(-1);
}
else
cout << cCharacter[0].getHealth() << endl << endl;
}
else if (ran_pick == 5)
cout << "\t\t" << cCharacter[1].getName() << " missed you! Your health is still: " << cCharacter[0].getHealth() << endl << endl;
if (cCharacter[0].getHealth() > 0) {
cout << "\n\t\t";
system("PAUSE"); // Lets them read the attacks
}
i++; // Increases i to change some wording
}
if (win == 1)
roundWon();
else
characterLost();
return;
}
int fightMoves(int st, int sp, int num) {
srand(time(NULL));
int rand_num;
int addition;
switch (num) {
case 1: {
rand_num = rand() % 40 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
case 2: {
rand_num = rand() % 20 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
case 3: {
rand_num = rand() % 30 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
case 4: {
rand_num = rand() % 30 + 1;
addition = (st + sp) / rand_num;
cout << "\n\t\tDamage inflicted: " << addition;
return addition; break;
}
}
}
int start()
{
while (repeat == 'y' || repeat == 'Y') {
fightScene();
if (repeat == 'y' || repeat == 'Y')
secondFighter();
}
return 0;
}
void roundWon() {
srand(time(NULL));
timesWon++;
purposeIgnore++;
cCharacter[0].setHealth(cCharacterSave.getHealth());
cCharacter[1].setHealth(100);
int item_won = rand() % 3 + 1;
cout << "\t\tYou won!";
cout << "\n\n\t\tYou have been rewarded ";
if (item_won == 1) {
cout << "a small protein bar! (+20 health)";
bars.bar_small++;
}
else if (item_won == 2) {
cout << "a medium protein bar! (+30 health)";
bars.bar_medium++;
}
else if (item_won == 3) {
cout << "a large protein bar! (+40 health)";
bars.bar_large++;
}
cout << "\n\n\t\tItems can be used in game at the cost of a turn.";
cout << "\n\n\t\t";
system("PAUSE"); // Lets them read
int ability_gain[3] = { rand() % 10 + 1, rand() % 10 + 1, rand() % 10 + 1 };
cCharacter[0].setHealth(ability_gain[0] += cCharacter[0].getHealth());
cCharacterSave.setHealth(cCharacter[0].getHealth());
cCharacter[0].setAttack(ability_gain[1] += cCharacter[0].getAttack());
cCharacter[0].setSpeed(ability_gain[2] += cCharacter[0].getSpeed());
cout << "\n\t\tYour health is now: " << cCharacter[0].getHealth();
cout << "\n\t\tYour strength is now: " << cCharacter[0].getAttack();
cout << "\n\t\tYour speed is now: " << cCharacter[0].getSpeed();
cout << "\n\n\t\tWould you like to fight another person? (y/n) ";
cin >> repeat;
return;
}
void characterLost() {
cCharacter[0].setHealth(cCharacterSave.getHealth());
cCharacter[1].setHealth(100);
purposeIgnore++;
cout << "\n\n\t\tYou have lost.";
cout << "\n\t\tWould you like to fight another person? (y/n) ";
cin >> repeat;
return;
}
void use_bar() {
int choose_bar;
char another_bar;
do {
cout << "\n\n\t\tWhich bar would you like to use? ";
cout << "\n\t\t 1: Small Bar: " << bars.bar_small;
cout << "\n\t\t 2: Medium Bar: " << bars.bar_medium;
cout << "\n\t\t 3: Large bar: " << bars.bar_large;
do {
cout << "\n\n\t\tI choose: ";
cin >> choose_bar;
} while (choose_bar <= 0 || choose_bar >= 4);
if (choose_bar == 1 && bars.bar_small >= 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() + 20);
bars.bar_small--;
cout << "\n\n\t\tYour health is now: " << cCharacter[0].getHealth();
}
else if (choose_bar == 2 && bars.bar_medium >= 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() + 30);
bars.bar_medium--;
cout << "\n\n\t\tYour health is now: " << cCharacter[0].getHealth();
}
else if (choose_bar == 3 && bars.bar_large >= 1) {
cCharacter[0].setHealth(cCharacter[0].getHealth() + 40);
bars.bar_large--;
cout << "\n\n\t\tYour health is now: " << cCharacter[0].getHealth();
}
else
cout << "\n\t\tNot enough bars!";
cout << "\n\n\t\tWould you like to use another bar? (y/n) ";
cin >> another_bar;
} while (another_bar == 'y' || another_bar == 'Y');
return;
}
void newlines() {
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
void changeCase(string &convert) {
for (int i = 0; convert[i]; i++) {
if (isupper(convert[i]))
convert[i] = tolower(convert[i]);
else
continue;
}
convert.at(0) = toupper(convert.at(0));
return;
}
void bSmith()
{
Blacksmith shopKeeper; //The shop keeper
int responce; //Menu navigation
cout << "*******************************" << endl;
cout << "* Welcome to the Blacksmith *" << endl;
cout << "*******************************" << "\n" << endl;
cout << "*****************************" << endl;
cout << "* 1: Purchase Items *\n";
cout << "* 2: Sell Items *\n";
cout << "* 3: List Your Items *\n";
cout << "* 4: Currency *\n";
cout << "* 5: Exit *\n";
cout << "*****************************" << endl;
do
{
cout << "Enter Option Here: ";
cin >> responce;
switch (responce)
{
case 1:
shopKeeper.buyItem(cCharacter[0]);
cout << endl;
break;
case 2:
cout << "*****************************" << endl;
cout << "* Items To Sell *" << endl;
cout << "*****************************" << endl;
shopKeeper.sellItem(cCharacter[0]);
cout << endl;
break;
case 3:
cout << "*****************************" << endl;
cout << "* Character Inventory *" << endl;
cout << "*****************************" << endl;
cCharacter[0].listInv();
cout << endl;
break;
case 4:
cout << "*****************************" << endl;
cout << "* Current Gold Currency *" << endl;
cout << "*****************************" << endl;
cout << "\t" << cCharacter[0].getCurrency() << endl;
cout << endl;
break;
case 5:
cout << "*****************************" << endl;
cout << "* Thanks for shopping *" << endl;
cout << "*****************************" << endl;
return;
default:
cout << "Please enter valid data." << "\n";
break;
}
cout << "*****************************" << endl;
cout << "* 1: Purchase Items *\n";
cout << "* 2: Sell Items *\n";
cout << "* 3: List Your Items *\n";
cout << "* 4: Currency *\n";
cout << "* 5: Exit *\n";
cout << "*****************************" << endl;
} while (responce != 5);
return;
}
All I can see that sets win is this part of the code,
if (cCharacter[1].getHealth() < 0) {
ran_pick = 0;
win = 1;
}
else if (cCharacter[1].getHealth() > 0) {
cout << "\n\t\t<" << cCharacter[1].getName() << "'s turn>" << endl;
win = 0;
}
However, if cCharacter[1].getHealth()==0, win will be left unset.
So I'm new to programming and I've encountered a problem when trying to make a basic text game.
BTW I do know I could've made some more functions to decrease the code (like instead of copy and paste the algorithm of the percentage I could've made a function that returned the value. But I just wanted to get it done so I could advance slowely
When compiling and running it expand the console, otherwise it'll not look good. (for menu and settings). Thanks in advance!
It isn't properly working. eHealth and hHealth (private class variables) isn't properly getting subtracted. I've the best I could but I have no idea what I'm doing wrong so please help me! Here's the source code:
2D game V1.cpp
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include "Header.h"
#include <windows.h>
using namespace std;
int main()
{
combat CombatEngine;
CombatEngine.renderMenu(0);
return 0;
}
Header.h
#ifndef Header_H_
#define Header_H_
class combat {
private:
int hHealth;
int eHealth;
public:
combat() : hHealth(100), eHealth(100) { };
combat(int H, int E) : hHealth(H), eHealth(E) {};
float fGen();
void renderMenu(int choise);
void renderSettings();
void renderHuman(int D);
void renderHumanAttack();
void refreshScreen();
void renderSkeleton(int D);
void renderSkeletonAttack();
void renderHumanVSkeleton(int D);
void renderHumanVSkeletonAttack(int D);
void inGame();
int attacks(int attack);
void dealDMG(int dmg, int d);
};
#endif
Combat Engine.cpp
#include "stdafx.h"
#include <random>
#include <iostream>
#include <Windows.h>
#include "Header.h"
#include "string"
#include <ctime>
using namespace std;
float combat::fGen() {
mt19937 fGenerator(time(NULL));
uniform_real_distribution<float> randF(0.0f, 1.0f);
float fNum(randF(fGenerator));
return fNum;
}
void combat::renderMenu(int rendNum) {
if (rendNum == 0)
{
for (int i = 0; i != 12; i++)
{
cout << "####################################################################################################\n";
}
cout << "############################################|-Play (1)|#############################################\n";
cout << "###########################################|Settings (2)|###########################################\n";
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
}
int dec = 0;
while (dec != 1 && dec != 2) {
cin >> dec;
if (dec != 1 && dec != 2)
{
cout << "Error: Invalid input!\n";
}
}
if (dec == 1) {
refreshScreen();
inGame();
}
if (dec == 2) {
renderSettings();
}
}
void combat::renderSettings() {
refreshScreen();
for (int i = 0; i != 12; i++)
{
cout << "####################################################################################################\n";
}
cout << "#####################################| change human health (1)|#####################################\n";
cout << "######################################| change mob health (2)|######################################\n";
cout << "#############################################|Back (3)|#############################################\n";
for (int i = 0; i != 12; i++)
{
cout << "####################################################################################################\n";
}
int dec = 0;
while (dec != 1 && dec != 2 && dec != 3)
{
cin >> dec;
if (dec != 1 && dec != 2 && dec != 3)
{
cout << "Error: Invalid input!\n";
}
}
for (;;)
{
if (dec == 1)
{
refreshScreen();
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
cout << "#############################| << Type the new value (previously: " << hHealth << ".)|############################\n";
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
int inc;
cin >> inc;
hHealth = inc;
refreshScreen();
renderMenu(0);
}
if (dec == 2)
{
refreshScreen();
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
cout << "#############################| << Type the new value (previously: " << eHealth << ".)|############################\n";
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
int inc;
cin >> inc;
eHealth = inc;
refreshScreen();
renderMenu(0);
}
if (dec == 3)
{
refreshScreen();
renderMenu(0);
}
}
}
void combat::renderHuman(int D) {
if (D == 0) {
cout << "_____\n";
cout << "| |\n";
cout << "O _ O\n";
cout << "/|_|\\ \n";
cout << "_| |_\n";
cout << "#############################################" << endl;
}
if (D == 1) {
cout << "_____\n";
cout << "| |\n";
cout << "O _ O/\n";
cout << "/|_|/ \n";
cout << "_| |_\n";
cout << "#############################################" << endl;
}
}
void combat::renderHumanAttack()
{
Sleep(500);
refreshScreen();
renderHuman(1);
Sleep(250);
refreshScreen();
renderHuman(0);
Sleep(500);
refreshScreen();
renderHuman(1);
Sleep(250);
refreshScreen();
renderHuman(0);
}
void combat::refreshScreen()
{
system("cls");
}
void combat::renderSkeleton(int D)
{
if (D == 0)
{
cout << " ___" << endl;
cout << "{o_o}" << endl;
cout << "/( )\\ " << endl;
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
if (D == 1)
{
cout << " ___" << endl;
cout << "\\{o_o}" << endl;
cout << " \\( )\\ " << endl;
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
}
void combat::renderSkeletonAttack()
{
Sleep(500);
refreshScreen();
renderSkeleton(1);
Sleep(250);
refreshScreen();
renderSkeleton(0);
Sleep(500);
refreshScreen();
renderSkeleton(1);
Sleep(250);
refreshScreen();
renderSkeleton(0);
}
void combat::renderHumanVSkeleton(int D)
{
if (D == 0)
{
//refreshScreen();
cout << "_____ \n";
cout << "| | ";
cout << " ___" << endl;
cout << "O _ O ";
cout << "{o_o}" << endl;
cout << "/|_|\\ ";
cout << "/( )\\ " << endl;
cout << "_| |_ ";
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
if (D == 1)
{
//human attacking
//refreshScreen();
cout << "_____ \n";
cout << "| | ";
cout << " ___" << endl;
cout << "O _ O/ ";
cout << "{o_o}" << endl;
cout << "/|_|/ ";
cout << "/( )\\ " << endl;
cout << "_| |_ ";
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
if (D == 2)
{
//skeleton attacking
//refreshScreen();
cout << "_____ \n";
cout << "| | ";
cout << " ___" << endl;
cout << "O _ O ";
cout << "\\{o_o}" << endl;
cout << "/|_|\\ ";
cout << " \\( )\\ " << endl;
cout << "_| |_ ";
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
}
void combat::renderHumanVSkeletonAttack(int D)
{
if (D == 0)
{
//human attacking
Sleep(500);
refreshScreen();
renderHumanVSkeleton(1);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
Sleep(500);
refreshScreen();
renderHumanVSkeleton(1);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
}
if (D == 1)
{
//skelleton attacking
Sleep(500);
refreshScreen();
renderHumanVSkeleton(2);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
Sleep(500);
refreshScreen();
renderHumanVSkeleton(2);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
}
}
int combat::attacks(int attack)
{
if (attack == 1) {
//25% chance for 15 DMG if hit 40% for hit
if (fGen() <= 0.25f)
{
if (fGen() <= 0.25f)
{
return 15 * 1.5;
}
else
return 15;
}
if (attack == 2) {
//60% chance for 7 DMG if hit 40% for hit
if (fGen() <= 0.60f)
{
if (fGen() <= 0.25f)
{
return 7 * 2;
}
else
return 7;
}
}
if (attack == 3) {
//75% chance for 3 DMG if hit 40% for hit
if (fGen() <= 0.75f)
{
if (fGen() <= 0.40f)
{
return 3 * 2;
}
else
return 3;
}
}
if (attack == 4) {
//10% chance for 20 DMG if hit 25% for crit
if (fGen() <= 0.10f)
{
if (fGen() <= 0.25f)
{
return (int)20 * (int)1.5;
}
else
return 20;
}
}
}
return 0;
}
void combat::dealDMG(int dmg, int d)
{
if (d == 0)
{
eHealth - dmg;
}
else
{
hHealth - dmg;
}
}
void combat::inGame()
{
float t = fGen();
cout << t;
bool who;
if (t >= 0.5f)
{
cout << "You start attacking first!" << endl;
who = true;
}
else
{
cout << "The skeleton starts attacking first!" << endl;
who = false;
}
while (eHealth > 0 || hHealth > 0)
{
if (!who)
{
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeleton(0);
Sleep(1500);
int decision;
float randF = fGen();
if (randF <= 0.25f)
decision = 1;
else if (randF > 0.25f && randF < 0.50f)
decision = 2;
else if (randF > 0.50f && randF < 0.75f)
decision = 3;
else
decision = 4;
int dmg = attacks(decision);
dealDMG(dmg, 1);
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeletonAttack(1);
who = true;
}
if (who)
{
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeleton(0);
cout << "It's your turn to attack!\n1) 25% chance to hit, DMG = 15. 40% chance of crit (22 DMG)\n2) 60% chance for 7 dmg. 40% chance for crit (14 DMG)\n3) 75 chance to hit, DMG = 3 dmg. 40% to crit (6 DMG)\n4)10% chance to hit, DMG = 20. 25% chance to crit (30 DMG)\n";
int decision2;
cin >> decision2;
while (decision2 != 1 && decision2 != 2 && decision2 != 3 && decision2 != 4) {
cin >> decision2;
}
int dmg = attacks(decision2);
dealDMG(dmg, 0);
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeletonAttack(0);
who = false;
}
}
if (eHealth == 0) {
refreshScreen();
renderHuman(0);
cout << "You won!" << endl;
renderMenu(0);
}
else
{
refreshScreen();
renderSkeleton(0);
cout << "You loose!" << endl;
renderMenu(0);
}
}
eHealth - dmg;
shouldn't that be
eHealth -= dmg;
and
#include "string"
try
#include <string>
Very new to programming.
This bit of my program accepts two strand of DNA as input and output them in a double helix drawing. The problem is, if one of the two input strand is longer than the other, i will receive error.
So I thought, is it possible that if strand[add] is non-existent anymore, replace it with *?
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
void helix(string &strand1, string &strand2)
{
int nucleo;
int length;
if (strand1.length() >= strand2.length())
{
length = strand1.length();
}
else
{
length = strand2.length();
}
int add;
for (int add = 0; add <= length - 1; add++)
{
if (add > 7)
{
nucleo = add % 8;
}
else
{
nucleo = add;
}
if (nucleo == 0)
{
cout << " " << strand1[add] << "---"<<strand2[add] << endl;
}
else if (nucleo == 1)
{
cout << " " << strand1[add] << "------" << strand2[add] << endl;
}
else if (nucleo == 2)
{
cout << " " << strand1[add] << "------" << strand2[add] << endl;
}
else if (nucleo == 3)
{
cout << " " << strand1[add] << "---" << strand2[add] << endl;
cout << " *" << endl;
}
else if (nucleo == 4)
{
cout << " " << strand2[add]<<"---" << strand1[add] << endl;
}
else if (nucleo == 5)
{
cout << " " << strand2[add]<<"------" << strand1[add] << endl;
}
else if (nucleo == 6)
{
cout << " " << strand2[add]<<"------" << strand1[add] << endl;
}
else if (nucleo == 7)
{
cout << " " << strand2[add]<<"-----" << strand1[add] << endl;
cout << " *" << endl;
}
}
}
int main()
{
string strand1,strand2;
cout << "ENTER STRAND:" << endl;
cin >> strand1;
cout << "ENTER STRAND:" << endl;
cin >> strand2;
helix(strand1,strand2);
_getch();
return 0;
}
I was hoping I could still show the longer strand even if the other side of the strand is empty(want to put *) like this :imgur.com/t7riVrS
I think you inverted the legnth test, it should be:
//if (strand1.length() >= strand2.length())
if (strand1.length() < strand2.length())
{
length = strand1.length();
}
else
{
length = strand2.length();
}
Edit:
If you want it fill one the string with '*', replace the code above with:
while (strand1.length() < strand2.length())
{
strand1 += "*";
}
while (strand1.length() > strand2.length())
{
strand2 += "*";
}
I am writing a yahtzee game for my c++ programming class. One of my difficulties I have ran into is the scoring system for different categories. I think I have figured out how to do it for adding 1s, 2s etc but I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled. Here is my code so far.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Declare variables
int players;
int turn = 1;
vector<string> names;
string playerName;
int dice[5];
int finalScore = 0;
char reroll[5];
char rollDice;
int tries = 1;
const int DICE = 5;
int roll[DICE];
int scorecard;
int scoreThisTurn(int scorecard);
int turnScore = 0;
//Introduction, get number of players.
cout << "Hello, welcome to Yahtzee! How many players are there?" << endl;
cin >> players;
if (players > 4) {
cout << "Sorry, the maximum number of players is 4." << endl;
cout << "How many players are there?" << endl;
cin >> players;
}
//Get player names
string getNames();
for (int i = 0; i < players; i++) {
cout << "Hello player " << i + 1 << ", please enter your name" << endl;
cin >> playerName;
names.push_back(playerName);
}
srand(time(NULL)); //random seed
cout << "Welcome to Yahtzee!\n";
while (turn <= 13) { //roll dice
cout << "Press 'r' to roll" << endl;
cin >> rollDice;
if (rollDice == 'r') {
for (int i = 0; i < DICE; i++) {
roll[i] = rand() % 6 + 1;
}
}
cout << "You rolled: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your second roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your third roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
//displays scorecard categories
cout << "Which category would you like to score this in" << endl;
cout << "1 - ones: " << endl;
cout << "2 - twos: " << endl;
cout << "3 - threes: " << endl;
cout << "4 - fours: " << endl;
cout << "5 - fives: " << endl;
cout << "6 - sixes: " << endl;
cout << "7 - 3 of a kind: " << endl;
cout << "8 - 4 of a kind: " << endl;
cout << "9 - small straight: " << endl;
cout << "10 - large straight: " << endl;
cout << "11 - full house: " << endl;
cout << "12 - yahtzee: " << endl;
cout << "13 - chance: " << endl;
//asks player to choose where to score
cout << "\nEnter 1-14 to choose a category." << endl;
cin >> scorecard;
//assigns points
for (int i = 0; i < DICE; i++) {
turnScore = 0;
if (scorecard == 1) {
if (roll[i] == 1) {
turnScore = turnScore + 1;
}
}
if (scorecard == 2) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
if (scorecard == 3) {
if (roll[i] == 3) {
turnScore = turnScore + 3;
}
}
if (scorecard == 4) {
if (roll[i] == 4) {
turnScore = turnScore + 4;
}
}
if (scorecard == 5) {
if (roll[i] == 5) {
turnScore = turnScore + 5;
}
}
if (scorecard == 6) {
if (roll[i] == 6) {
turnScore = turnScore + 6;
}
if (scorecard == 7) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
}
cout << scorecard << endl;
turn++;
}
system("pause");
return 0;
}
As you can see I've set up the scoring for the first 6 categories but don't know how to proceed.
I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled.
Create a variable to help you keep track of the number of dice that have a given number.
int diceCount[DICE] = {0};
and fill up the array with:
for (int i = 0; i < DICE; i++) {
diceCount[roll[i-1]]++
}
Create helper functions to determine whether five, four, or three of a kind have been thrown.
int getNOfAKind(int diceCount[], int N)
{
// This will need moving DICE out of `main`
// making it a global variable.
for ( int i = 0; i < DICE; ++i )
{
if (diceCount[i] == N )
{
return i+1;
}
}
return -1;
}
int getFiveOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 5);
}
int getFourOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 4);
}
int getThreeOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 3);
}
and use it as:
int fiveCount = getFiveOfAKind(diceCount);
if ( fiveCount != -1 )
{
}
int fourCount = getFourOfAKind(diceCount);
if ( fourCount != -1 )
{
}
int threeCount = getThreeOfAKind(diceCount);
if ( threeCount != -1 )
{
}