Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
my program seems to be running fine but at certain points the variable healthone seems to be added on too instead of being subtracted from like its supposed to be although there isnt a line in this code to adding to healtone.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
float healthone, healthtwo, ending;
int attack, move, random, chance, comprandom, attackp, attackcp, rando;
char c;
string comattackname;
cout << "Do you want to be Naruto or Goku? [n or g]: ";
cin >> c;
cout << " n";
cout << "Naruto and Goku enter the ring!" << endl;
cout << " n";
healthone = 23;
healthtwo = 23;
ending = 10;
while (ending >= 0) {
if (c == 'n') {
cout << "What move do you want to use?" << endl;
cout << "1 - Tail Beast Bomb" << endl;
cout << "2 - Rasengan Shurikan Barrage" << endl;
cout << "3 - Giant Rassengan Barrage" << endl;
cout << "4 - Rassengan Barrage" << endl;
cout << "5 - Rassengan Shuriken" << endl;
cout << "6 - Rasengan" << endl;
cout << "7 - Direct Punch" << endl;
cout << "8 - Shuriken" << endl;
cout << "9 - Enter Sage Mode" << endl;
cout << "10 - Enter Limited Tail Beast Mode" << endl;
cout << "11 - Enter 9 Tails Mode" << endl;
cout << "-";
cin >> move;
}
if (c == 'g') {
cout << "What move do you want to use?" << endl;
cout << "1 - Universal Spirit Bomb" << endl;
cout << "2 - Super Spirit Bomb" << endl;
cout << "3 - Continuos Kamehameha" << endl;
cout << "4 - Super Kamehameha" << endl;
cout << "5 - Kamehameha" << endl;
cout << "6 - Destructo Disk" << endl;
cout << "7 - Direct Punch" << endl;
cout << "8 - Ki Blast" << endl;
cout << "9 - Enter Sage Mode" << endl;
cout << "10 - Enter Limited Tail Beast Mode" << endl;
cout << "11 - Enter 9 Tails Mode" << endl;
cout << "-";
cin >> move;
}
if (move == 8) {
attackp = 1;
rando = 23;
}
if (move == 7) {
attackp = 2;
rando = 17;
}
if (move == 6) {
attackp = 3;
rando = 12;
}
if (move == 5) {
attackp = 5;
rando = 8;
}
if (move == 4) {
attackp = 8;
rando = 5;
}
if (move == 3) {
attackp = 12;
rando = 3;
}
if (move == 2) {
attackp = 17;
rando = 2;
}
if (move == 1) {
attackp = 23;
rando = 1;
}
chance = 1 + rand() % 23;
if (chance <= rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo = healthtwo - attackp;
} else {
cout << " " << endl;
cout << "Miss!" << endl;
}
comprandom = 1 + rand() % 8;
if (comprandom == 8) {
attackcp = 1;
rando = 23;
}
if (comprandom == 7) {
attackcp = 2;
rando = 17;
}
if (comprandom == 6) {
attackcp = 3;
rando = 12;
}
if (comprandom == 5) {
attackcp = 5;
rando = 8;
}
if (comprandom == 4) {
attackcp = 8;
rando = 5;
}
if (comprandom == 3) {
attackcp = 12;
rando = 3;
}
if (comprandom == 2) {
attackcp = 17;
rando = 2;
}
if (comprandom == 1) {
attackcp = 23;
rando = 1;
}
if (c = 'n') {
if (comprandom == 1) {
comattackname = "Tail Beast Bomb";
}
if (comprandom == 2) {
comattackname = "Rasengan Shurikan Barrage";
}
if (comprandom == 3) {
comattackname = "Giant Rassengan Barrage";
}
if (comprandom == 4) {
comattackname = "Rassengan Barrage";
}
if (comprandom == 5) {
comattackname = "Rassengan Shuriken";
}
if (comprandom == 6) {
comattackname = "Rasengan";
}
if (comprandom == 7) {
comattackname = "Direct Punch";
}
if (comprandom == 8) {
comattackname = "Shuriken";
}
} else if (comprandom == 1) {
comattackname = "Universal Spirit Bomb";
}
if (comprandom == 2) {
comattackname = "Super Spirit Bomb";
}
if (comprandom == 3) {
comattackname = "Continuos Kamehameha";
}
if (comprandom == 4) {
comattackname = "Super Kamehameha";
}
if (comprandom == 5) {
comattackname = "Kamehameha";
}
if (comprandom == 6) {
comattackname = "Destructo Disk";
}
if (comprandom == 7) {
comattackname = "Direct Punch";
}
if (comprandom == 8) {
comattackname = "Ki Blast";
}
chance = 1 + rand() % 23;
if (chance <= rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone = healthone - attackcp;
} else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
cout << "Your health: " << healthone << endl;
cout << "Comp health: " << healthtwo << endl;
cout << " " << endl;
if (healthone > healthtwo) {
ending = healthone;
}
if (healthone < healthtwo) {
ending = healthtwo;
}
if (healthone = healthtwo) {
ending = healthone;
}
if (healthone <= 0) {
cout << "You loose." << endl;
cout << " " << endl;
}
if (healthtwo <= 0) {
cout << "You win." << endl;
cout << " " << endl;
}
}
}
This line is your problem:
if (healthone=healthtwo)
{
ending=healthone;
}
Change it to:
if (healthone==healthtwo)
{
ending=healthone;
}
I don't know about your IDE, but many of them will flag something like this as a warning. Check your warnings...
Related
I do not know how to make my program repeat without a do-while loop. The loop makes my program repeat saying "the mode is set to dumb" and "it is your turn." I could really use some help on completing this program because it is homework and due tonight at 9:30. The game is taking marbles out of a pile and the person to take the last marble loses.
// new project 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ctime>
#include <string>
#include <iostream>
#include <random>
int smartcomputer(int);
int dumbcomputer(int, int);
int playerturn(int, int);
using namespace std;
int main()
{
bool flag = true;
int marbletake = 0;
string dumborsmart;
srand(time(0));
int pilesize = rand() % (100 - 10) + 10;
cout << "Hello, Welcome to the game of Nim. Two players alternatively take marbles from a pile." << endl;
cout << "Each move a player must take at least one marble but at most half of the marbles." << endl;
cout << "Enter dumb for dumb mode and smart for smart mode." << endl;
getline(cin, dumborsmart);
do {
if (dumborsmart == "dumb") {
cout << "The mode is set to dumb." << endl;//set to dumb
bool turn = rand() % 2;
if (turn = true) {
cout << "It is your turn" << endl;
}
else {
cout << "It is the bots turn" << endl;
}
if (turn = false) { //not player's turn=false
if (dumborsmart == "dumb") {
pilesize = dumbcomputer(marbletake, pilesize);
bool turn = true;
}
else {
pilesize = smartcomputer(pilesize);
bool turn = true;
}
}
}
else {
pilesize = playerturn(marbletake, pilesize);
bool turn = false;
}
} while (pilesize != 1);
return 0;
}
int playerturn(int marbletake, int pilesize){
cout << "It is your turn. Marbles remaining in pile: " << pilesize << endl;
cout << "Enter a number of marbles to remove from the pile" << endl;
cin >> marbletake;
if (marbletake > 1 && marbletake < (pilesize / 2)) {
pilesize = pilesize - marbletake;
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You win. The bot is forced to take the last marble." << endl;
system("pause");
}
}
else {
cout << "Error, please remove a number of marbles greater than 0 or less than half the pile." << endl;
cin >> marbletake;
pilesize = pilesize - marbletake;
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You win. The bot is forced to take the last marble." << endl;
}
}
return pilesize;
}
int smartcomputer(int pilesize){
cout << "The bot is removing marbles from the pile." << endl;
if (pilesize > 63) {
pilesize = 63;
}
else if (pilesize > 31 && pilesize < 63) {
pilesize = 31;
}
else if (pilesize > 15 && pilesize < 31) {
pilesize = 15;
}
else if (pilesize > 7 && pilesize < 15) {
pilesize = 7;
}
else if (pilesize > 3 && pilesize < 7) {
pilesize = 3;
}
else {
pilesize = pilesize - rand() % (pilesize / 2) + 1;
}
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You lose. You are forced to take the last marble." << endl;
}
return pilesize;
}
int dumbcomputer(int marbletake, int pilesize){
cout << "The bot is removing marbles from the pile." << endl;
pilesize = (pilesize - rand() % (pilesize / 2) + 1); // bot takes marbles from pile(half pile size-1)
cout << "Number of marbles in the pile: " << pilesize << endl;
if (pilesize ==1) {
cout << "You lose. You are forced to take the last marble." << endl;
}
return pilesize;
}
Take the cout line that prints the mode out of the loop:
cout << "The mode is set to " << dumborsmart << "\n";
do {
...
} while (pilesize != 1);
Also, if (turn = true) and if (turn = false) won't work, you need to use == there. But it's even better to write if (turn) and if (!turn).
And in your else if (pilesize > X && pilesize < Y) tests, you're skipping the cases where pilesize == Y. You should just remove && pilesize < Y, since it's already guaranteed by the fact that the previous if failed, then these boundary cases won't be skipped.
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.
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 )
{
}
Im sitting here for two hours and my mind is blowing. Im trying to code a generator for linear equation with one unknown. It has 3 combinations:
ax+b=c (ex. 2x-3=1)
a+bx=c (ex. 3-2x=1)
a+b=cx (ex. 4-6=2x)
So i did some simple math, made an algorithm (x=(c-b)/a), excluded zero from denominator etc. The whole point is that to make the result as integer. That's why there is this code:
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
First two combinations are working perfectly, but third one isn't. The example of an output from the third one is:
5-7=6x AND the calculated solution is x = 2. It would be x=2 if there was a + between 5 and 7. And it always goes this way. This one digit is wrong. The problem situated not in printing in the console, but in calculations.
If you have any idea, please help me to solve this.
Here is the code:
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int level_1()
{
/* LEVEL 1 - ROWNANIE Z 1 NIEWIADOMA*/
int a = 5, b = 123, c = 32;
double x;
double answer;
double licznik, mianownik;
cout << "level 1" << endl;
// losujemy kombinacje (1 z 3) dla roznorodnosci
int losowanie = 3;//= rand() % 3 + 1;
if (losowanie == 1) {
cout << "Rolled: 1" << endl << endl; // x jest przy wspolczynniku a
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
if (b > 0) cout << "+" << b;
else if (b == 0) cout << "";
else cout << b;
cout << "=";
if (c >= 0) cout << c;
else cout << c;
x = (c - b) / a;
cout << endl << "Type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
else if (losowanie == 2){
cout << "Rolled: 2" << endl << endl; // x jest przy wspolczynniku b
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
if (b!=0)
cout << b;
if (a < 0 || a == 0) cout << "";
else if (a>0) cout << "+";
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
cout << "=" << c;
x = (c - b) / a;
cout << endl << "Type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
else
{
cout << "You rolled: 3" << endl << endl; // x jest przy wspolczynniku c
a = 5;
while (((c - b) % (a) != 0))
{
do
{
a = rand() % 24 - 12;
} while (a == 0);
b = rand() % 24 - 12;
c = rand() % 24 - 12;
}
cout << a << endl << b << endl << c << endl;
if (c != 0) cout << c;
if (b != 0)
{
if (b > 0) cout << "+";
else cout << "";
cout << b;
}
cout << "=";
if (a == 1) cout << "x";
else if (a == -1) cout << "-x";
else cout << a << "x";
x = ((c - b) / a);
cout << endl<< "zzz" << x << endl;
cout << endl << "type x = ";
cin >> answer;
if (answer == x)
{
cout << endl << "good answer" << endl;
return 1;
}
cout << endl << "bad answer." << endl;
return 0;
}
return 1;
}
int main()
{
//cout << y << endl;
srand(time(NULL));
while (1){
level_1();
}
}
x=(c-b)/a only holds true for equations of the first form. The other ones would be x = (c-a)/b and x = (a+b)/c. You should change your while loops accordingly.
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.