Help with creating a game.

Started by
4 comments, last by Denisb 14 years, 9 months ago
Hi! I joined today on this site as i saw it was good c++ tutorials and people getting much help. So I wanted to give it a shot. Ok, i'm making a text based game. But i have some problems with generating the price of the sold ores. Here is the code ( i will tell more info under it ) OBS: THIS IS JUST THE GENERATING CODE IN OTHER PROJECT NOT IN MY GAME
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int RandInt(int a,int b)
{
   return a + rand() % (b - a + 1);
}

void main()
{
   //Seed rand() with current time
   srand(unsigned(time(NULL)));
   //x will be between 2 and 10, inclusive
   int x = RandInt(2,10);
   int m = RandInt(5, 7);
   int n = RandInt(11, 15);
   int Ores = (n);

  //cout << "Ores mined: " <<m << endl;
  //cout << "Ores mined: " <<x << endl;
   cout << "Ores mined: " <<n << endl;
  

   cout << "You sell the ores and you get gold." << endl;
   if(
   cout << "Gold Given: " << endl;
    system("pause");
}

Okey so you see that i have made a generating code right. And it work flawless. But now i want the ores to sell and that i get money(gold). So i want that this code.
 int Ores = (n); 
get the number that was generated for this line
cout << "Ores mined: " <<n << endl;
and take that * 0.50 So basically i this is how i want it. Game generates number. Number goes into
 cout << "Ores mined: " <<n << endl;
and into
 int Ores = (n);
And then
 int Ores = (n);
takes that number and multiple's by 4 So we say i get a random number. Let's say 11. 11 * 4 = 44. And then i get 44 gold. Is it any way to do that? If you dont understand please tell me. ( PS I learned c++ around one week ago. ( I was training and training about 3 - 4 weeks. but i never learned this ) Thanks Here's my game code! PS: Some of the code is from a friend of mine. ( He did some of it but then i took over as i did not understand at that point how to code :) )
#include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main()
{
    int potion;
    int staff;
    int gold = 100;
    int armour;
    int sword;
    int pick;
    int goldneeded;
    int battlepick;
    int fight;
    int Dragon = 100;
    int Dragonremain;
	srand(time(NULL));


    while(true)
    {
         srand(static_cast<unsigned int>(time(0)));
      
        system("CLS");

    cout << "You come apon a shop" << endl;
    cout << "1) Potions 100 gold" << endl;
    cout << "2) Staffs 150 gold " << endl;
    cout << "3) Armour 500 gold " << endl;
    cout << "4) Swords 250 gold " << endl;
    cout << "5) Go mine Ores for cash" << endl;
    cout << "6) Fight" << endl;
    cin >> pick;
// HERE I STARTED TO CODE!
	if(pick == 1)
	{
		if(gold<100)
		{
			cout << " You don't have enough gold to buy a Potion!\n " << endl;
				goldneeded = gold = 100;
				cout << " You need 100+ gold ! " << endl;

				system("pause");

		}

		if(gold >= 100)
		{
			cout << "You just bought a potion for 100 gold." << endl;
			gold = gold - 100;
			cout << "Gold Left: " << gold << endl;

			system("pause");
		}

	}

	if(pick = 2)
	{
		if(gold < 150)
		{
			cout << "You dont have enough gold to buy a staff!" << endl;
			goldneeded = gold = 150;
			cout << "You need 150 gold to buy a staff!" << endl;
		
				system("pause");
		}

		if(gold >= 150)
		{
			cout << "You bought a staff for 150 gold!" << endl;

				gold = gold - 150;
				cout << "Gold left: " << gold << endl;
				system("pause");
		}

		if(pick = 3)
		{
			if(gold < 500)
			{
				cout << "You dont have enough gold to buy armour!" << endl;
				goldneeded = gold = 500;
				cout << "You need 500 gold to buy armour!" << endl;

					system("pause");
			}

			if(gold >= 500)
			{
				cout << "You have bought armour for 500 gold!" << endl;

					gold = gold - 500;
					cout << "Gold left: " << gold << endl;
					system("pause");
			}

			if(pick = 4)
			{
				if(gold < 250)
				{
					cout << "You dont have enough gold to buy a sword!" << endl;
					goldneeded = gold = 500;
					cout << "You need 500 gold to buy a sword!" << endl;
						system("pause");
				}

				if(gold >= 250)
				{
					cout << "You bought a sword for 250 gold!" << endl;
					gold = gold - 500;
					cout << "Gold left: " << gold << endl;
					system("pause");
				}

				if(pick = 5)
				{ // HERE I HAVE NOT STARTED TO CODE AS I NEED TO MAKE A GENERATING CODE FOR THE ORES


	
Advertisement
You've already solved the problem, you're just thinking too hard about how to translate that solution into code. If Ores holds the number of ores you generated, then you can compute amount of money ("gold") using your formula (4 * Ores) and store that in a separate variable repenting the gold:
int gold = 4 * Ores;


To integrate that into the rest of your game, since you already have a variable for holding the player's gold, you can just do
gold += 4 * Ores;

at the appropriate point. Do note that the += operator means, essentially, the same thing as gold = gold + (4 * Ores) here.

I suggest you take a look at a book called "Thinking in C++." Google for it -- it is freely available online and is generally considered a good introduction to the language.

There are some other things to note about your code as you've presented it so far that are not directly related to your question but will help you improve:

  1. You should include <cstdlib>, not <stdlib.h> (the former is more idiomatic C++)

  2. You declare all your variables up front, which is serviceable. But you don't initialize all of them, and you should generally always do that.

  3. You should consider packaging up all those variables related to the players into a struct representing a player's statistics and inventory, et cetera.

  4. You only need to call srand() once, at the start. You should not call it every time through your main loop as the frequency with which you reseed relative to the resolution of the timer you're using will actually cause your 'random' numbers to be extremely predictable.

  5. Consider using a switch statement instead of a series of if statements. Also consider breaking out each of the bits of code that handle the player's choice into their own function.

  6. void main() should be int main().

  7. == is the equality operator. = is the assignment operator. Code like if( pick = 5 )... doesn't do what you think it does.

  8. Try to be consistent with the capitalization of your identifier names. Use readable identifier names, not "x" and "n."



Some of the above suggestions may not make immediate sense to you, but keep them in mind as they will start to make sense when you reach the appropriate point in your education concerning the C++ language (in particular the notes about structs and functions).

Looking further, the logic of your game is also quite hard-coded (magic numbers, such as price points, are embedded into the code itself). This is generally considered a bad thing, although it is expected for a beginners programs to look like this for some time. As a bonus challenge question, think about how you might be able to reduce the amount of hard-coded numbers in the program.
Thanks for the help. But there is one problem.

I mean I dont understand so well of what you wrote.

Quote: You've already solved the problem, you're just thinking too hard about how to translate that solution into code. If Ores holds the number of ores you generated, then you can compute amount of money ("gold") using your formula (4 * Ores) and store that in a separate variable repenting the gold:



int gold = 4 * Ores;



To integrate that into the rest of your game, since you already have a variable for holding the player's gold, you can just do



gold += 4 * Ores;



( im talking about the bold text now )

You wrote that i have a variable for holding the players gold. Yes thats true. but not one for the ores. That is my problem.

This i dont know if work.

int Ores = (n);

Will it work?

EDIT: Oh i fixed it ! Thanks !!!!!

[Edited by - Denisb on July 21, 2009 10:37:04 AM]
Just recently I released a video tutorial on generating random numbers in C++ found here: http://www.marek-knows.com/downloadSection.php?Topic=Cpp&pg=1#Cpp19

Have a look at the video to see why you should not use the mod (%) operator to create a random number.
I'd also recommend you use the switch statement for some of this stuff. And also use functions to create the gameflow instead of making it all in the main function.

I'd recommend splitting all the functions by part of the game so its even easier to work with. Like if you're in a cave somewhere during act 1 or something for example, put it in a file called act1caveA.cpp.

Also have all the functions return the result to main and have main keep track of what should come next. If each function calls other functions, it may overflow the stack frame which is what holds called functions and in a game like this, if they don't return to main after each call there can be MANY functions. Also it'll be easier to save progress since you just store the gamedata that main is keeping track of in a file and load it and continue from there.
Quote:Original post by ill
I'd also recommend you use the switch statement for some of this stuff. And also use functions to create the gameflow instead of making it all in the main function.

I'd recommend splitting all the functions by part of the game so its even easier to work with. Like if you're in a cave somewhere during act 1 or something for example, put it in a file called act1caveA.cpp.

Also have all the functions return the result to main and have main keep track of what should come next. If each function calls other functions, it may overflow the stack frame which is what holds called functions and in a game like this, if they don't return to main after each call there can be MANY functions. Also it'll be easier to save progress since you just store the gamedata that main is keeping track of in a file and load it and continue from there.


I keep it in mind thanks.



Quote:Just recently I released a video tutorial on generating random numbers in C++ found here: http://www.marek-knows.com/downloadSection.php?Topic=Cpp&pg=1#Cpp19

Have a look at the video to see why you should not use the mod (%) operator to create a random number.


Yea nice adverstating. I mean to get that file you need to register...

This topic is closed to new replies.

Advertisement