Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   RPG Code Issue (http://www.go4expert.com/forums/rpg-code-issue-t26569/)

tonydav43 26Aug2011 04:49

RPG Code Issue
 
Another problem, I am trying to test to see if the player has enough gold to purchase items from the store before purchasing them, however it allows me to buy any item on the first input, no matter how much it costs, and no matter how much gold I have, it then puts me into a minus figure for gold. If you after the first purchase try to buy something else, it does test to see if you have enough gold, and if not it stops you from buying it.

Code:


if (player.getGold() < mGoldRequired )

*********{
************cout << endl;
************cout << "You dont have enough gold for that" << endl;
************cout << endl;
******break ;
*********}

I have tried swapping the code around, using different operators etc, Dont understand as I am using the same piece of code that I used for magic points for spells, and that works fine. I am calling the get gold from player, and the mGoldRequired is set to private in store.h

Here is the complete file

Code:



// Store cpp
#include <iostream>
#include "Store.h"
#include "Random.h"
using namespace std;


void Store::enter(Player& player)

{

******int select;
******bool leave = false;

************cout << "Welcome to my store, what can I do for you?" << endl;
************cout << endl;

******while (!leave)

***{

************cout << "1) View Prices, 2) Buy Potions, 3) Buy Armor, 4) Rest a while, 5) Leave... ";
************cin >> select;
************cout << endl;


******switch(select)
******{

******case 1:
************cout << "==== POTIONS ====" << endl;
************cout << endl;
************cout << "Belt 500, Heal 100, Poison 100, Invisability 1000" << endl;
************cout << endl;
************cout << "==== ARMOR ====" << endl;
************cout << endl;
************cout << "Leather 100, Ring 200, Splintered 300, Mail 500" << endl;
************cout << endl;
*********break;
******
******case 2:
*********{
************cout << "What Potions would you like to buy?" << endl;
************cout << endl;
************cout << "We have 1) Belt, 2) Heal, 3) Poison, 4) Invisability...";
*********
************int potion = 1;
************cin >> potion;

*********if (player.getGold() < mGoldRequired )

*********{
************cout << endl;
************cout << "You dont have enough gold for that" << endl;
************cout << endl;
******break ;
*********}

***
******if(potion == 1)
*********{
************mPotionName    = "Belt";
************cout << endl;
************cout << "You have purchased a " << mPotionName << " Potion for 500 Gold"<< endl;
************cout << endl;
************mGoldRequired = 500;
************player.SetGold (- 500);
*********}

******else if(potion == 2)
*********{
************mPotionName    = "Heal";
************cout << endl;
************cout << "You have purchased a " << mPotionName << " Potion for 100 Gold"<< endl;
************cout << endl;
************mGoldRequired = 100;
************player.SetGold (- 100);
************player.SetHitPoints (+ 15);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else if(potion == 3)
*********{
************mPotionName    = "Poison";
************cout << endl;
************cout << "You have purchased a " << mPotionName << " Potion for 100 Gold"<< endl;
************cout << endl;
************mGoldRequired = 100;
************player.SetGold (- 100);
************player.SetHitPoints (+ 50);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else
*********{
************mPotionName    = "Invisability";
************cout << endl;
************cout << "You have purchased a " << mPotionName << " Potion for 1000 Gold" << endl;
************cout << endl;
************mGoldRequired = 1000;
************player.SetGold (- 1000);
************player.SetHitPoints (+ 200);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}
******break;
*********}

******case 3:

*********{
************cout << "What Armor would you like to buy?" << endl;
************cout << endl;
************cout << "We have 1) Leather, 2) Ring, 3) Splintered, 4) Mail...";
*********
*********int armor = 1;
*********cin >> armor;

******if (player.getGold() < mGoldRequired )

*********{
************cout << endl;
************cout << "You dont have enough gold for that" << endl;
************cout << endl;

******break ;
*********}

******if(armor == 1)
*********{
************mClothingName = "Leather Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 100 Gold "<< endl;
************cout << endl;
************mGoldRequired = 100;
************player.SetGold (- 100);
************player.SetHitPoints (+ 20);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else if(armor == 2)
*********{
************mClothingName = "Ring Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 200 Gold " << endl;
************cout << endl;
************mGoldRequired = 200;
************player.SetGold (- 200);
************player.SetHitPoints (+ 30);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}
******

******else if(armor == 3)
*********{
************mClothingName = "Splintered Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 300 Gold " << endl;
************cout << endl;
************mGoldRequired = 300;
************player.SetGold (- 300);
************player.SetHitPoints (+ 40);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else
*********{
************mClothingName = "Mail Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 500 Gold " << endl;
************cout << endl;
************mGoldRequired = 500;
************player.SetGold (- 500);
************player.SetHitPoints (+ 50);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}
******break;
*********}

******case 4:
*********{
*********int roll = Random(1, 4);

******if( roll == 1 )
******{
************cout << "A monster has also come to the shop" << endl;
************cout << endl;
************cout << "He picks your pocket and gets 150 Gold and 15 Magic Points" << endl;
************cout << endl;
******
************player.SetGold (- 150);
************player.SetMagicPoints (- 15);

******}
******else
******{
************cout << "Do you want to rest" << endl;
************cout << endl;
************cout << "Rooms are 200, Chairs are 100 & Floor is 10 gold" << endl;
************cout << endl;
************cout << "1) Room, 2) Chair, 3) Floor...";

*********int rest = 1;
*********cin >> rest;
*********cout << endl;

******if (rest == 1)
*********{
************player.SetGold (- 200);***
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else if (rest == 2)
*********{
************player.SetGold (- 100);
************player.SetHitPoints (+ 20);
************player.SetMaxHitPoints ( player.getHitPoints());

*********}

******else
*********{
************player.SetGold (- 10);***
************player.SetHitPoints (+ 5);
************player.SetMaxHitPoints ( player.getHitPoints());

*********}
************cout << "You are now rested" << endl;
************cout << endl;
*********}

******break;
*********}

******case 5:

************cout << "Thank you for coming, please come again" << endl;
************leave = true;
******}
******
***}

}


xpi0t0s 26Aug2011 13:16

Re: RPG Code Issue
 
What's with all the asterisks? Makes the code very unreadable. Use spaces for indentation and code blocks when posting code here to preserve formatting.

Could you also give a screenshot (copy and paste the text, not a bitmap) of the input you give and output you get?

In case 2, what is the value of mGoldRequired just before the if?

tonydav43 26Aug2011 16:11

Re: RPG Code Issue
 
Quote:

Originally Posted by xpi0t0s (Post 86373)
What's with all the asterisks? Makes the code very unreadable. Use spaces for indentation and code blocks when posting code here to preserve formatting.

Could you also give a screenshot (copy and paste the text, not a bitmap) of the input you give and output you get?

In case 2, what is the value of mGoldRequired just before the if?

Sorry, dont know why all the * came up.

I think that is the issue, it does not know the value of mGoldRequired, but here is where it is confusing.

Take the 2 seperate cpp files

1. Player.cpp
2. Store.cpp

Now in the player code, I use spells which cost magicPoints, if I go to cast a spell, it tests to see if the player has enough magicPoints before casting the spell, if you dont have enough, then it will not let you cast that spell. Now the code in the store.cpp, is the same as the spell code, but what happens when you try to buy potions, is on the first pass it will let you buy something no matter how much gold you have, then put you into negative gold. Once you have purchased a potion, it will then not let you buy anything if you dont have enough gold. So why does it work on 1 piece of code and not the other. I have included both cpp files in my original post.

In this example, I have from the onset 5 magic points and 10 gold.

Spell:

You encountered a goblin
Prepare for battle

1) Attack, 2) Cast Spell, 3) Run Away:

Please select your spell

1) FireBall 10 Magic Points
2) Water Jet 9 Magic Points
3) Lightening Bolt 8 Magic Points
4) Poison Wrap 7 Magic Points

