need help with game code in C++

September 6, 2011 at 15:49:29
Specs: Windows 7
Here is the code. I am trying to play a game call war. its a card game. I have everything working correctly except one thing and it will probally be an easy fix. but for every loop i want the program to either add one to a number or subject one. help??

#include "string"
#include "iostream"
#include "calculator.h"
#include "war.h"
#include "ctime"

using namespace std;


void cards::play()
{
cout << "You have selected to play war!" << endl;
cout << "What is war?? It is a card game. You and the cpu will get one card";
cout << "and who ever gets the highest cards wins that hand untill either";
cout << "you or the cpu has no cards left!";
cout << endl;
cout << "Get ready!" << endl;
cout << endl;

bool quit = false;

while ( !quit)
{
int cpu = 1 + rand() % (14 - 2);
int human = 1 + rand() % (14 - 2);
int cardCpu = 20;
int cardHuman = 20;
int yes = 0;

if (cpu > human)
{
cout << "cpu = " << cpu << " human = " << human << endl;
cardHuman -= 1;
cardCpu += 1;
cout << endl;
cout << "Your cards left: " << cardHuman << endl;
cout << "Cpu's cards left: " << cardCpu << endl;
cout << "Do you wish to quite? 1) yes or 2) no"; cin >> yes; cout << endl;

if (yes == 1)
{
cout << "You have choosen to quit war!" << endl;
quit = true;
}

if (yes == 2)
{
quit = false;
}
}

if (cpu < human)
{
cout << "cpu = " << cpu << "human = " << human << endl;
cardHuman += 1;
cardCpu -= 1;
cout << endl;
cout << "Your cards left: " << cardHuman << endl;
cout << "Cpu's cards left: " << cardCpu << endl;
cout << "Do you wish to quite? 1) yes or 2) no"; cin >> yes; cout << endl;

if (yes == 1)
{
cout << "You have choosen to quit war!" << endl;
quit = true;
}

if (yes == 2)
{
quit = false;
}
}

if (cpu == human)
{
cout << "cpu = " << cpu << "human = " << human << endl;
cout << "You have tied the computer meaning you do not gain another card!" << endl;
cout << "Your cards left: " << cardHuman << endl;
cout << "Cpu's cards left: " << cardCpu << endl;
cout << endl;
cout << "Do you wish to quite? 1) yes or 2) no"; cin >> yes; cout << endl;

if (yes == 1)
{
cout << "You have choosen to quit war!" << endl;
quit = true;
}

if (yes == 2)
{
quit = false;
}
}
if (cardHuman == 0)
{
cout << "Sorry you have lost the game!" << endl;
quit = true;
}

if (cardCpu == 0)
{
cout << "You have won the game!" << endl;
quit = true;
}
}
}


See More: need help with game code in C++

Report •


#1
September 6, 2011 at 17:22:08
Could you load your rand() into a variable and then add 1 to it?

Report •

#2
September 7, 2011 at 17:11:04
I guess what i want to say is that i want to add one to number like the following.
int cardHuman = 20;
then below this which i want to happen.
cardHuman += 1;

but this doesnt work but only for the first look. this clear it up??


Report •

#3
September 8, 2011 at 02:22:11
You arre initializing your cardHuman and cardCPU values within the while loop. So they get reinitialized every time round the loop. Move the initialization outside (before) the loop.

Report •

Related Solutions


Ask Question