Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Project 3

Here are some projects of actual programs you can practice making.

Re: Project 3

Postby InfiniteLoop on Fri May 01, 2009 8:38 pm

Alright, sorry for double post, but thought this would be easier.

So, I used the code for level * 10 and so forth to get a random number, but every time I run the program, the damage that the player does and the monster does are the same.

For example, I run it and it says:(making up numbers, just an example)

Player does 12 damage.
Monster does 5 damage.
Player does 10 damage.
Monster does 8 damage.

and so forth until one dies. Then I run the program and fight again and get this:

Player does 12 damage.
Monster does 5 damage.
Player does 10 damage.
Monster does 8 damage.

Its the same exact every time. Why is it this way?(Edit: it seems any random number is the same every time I run the program)

Also, after it asks your name in the main function, I put a for loop, and then ended it right before the main function ends. When I do the fight, it runs, then when the menu comes back up again, I try to do fight again, but when I type 1 for fight, it just brings the menu again. The shop and use items both work fine.

Here is my code:
I'm obviously no expert so my code might look messy.

Code: Select all
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int GetRand(int low, int high)
{
    return (int)(rand() / (RAND_MAX + 1.0) * (high - low) + low);
}


class player
{
private:
   int health;
   int pdamage;
   int mdamage;
   int healthLoss;
   int goldDrop;
   int monsterHealth;
   int exp;

public:
   int level;
   int smallPotions;
   int largePotions;
   int gold;

   player():
     health(100), gold(500), monsterHealth(50), smallPotions(0), largePotions(0), level(1)
     {
     }

   void useSmallPotion()
     {
        cout << "You use a Small Healing Potion.\n";
        health += 25;
        cout << "Your health is now " << health << "!\n";
        smallPotions--;
     }
    void useLargePotion()
    {
       cout << "You use a Large Healing Potion.\n";
       health += 50;
       cout << "Your health is now " << health << "!\n";
       largePotions--;
    }
    void attack()
    {
       while(monsterHealth > 0 && health > 0)
       {
       pdamage = GetRand((level * 10) - 10, (level * 10) + 10);
       cout << "You attack the Forest Bear for " << pdamage << " health.\n";
       monsterHealth -= pdamage;
       Sleep(1000);
       mdamage = GetRand((level * 10) - 15, (level * 10) + 5);
       cout << "Forest Bear bites you for " << mdamage << " health!\n";
       health -= mdamage;
       Sleep(1000);
        if(monsterHealth <= 0)
       {
          cout << "Forest Bear dies.\n";
          Sleep(1000);
          goldDrop = GetRand(0, 50);
          cout << "You loot the bear and find " << goldDrop << " gold!\n";
          gold += goldDrop;
          Sleep(1000);
          cout << "You now have " << health << " health and " << gold << " gold.\n";
       }
        if(health <= 0)
       {
          cout << "You have died.\n";
          Sleep(1000);
       }
       }
    }
};

int main()
{
   int a;
   string name;
   player one;

   cout << "Hello and welcome to Combat v1.0\n";
   Sleep(1000);
   cout << "May I ask, what is your name?\n";
   cin >> name;
   cout << "Hello " << name << "!\n";
   
   for(;;)
   {
   cout << "What would you like to do?\n";
   cout << "1. Fight\n" << "2. Shop\n" << "3. Use Items\n";
   
   cin >> a;

   switch(a)
   {
case 1:
   {
      one.attack();
      break;
   }
case 2:
   {
      cout << "Welcome to A Warrior's Supply Shop\n\n";
      cout << "Would you like to buy:\n";
      cout << "1. Small Healing Potion (for 50 gold)\n";
      cout << "2. Large Healing Potion (for 100 gold)\n";
      cin >> a;

      switch(a)
      {
      case 1:
         {
         one.gold -= 50;
         one.smallPotions += 1;
            Sleep(1000);
         cout << "You now have " << one.gold << " gold left.";
         break;
         }
      case 2:
         {
         one.gold -= 100;
         one.largePotions += 1;
         Sleep(1000);
         cout << "You now have " << one.gold << " gold left.";
         break;
         }
      }

      break;
   }
case 3:
   {
      cout << "Which item would you like to use?\n\n";
      cout << "1. Small Healing Potion (You own " << one.smallPotions << ")\n";
      cout << "2. Large Healing Potion (You own " << one.largePotions << ")\n";
      cin >> a;
      switch(a)
      {
      case 1:
         {
         if(one.smallPotions > 0)
         {
            one.useSmallPotion();
         }
         else
         {
            cout << "Sorry, you do not have any left.";
         }

         break;
         
         }
      case 2:
         {
         if(one.largePotions > 0)
         {
            one.useLargePotion();
         }
         else
         {
            cout << "Sorry, you do not have any left.";
         }

         break;

         }
      }

   }
   }
   }

   return 0;
}
InfiniteLoop
 