1

You dont have enough Magic Points

1) Attack, 2) Cast Spell, 3) Run Away:

Now potions:

Welcome to my store, what can I do for you?

1) View Prices, 2) Buy Potions, 3) Buy Armor, 4) Rest a while, 5) Leave...

2

What Potions would you like to buy?

We have 1) Belt, 2) Heal, 3) Poison, 4) Invisability...

You have purchased a Heal Potion for 100 Gold

1) View Prices, 2) Buy Potions, 3) Buy Armor, 4) Rest a while, 5) Leave...

Now if I try to buy it wont let me, as my gold is in the minus figures

xpi0t0s 26Aug2011 17:52

Re: RPG Code Issue
 
Try displaying the value, e.g.
Code:

cout << "You dont have enough gold for that (you have " << player.getGold() << " but need " << "mGoldRequired << ")" << endl;

tonydav43 26Aug2011 19:04

Re: RPG Code Issue
 
Quote:

Originally Posted by xpi0t0s (Post 86396)
Try displaying the value, e.g.
Code:

cout << "You dont have enough gold for that (you have " << player.getGold() << " but need " << "mGoldRequired << ")" << endl;

Tried that, but all what happened was it went through and allowed me to buy an item even though it was more gold than I had, and then when I tried to buy another item, it then displayed:

