Help with C++

Grab your favourite IDE and tinker with the innards of game engines

Help with C++

Postby jaddorii on Mon Apr 02, 2007 2:07 pm

I need some help with C++. I tried to make this text-based game where u can choose to attack, defend or wait. Then the computer randomly chose a number of gold you gain etc. But when u attack again the computer kinda forgot how much gold u had after the last attack. So is there a way to store numbers through the code?
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby Nexus Trimean on Mon Apr 02, 2007 6:41 pm

post your code, cant help unless i can see where the issue is,
though it sounds like you good part comes after you combat part ends, and is skipped.
**% of people listen to music. If you're one of the people who doesn't care what others listen to as long as they are wearing headphones, join the quest to leave no Head unphoned. And keep your sanity.
User avatar
Nexus Trimean
Regular
Regular
 
Joined: Wed Nov 29, 2006 2:23 am

Postby jaddorii on Mon Apr 02, 2007 6:55 pm

Here's the code:

// Ett medival spel

#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
int attack();

int main()
{
// Allied Starting Forces

int FootSoldiers = 500;
int HorseMen = 100;
int Cannons = 50;
int Citizen = 400;

// Enemy Starting Forces

int EneSold = 1000;
int EneHors = 500;
int EneCans = 100;

srand(time(0));

// Allied Killing Dices

int SoldDice = rand() % 250; // Dice for allied soldiers dying
int HorsDice = rand() % 50; // Allied Horse Men diying
int CanDice = rand() % 5; // Allied Cannon destroyed

// Gold Dices

int GoldDice = rand() % 20000;
int GoldDiceDefend = rand() % 2000;
int SpyGoldDice = rand() % 200000;
int WaitGold = rand() % 20000;

// Enemy Killing Dices

int KillDiceSold = rand() % 400;
int KillDiceHors = rand() % 200;
int KillDiceCan = rand() % 50;

// Choose

char action = 'w';

cout << endl
<< "Hi and welcome to this Medival Game." << endl
<< "You will be starting with 500 Footsoldiers, 100 Horse Men, 50 Cannons," << endl
<< "400 citizens and 1000 gold." << endl
<< "What would you like to do?" << endl
<< "A = Attack" << endl << "D = Defend" << endl << "W = Wait" << endl
<< endl << endl //Give some space
<< "Attack:" << endl
<< "Attack your enemy to steal gold and capture prisoners." << endl << endl << endl //More space
<< "Defend:" << endl
<< "Defend your castle and send out spy's." << endl << endl << endl
<< "Wait:" << endl
<< "Wait for your citizens to train and join ur army, but be carefull you will be -" << endl
<< "vurnable against attacks."
<< endl;
cin >> action;

Attack:
if((action == 'A') || (action == 'a'))
{
srand(time(0)); // Initialize random number generator.

int SoldDice = rand() % 250; // Dice for allied soldiers dying
int HorsDice = rand() % 50; // Allied Horse Men diying
int CanDice = rand() % 5; // Allied Cannon destroyed

int GoldDice = rand() % 20000; // Gold Gained

int KillDiceSold = rand() % 500; // Enemy Soldiers Killed
int KillDiceHors = rand() % 100; // Enemy Horse Men killed
int KillDiceCan = rand() % 20; // Enemy Cannons Destoyed

char action2 = 'w';
char conti = 'n';

int Loss = 0;
int FootSoldiers = 500;
int HorseMen = 100;
int Cannons = 50;
int Gold = 1000;

cout << endl
<< "Your army attacks the enemy." << endl << endl
<< "===============================================================================" << endl
<< "You loose: " << SoldDice << " Footsoldiers, " << HorsDice << " Horse Men and " << CanDice << " Cannons." << endl
<< "===============================================================================" << endl
<< endl
<< endl
<< "===============================================================================" << endl
<< "You gained: "<< GoldDice << " gold from the enemy." << endl
<< "===============================================================================" << endl
<< "You killed: " << KillDiceSold << " enemy Footsoldiers, " << KillDiceHors << " enemy Horse Men and " << KillDiceCan << " enemy Cannons" << endl
<< "===============================================================================" << endl
<< endl; // To make some space

int NewSold = FootSoldiers - SoldDice;
int NewHorse = HorseMen - HorsDice;
int NewCan = Cannons - CanDice;
int NewGold = Gold + GoldDice;

cout << endl
<< "===============================================================================" << endl
<< "Now you've got " << NewSold << " Footsoldiers, " << NewHorse << " Horse Men and " << NewCan << " Cannons." << endl
<< "===============================================================================" << endl
<< "Now you've got " << NewGold << " coins of gold." << endl
<< "==============================================================================="
<< endl;

cout << endl
<< "Do u wish to continue? Y or N."
<< endl;
cin >> conti;

if((conti == 'Y') || (conti == 'y'))
{
cout << endl
<< "What would u like to do next?" << endl
<< "A = Attack." << endl
<< "D = Defend." << endl
<< "W = Wait."
<< endl;
cin >> action2;

if((action2 == 'A') || (action2 == 'a'))
{
goto Attack;
}

if((action2 == 'D') || (action2 == 'd'))
{
goto Defend;
}

if((action2 == 'W') || (action2 == 'w'))
{
goto Wait;
}

else
{
return 0;
}
}

else
{
cout << endl
<< "Good bye and Good luck!"
<< endl;

return 0;
}
}

Defend:
if((action == 'D') || (action == 'd'))
{
srand(time(0)); // Initialize random number generator.

int SoldDice = rand() % 100; // Dice for allied soldiers dying
int HorsDice = rand() % 10; // Allied Horse Men diying
int CanDice = rand() % 10; // Allied Cannon destroyed

int GoldDice = rand() % 20000; // Gold Gained

int KillDiceSold = rand() % 750; // Enemy Soldiers Killed
int KillDiceHors = rand() % 200; // Enemy Horse Men killed
int KillDiceCan = rand() % 5; // Enemy Cannons Destoyed

char action2 = 'w';
char conti = 'n';

int Loss = 0;
int FootSoldiers = 500;
int HorseMen = 100;
int Cannons = 50;
int Gold = 1000;

cout << endl
<< "Your army defends the castle." << endl
<< "===============================================================================" << endl
<< "You loose: " << endl
<< SoldDice << " Footsoldiers, " << endl
<< HorsDice << " Horse Men and " << endl
<< CanDice << " Cannons." << endl
<< "===============================================================================" << endl
<< endl
<< "===============================================================================" << endl
<< "You gained: "<< GoldDice << " gold from the enemy." << endl
<< "===============================================================================" << endl
<< "You killed: " << KillDiceSold << " enemy Footsoldiers, " << KillDiceHors << " enemy Horse Men and " << KillDiceCan << " enemy Cannons" << endl
<< "==============================================================================="
<< endl; // To make some space

int NewSold = FootSoldiers - SoldDice;
int NewHorse = HorseMen - HorsDice;
int NewCan = Cannons - CanDice;
int NewGold = Gold + GoldDice;

cout << endl
<< "===============================================================================" << endl
<< "Now you've got " << NewSold << " Footsoldiers, " << NewHorse << " Horse Men and " << NewCan << " Cannons." << endl
<< "===============================================================================" << endl
<< "Now you've got " << NewGold << " coins of gold." << endl
<< "==============================================================================="
<< endl;

cout << endl
<< "Do you wish to continue? Y or N."
<< endl;
cin >> conti;

if((conti == 'Y') || (conti == 'y'))
{
cout << endl
<< "What would u like to do next?" << endl
<< "A = Attack." << endl
<< "D = Defend." << endl
<< "W = Wait."
<< endl;
cin >> action2;

if((action2 == 'A') || (action2 == 'a'))
{
goto Attack;
}

if((action2 == 'D') || (action2 == 'd'))
{
goto Defend;
}

if((action2 == 'W') || (action2 == 'w'))
{
goto Wait;
}

else
{
return 0;
}
}

else
{
cout << endl
<< "Good bye and Good luck."
<< endl;

return 0;
}
}

Wait:
if((action == 'W') || (action == 'w'))
{
srand(time(0)); // Initialize random number generator.

int SoldDice = rand() % 50; // Dice for allied soldiers dying
int HorsDice = rand() % 10; // Allied Horse Men diying
int CanDice = rand() % 10; // Allied Cannon destroyed

int GoldDice = rand() % 20000; // Gold Gained

int KillDiceSold = rand() % 10; // Enemy Soldiers Killed
int KillDiceHors = rand() % 5; // Enemy Horse Men killed
int KillDiceCan = rand() % 1; // Enemy Cannons Destoyed

char action2 = 'w'; //Attack,defend or wait
char conti = 'n'; // Continue or not

int Loss = 0;
int FootSoldiers = 500;
int HorseMen = 100;
int Cannons = 50;
int Gold = 1000;

cout << endl
<< "Your army waits for recruits." << endl
<< "===============================================================================" << endl
<< "You loose: " << endl
<< SoldDice << " Footsoldiers, " << endl
<< HorsDice << " Horse Men and " << endl
<< CanDice << " Cannons." << endl
<< "===============================================================================" << endl
<< endl
<< "===============================================================================" << endl
<< "You gained: "<< GoldDice << " gold from different taxes." << endl
<< "===============================================================================" << endl
<< "You killed: " << KillDiceSold << " enemy Footsoldiers, " << KillDiceHors << " enemy Horse Men and " << KillDiceCan << " enemy Cannons" << endl
<< "==============================================================================="
<< endl; // To make some space

int NewSold = FootSoldiers - SoldDice;
int NewHorse = HorseMen - HorsDice;
int NewCan = Cannons - CanDice;
int NewGold = Gold + GoldDice;

cout << endl
<< "===============================================================================" << endl
<< "Now you've got " << NewSold << " Footsoldiers, " << NewHorse << " Horse Men and " << NewCan << " Cannons." << endl
<< "===============================================================================" << endl
<< "Now you've got " << NewGold << " coins of gold." << endl
<< "==============================================================================="
<< endl;

cout << endl
<< "Do you wish to continue? Y or N."
<< endl;
cin >> conti;

if((conti == 'Y') || (conti == 'y'))
{
cout << endl
<< "What would u like to do next?" << endl
<< "A = Attack." << endl
<< "D = Defend." << endl
<< "W = Wait."
<< endl;
cin >> action2;

if((action2 == 'A') || (action2 == 'a'))
{
goto Attack;
}

if((action2 == 'D') || (action2 == 'd'))
{
goto Defend;
}

if((action2 == 'W') || (action2 == 'w'))
{
goto Wait;
}

else
{
return 0;
}
}

else
{
cout << endl
<< "Good bye and Good luck."
<< endl;

return 0;
}
}

return 0;
}