Posts: 11
Joined: Sun Apr 26, 2009 1:03 pm

Re: Project 3

Postby antiRTFM on Sun May 03, 2009 1:21 am

follow these instructions to get good random numbers
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Project 3

Postby InfiniteLoop on Sun May 03, 2009 4:53 pm

Ok thanks. I'm close to finishing actually. Just a few more touchups.

Btw....setting a value for each variable is very important, as you said in that video.

I did a thing that says your exp after you kill a monster and it says "You now have -87564357 experience!"

Thanks for the help with the randomize though.
InfiniteLoop
 
Posts: 11
Joined: Sun Apr 26, 2009 1:03 pm

Re: Project 3

Postby mjd550 on Thu May 07, 2009 12:09 pm

Hi all. This is the biggest project I've done, its similar to the combat simulator but boxing. You train before each fight.

http://rapidshare.com/files/230267013/Boxer.rar.html

I want to make a d&d style game but trying to get my head round the idea. It's made in Gwin, this is a simple graphics library I got from my University which allows you to use images, change text colour and other simple things.

Please give me your feedback, I know its not that good !
mjd550
 
Posts: 9
Joined: Thu May 07, 2009 11:33 am

Re: Project 3

Postby InfiniteLoop on Sun May 10, 2009 10:31 pm

Not bad mjd550.

I liked it. I only tried it once, but I noticed something you could easily change.

Now I didn't look at your code because this computer doesn't have a compiler, but lets just say the variable for user's health is just called health.

Then add this code.

Code: Select all
if(health < 0)
{
        health = 0;
}




That way, if your health goes below 0, it will just stay at 0. It said I died and it showed that my health was -1.
InfiniteLoop
 
Posts: 11
Joined: Sun Apr 26, 2009 1:03 pm

Version 1.0

Postby Adamp on Fri Jun 26, 2009 9:56 pm

Here is my program, it took a while but I finished it. It probably isn't the best use of OOP as I used a few global variables and the combat system didn't exactly work the way I wanted it, but it served its purpose. The point is learning OOP.

My code is attached


Any comments or criticism accepted.
Attachments
ANS Project 3.zip
(4.51 MiB) Downloaded 21 times
Adamp
 
Posts: 14
Joined: Mon Jun 22, 2009 2:43 pm

Re: Version 1.0

Postby C++ on Sat Jun 27, 2009 1:42 pm

Adamp wrote:Here is my program, it took a while but I finished it. It probably isn't the best use of OOP as I used a few global variables and the combat system didn't exactly work the way I wanted it, but it served its purpose. The point is learning OOP.

My code is attached


Any comments or criticism accepted.


You learn by good reference material and interactions with good C++ programmers. Rome wasn't built in one day either :wink:
You did a good job!

Things to improve:

1) The combat system;
2) Preventing the user from providing bad input;
3) Try to refrain from including the entire std namespace, especially in header files. See here as to why.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Project 3

Postby Kaijethegreat on Mon Jul 06, 2009 6:32 am

I'm over half way through my RPG, just got a few things to ask before i do things the hard way. I have a class for items and i have like 20 items all with (ID, name, cost, etc), i know how to generate random numbers and each of my items have different ID's so i generate a random number from 1-20 and i want the number to refer to the item by the ID. Once the program finds a matching ID from the random number i want that items name and i want the item to go in my inventory, how would i do that?

and whats a good way to do an inventory? arrays for slots?
Kaijethegreat
 
Posts: 5
Joined: Mon Jul 06, 2009 6:23 am

Re: Project 3

Postby antiRTFM on Mon Jul 06, 2009 7:31 pm

containers containers containers

specifically std::vector. would help with both those questions i think

video about this one day... meanwhile try looking it up
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Project 3

Postby Kaijethegreat on Sat Jul 11, 2009 11:34 am

some of my functions are ending up really long, e.g.

void stats(string & retname, short & rethp, short & retdmg, short & retstr, short & retxp, short & retlvl, short & retgold, string & retclass);