You dont have enough gold for that you have -90 but need 100

It seems as if it is bypassing the if statement on its first iteration, strange as it works on the spell code just fine

xpi0t0s 28Aug2011 03:20

Re: RPG Code Issue
 
What *exact* type (a) does player.getGold() return, and (b) is mGoldRequired defined as? Specifically, are either of them unsigned?

It's not bypassing the if - it's doing it, we have to find out why it's evaluating false instead of true.

pein87 28Aug2011 05:04

Re: RPG Code Issue
 
In your swicth statement for the select variable I see something interesting.

Code:

******case 3:

*********{
************cout << "What Armor would you like to buy?" << endl;
************cout << endl;
************cout << "We have 1) Leather, 2) Ring, 3) Splintered, 4) Mail...";
*********
*********int armor = 1;
*********cin >> armor;

******if (player.getGold() < mGoldRequired )

*********{
************cout << endl;
************cout << "You dont have enough gold for that" << endl;
************cout << endl;

******break ;
*********}

******if(armor == 1)
*********{
************mClothingName = "Leather Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 100 Gold "<< endl;
************cout << endl;
************mGoldRequired = 100;
************player.SetGold (- 100);
************player.SetHitPoints (+ 20);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else if(armor == 2)
*********{
************mClothingName = "Ring Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 200 Gold " << endl;
************cout << endl;
************mGoldRequired = 200;
************player.SetGold (- 200);
************player.SetHitPoints (+ 30);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}
******

******else if(armor == 3)
*********{
************mClothingName = "Splintered Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 300 Gold " << endl;
************cout << endl;
************mGoldRequired = 300;
************player.SetGold (- 300);
************player.SetHitPoints (+ 40);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}

******else
*********{
************mClothingName = "Mail Armor";
************cout << endl;
************cout << "You have purchased " << mClothingName << " for 500 Gold " << endl;
************cout << endl;
************mGoldRequired = 500;
************player.SetGold (- 500);
************player.SetHitPoints (+ 50);
************player.SetMaxHitPoints ( player.getHitPoints());
*********}
******break;
*********}

The part in specific is this

Code:

************mGoldRequired = 500;
************player.SetGold (- 500);

From what I gather your seting the price for the item and then just removing that amount if they pick that option without checking if they have enough money to do so. I think on each set you should be testing if the players coin value is greater than the price of the item. That way you only call the player.SetGold() if the player can actually afford that item and display an error if they can't.

Code:

if(player.GetGold() > mGoldRequired)
{
      player.setGold(-500);
}
else
{
      cout<<"You do not have enough gold to buy this item"<<endl;
}

checking to see if the players gold count is greater then the items cost would allow you to only allow the purchase of the item if the players gold count is greater than the gold count of the item's cost. From what I see the code doing is it is not checking on each item and instead just removes the amount even if the user doesn't have the money to buy it. I hope this helps out.


All times are GMT +5.5. The time now is 10:12.