I know I did some unnecessary things in the begining but ...

So what I need is something for the computer to remember how many (for example) soldiers I have left after attacking.
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby Psy on Mon Apr 02, 2007 7:01 pm

I'm glad you've got further with C++ then I ever could! :D
User avatar
Psy
Veteran
Veteran
 
Joined: Sat Dec 03, 2005 5:41 pm
Location: United Kingdom

Postby bobthehobo on Mon Apr 02, 2007 7:03 pm

Yeah seriously. It's a damn hard language.
Image
Do the internet a favor: proof read your posts.
XBL: Doctor Fifer
User avatar
bobthehobo
Pheropod
Pheropod
 
Joined: Thu Sep 08, 2005 9:27 pm

Postby jaddorii on Mon Apr 02, 2007 7:08 pm

wOOt? I learned this in about a week! This code is probably in the nOOb stage of programing :P It's just a text-based mediaval game xD

Ohh btw the { (semicolon) got messed up in placement when i copied it to interlopers!
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby Mr. Happy on Mon Apr 02, 2007 7:12 pm

Post the finished exe when you figure out the problem!
Image
-You've just been happified!?
User avatar
Mr. Happy
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Sat Dec 30, 2006 9:20 am
Location: Flyin' thru "da cloud" in the MotherShip

Postby jaddorii on Mon Apr 02, 2007 7:15 pm

Sure! I just need to get som HELP!! Can't find any good tutorials on the net either :x i guess i will hade to try everything i know until i figure out what to do. Btw i cant seem to use the & and * operators in C++ cuse the compiler get nuts when i do. Any1 knows why?
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby vecima on Mon Apr 02, 2007 8:26 pm

ok, i'm reading through this now, but first something caught my eye.

don't use "goto" statements. it's just bad form....

i'll edit this post with whatever else i find...

EDIT:
ok, all of your defined blocks of code (Attack, Wait, Defend) should be methods. i see you have a method prototype at the top for an int attack() method, but it's never defined. i would also make a method for all of your "do you want to continue?" and "what do you want to do next?" parts. it can be pretty easily done, and save you a little bit of code.

methods are defined outside of main. you can either put them above or below main (or even in seperate files) but if you put them below main, they have to have a prototype above main. a prototype basically says "there will be a method called this" (just like you have 'int attack()' above your main).

one problem i also noticed is that in your goto blocks, you're checking the variable action, but it looks like it's only being read in once (in the beginning).

ok, let's make your methods.

first input:
the idea here is to type this out once, and be able to use it again and again so that it doesn't require more typing. whenever you need to do this input you just call it.

Code: Select all
// the input method
void input()
{
   int action
   cout << "What would you like to do?\n";
   cout << "(A - attack, D - defend, W - wait, Q - quit).\n";
   cin >> action;
   if((action == 'A') || (action == 'a'))
   {
      // you had: goto Attack;
      attack(); // we'll define this method in a bit.
   }

   if((action == 'D') || (action == 'd'))
   {
      // you had: goto Defend;
      defend();
   }
   
   if((action == 'W') || (action == 'w'))
   {
      // you had: goto Wait;
      wait();
   }

   if((action == 'Q') || (action == 'q'))
   {
      //quit the game.
      return 0;
   }
}


another thing i noticed is that you don't check for correct input.
for example you ask the user:
"Do you wish to continue? Y or N."

if the user says yes, you ask:
"What would u like to do next?"
"A = Attack."
"D = Defend."
"W = Wait."

but here, if the user accidentally types another character (other than a, d, or w) the program quits. you should include a subroutine here that checks the input to be sure it's valid. after all, they DID tell you they wish to continue.

something like this added to the bottom of your input method might do the trick:

Code: Select all
if ((action != 'A') && (action != 'a')
 && (action != 'D') && (action != 'd')
 && (action != 'W') && (action != 'w'))
{
   cout << "your input was invalid, please try again.\n";
   input();
}


now for your attack / defend / wait methods:
we don't want to check if action is equal to 'A', 'D', or 'W' here, because if the attack method gets called, then we know for sure we want to attack. the same is true of the defend and wait methods. i think if you take all the code inside those if checks and put it in your methods, it should work. unfortunately i get off work right now and don't have time to look through all the lines individually. try it out and post if you still have trouble
Last edited by vecima on Mon Apr 02, 2007 9:08 pm, edited 3 times in total.
vecima
1337 p0st3r
1337 p0st3r
 
Joined: Sun Apr 16, 2006 8:48 pm
Location: Jersey Boy

Postby Nexus Trimean on Mon Apr 02, 2007 8:33 pm

Well, you have 3 variables for gold , and it looks to me like they are getting reset to the origional value of 1000 Every round. there is no place that i can see that adds the gold you get from combat to your total gold stockpile. im not that good with C++ but i know the basics.

Edit, okay this is going to be in totaly wrong syntax and stuff, but i use basic.

Creat a int for each of your primary variable, Like int SoldierTotal, and all the others, gold, horses and cannons.
you set this at the very begining, before you start with the combat loops. this gives you your base varialbes.

now instead of setting them in each loop, you just call the master varialbe. to which you add or subtract any losses and gains.
Do not set the master variables in any place that they will execute more than once. set them, and then use your dice rolling portions to create other variables to add or subtract from the master variables.
**% of people listen to music. If you're one of the people who doesn't care what others listen to as long as they are wearing headphones, join the quest to leave no Head unphoned. And keep your sanity.
User avatar
Nexus Trimean
Regular
Regular
 
Joined: Wed Nov 29, 2006 2:23 am

Postby DarkMortar on Mon Apr 02, 2007 8:53 pm

you can store your stuff in arrays.

... or you can write/read to text files...
check this out, http://www.cplusplus.com/doc/tutorial/files.html
Image
DarkMortar wrote:If you copied and pasted this in notepad, you may be a nerd. If you just highlighted this, you may be intelligent. If you read this like it is, you may become blind.
User avatar
DarkMortar
Senior Member
Senior Member
 
Joined: Wed Feb 09, 2005 12:19 am
Location: California, US.

Postby Dead-Inside on Mon Apr 02, 2007 10:50 pm

I'd go for arrays and save those to a text or ini file upon closing the program/backups.
Image
User avatar
Dead-Inside
Veteran
Veteran
 
Joined: Wed Jan 05, 2005 4:15 pm
Location: Sweden (It's just as cold as you think it is)

Postby Forceflow on Tue Apr 03, 2007 12:52 am

Just a quick remark:

C++ is supposed to be object-oriented. You're just writing vanilla C.

I know it might be a hard thing to conceptualize, but you can start by defining distinct methods, even a class for "Money" and other objects which are part of the game world. Trust me, it will make things a lot easier later on in the development.
User avatar
Forceflow
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 12:13 pm
Location: Belgium

Postby Mr. Happy on Tue Apr 03, 2007 1:04 am

vecima wrote:ok, i'm reading through this now, but first something caught my eye.

don't use "goto" statements. it's just bad form....



My dad told me you can write any program using only goto statements :shock:

EDIT: He used to be a professional RPG programmifier for the AS-400, but he also bullshits alot :?
Image
-You've just been happified!?
User avatar
Mr. Happy
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Sat Dec 30, 2006 9:20 am
Location: Flyin' thru "da cloud" in the MotherShip

Postby vecima on Tue Apr 03, 2007 1:37 am

Mr Happy wrote:...
My dad told me you can write any program using only goto statements :shock:...


he's absolutely right. it's just bad form.

you can also write any program in a language called whitespace. it is made up of only space, tab, and carriage return characters. it's a real language, you can write programs in it, but if you try, have fun debugging invisible code.

people aren't nearly as masochistic in their code writing today, as they were in the past.

jaddorii, object oriented is really the way to go... i was going to get to that if i didn't have to leave work earlier. but then someone else beat me to it. so listen to them!
vecima
1337 p0st3r
1337 p0st3r
 
Joined: Sun Apr 16, 2006 8:48 pm
Location: Jersey Boy
Next

Return to Programming

Who is online

Users browsing this forum: No registered users