should i really be setting it out like this? how else can i manage to get all the stats of my player in another function without this big line of references or pointers.. -.-
Kaijethegreat
 
Posts: 5
Joined: Mon Jul 06, 2009 6:23 am

Re: Project 3

Postby antiRTFM on Sun Jul 12, 2009 12:19 am

sometimes it comes down to just that...

but maybe you can divide this function tasks into several smaller functions, especially if this function is getting pretty long

Or, maybe you'd like to pass in one object that contains all of these variables
like- if these are all variables of an "Ogre" object, maybe you'd like to just pass in an Ogre by reference
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Project 3

Postby Kaijethegreat on Mon Jul 13, 2009 4:42 pm

thanks anti, i forgot about using my own classes as pointers or references, my function is much more cleaner now,

stats(Player & p);

:D

i made a function so i can change my private variables in my function,

void Player::hpchange(short num)
{
hp = hp + num;
}

i use it for leveling up and having different stats on my warrior/mages and stuff. but im wondering why make the variables private? if i had them public i could just change them normally instead of making this function. on your videos im sure you said its best to have the variables private, but why?
Kaijethegreat
 
Posts: 5
Joined: Mon Jul 06, 2009 6:23 am

Re: Project 3

Postby antiRTFM on Tue Jul 14, 2009 10:00 pm

While it may not be clear when making these little classes we're playing around with now, it gets more and more obvious with larger real projects that it is extremely useful to "let everyone have their own space" and not allowing anything/anywhere to access and change anything/anwhere else. This is an essential part of OOP. In your case for example, A player is a player and takes care of itslef internally. No one has permission to just dig into a player and mess around with its stuff, that would make things unbarably confusing and hard to keep track of. Need to operate with a player? Use the player's member functions which serve as an interface to its internals.

You dont manually turn a car's engine to get the car rolling. You use the car's interface (gas pedal) and let the engine take care of the rest internally.

A GPU doesnt control plasma cells of a computer screen, rather it sends signals through the LCD's interface; the VGA plug. The LCD internally uses these signals to mess with its own private stuff.

You don't rub flintstones or generate electricity sparks to light a fire on your stove, just use the interface (turn the stove knob) and it works its magic.


This way, everyone's responsabilities is abstracted for themselves. There is a clear idea about who is in charge of what and who isnt. Everyone does their own job and leaves the rest up to the correct department to take care of.

You wouldnt want to know what life was like back in the day when everyone/everything could access everyone/everything else... just know that ppl have been there done that and said "zomg, lets invent OOP"
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Project 3

Postby Kaijethegreat on Fri Jul 24, 2009 9:13 pm

http://rapidshare.com/files/259708767/Main.zip.html

it's done in visual studio 2008

theres not many projects on here so i'll give a link to my project up to now, give me any ideas for improvements or anything that i've done wrong.
Its not finished and i havn't worked on it much, might start it again to clean it up and make it better. You can choose classes like warrior, mage. choose which attack to use, like str, attack and magic. Theres only 6 monsters because i can't be bothered... but when you enter a fight it gives you a random monster to fight each time.

i havn't really played it much but i think my games unbalanced and too easy :P plus the monsters don't get harder the higher level you are (yet)..
Kaijethegreat
 
Posts: 5
Joined: Mon Jul 06, 2009 6:23 am

Re: Project 3

Postby Streamfarm on Mon Oct 05, 2009 5:47 am

Hi, this is my first post on this forum. I've been following the videos on YouTube and they really helped me a lot with learning C++. I decided to take on this challenge, and I'm quite pleased with the result. It only has 2 monsters (but it is very easy to add more) and I never got around to making several different player attacks (because it seemed trivial). At the moment I'm trying to move on to programming Windows application, and eventually real games. I got a lot of help and inspiration from the other solutions in this thread, so if a part of the code looks like yours, it probably is :).

Anyway, feel free to comment on my game, I'm looking forward to hearing what you think.

http://rapidshare.com/files/289533093/RPGv1.zip.htm

Oh, and this is my first real project, so I'm sorry about the messy/perhaps buggy code. I'll make more comments in the future, I swear!

EDIT: I uploaded a fixed version.
Last edited by Streamfarm on Tue Oct 06, 2009 3:44 pm, edited 1 time in total.
Streamfarm
 
Posts: 2
Joined: Mon Oct 05, 2009 5:39 am

PreviousNext

Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests