View previous topic :: View next topic |
Author |
Message |
educofu Expert Cheater
Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Fri Oct 22, 2010 12:31 pm Post subject: [C++] TB-RPG > The Best RPG! |
|
|
This is my first program in C++. Since i learned to program with auto assembly, my code structure rly looks like a assembly program. (improved that^^)
heres is the code:
The Best RPG v2!
v2.2
Code: |
#include <iostream>
#include <cstdlib>
using namespace std;
void clean_screen ()
{system("CLS");}
int random (int a,int b)
{
if (b<a) return 0;
int good;
random:
int r=rand();
if (r>=a) good=1;
else goto random;
if (r<=b) good+=1;
else goto random;
if (good==2)return r;
else return 0;
}
void show_stats ();
char* mob_name (int a);
int mob_level (int a);
int mob_tlife (int a);
void char_choose();
void main_screen ();
void reward ();
void char_opt ();
void count (int a);
void lvlup ();
void hunt ();
void city ();
void fight ();
int end();
void dead ();
void npcs ();
void blacksmith ();
void mayor ();
void quest ();
void iventary ();
void trade();
//char
int classe;
int level = 1;
float xp = 0;
float xpnext = 100;
float gold = 0;
float tlife;
float clife;
float tmana;
float cmana;
float min_dmg_f;
float max_dmg_f;
int min_dmg_i;
int max_dmg_i;
float dmg_bonus=0;
float armor;
float armor_bonus=0;
int agi=0;
int agi_bonus=0;
int str=0;
int str_bonus=0;
int wis=0;
int wis_bonus=0;
int stats_points=5;
int critical;
int datack;
char* inv_list=" ";
//mob
int mlvl;
float mtlife;
float mclife;
float mdmg;
char* mobname;
int mmin_dmg;
int mmax_dmg;
//other
int action;
char* sell_list=" ";
int main ()
{
//frescuras
system("mode con cols=100");
system("mode con lines=20");
system("color 70");
system("title The Best RPG Ever!");
//welcome
cout << "Welcome to The Best RPG Ever!\n\n";
char_choose ();
}
void char_choose()
{
cout << "\nChose a class you want to play with:\n";
cout << "\n1 - Raider; 2 - Warrior; 3 - Magician\n";
cin >> action;
if (action==1) {
clean_screen();
cout << "Raider: Long ranged or Melee, high atack, low defense. Continue?\n1 - Yes\n2 - No\n";
cin >> action;
if (action!=1) char_choose ();
classe = 1;
}
else if (action==2) {
clean_screen();
cout << "Warrior: Melee, medium atack, high defense. Continue?\n1 - Yes\n2 - No\n";
cin >> action;
if (action!=1) char_choose();
classe = 2;
}
else if (action==3) {
clean_screen();
cout << "Wizard: Ranged, low atack, low defense, high skills. Continue?\n1 - Yes\n2 - No\n";
cin >> action;
if (action!=1) char_choose();
classe = 3;
}
else char_choose();
count (1);
}
void main_screen ()
{
clean_screen ();
if (xp>=level*60) lvlup ();
cout << "Action: 0 - Exit; 1 - Character Options; 2 - Hunt; 3 - City\n";
cin >> action;
if (action==0) end ();
else if (action==1) char_opt ();
else if (action==2) hunt ();
else if (action==3) city ();
else if (action==4) lvlup();
else main_screen ();
}
//calculate player stats; if entry = 1 goto mainscreen, else goto stats.
void count (int a)
{
tlife=100+(str+str_bonus)*10+(agi+agi_bonus)*2;
tlife+=(level-1)*10;
if (classe==2) tlife*=2;
else if (classe==3) tlife*=0.8;
clife=tlife;
tmana=100+(wis+wis_bonus)*10;
tmana=tmana+(level-1)*8;
if (classe==2) tmana=tmana*0.8;
else if (classe==3) tmana=tmana*1.5;
cmana=tmana;
max_dmg_f=20+str*5+agi*2+level*5;
min_dmg_f=10+str*2+agi*5+level*4;
if (classe==1) {max_dmg_f*=1.5;min_dmg_f*=1.2;}
if (classe==3) {max_dmg_f*=0.8;min_dmg_f*=0.8;}
if (min_dmg_f>max_dmg_f) max_dmg_f=min_dmg_f;
max_dmg_i=(int)max_dmg_f;
min_dmg_i=(int)min_dmg_f;
armor=(agi*8+str)/10;
if (classe==2) armor*=3;
datack = (agi+level)/2;
if (classe==1) datack*=2;
critical = (agi*2+level)/2;
if (classe==1) critical*=2;
if (a==1) main_screen ();
else show_stats ();
}
void lvlup ()
{
clean_screen ();
level++;
xpnext = level*50;
xp=level*50-xp;
stats_points+=5;
count (0);
}
void char_opt ()
{
clean_screen ();
cout << "Action: 0 - Back; 1 - Stats 2 - Iventory\n";
cin >> action;
if (action==0) main_screen();
else if (action==1) show_stats();
else if (action==2) iventary ();
else char_opt ();
}
void show_stats ()
{
clean_screen ();
cout << "Your current stats:\n";
cout << "\nLevel " << level;
cout << "\n\nHP: " << clife << "/" << tlife << "\t MP: " << cmana << "/" << tmana;
cout << "\n\nAgi\t=\t" << agi << " + " << agi_bonus << "\n";
cout << "Str\t=\t" << str << " + " << str_bonus << "\n";
cout << "Wis\t=\t" << wis << " + " << wis_bonus;
if (stats_points >=1 ) cout << "\n\nRemaining bonus stats = " << stats_points;
cout << "\n\nArmor = " << armor << " + " << armor_bonus;
cout << "\nDamage = " << min_dmg_i << "-" << max_dmg_i << " + " << dmg_bonus;
cout << "\nXP = " << xp << "/" << xpnext;
cout << "\nChance to Critical Hit:" << critical << "%";
cout << "\nChance to Double Atack:" << datack << "%";
cout << "\n\nAction: 0 - Back";
if (stats_points >=1 ) cout << "; 1 - Add Agility; 2 - Add Strentgh; 3 - Add Wisdown.";
cout << "\n";
cin >> action;
if (action==0) char_opt ();
else if (action==1) {if (stats_points >= 1) {agi++; stats_points--; count (0);} else show_stats();}
else if (action==2) {if (stats_points >= 1) {str++; stats_points--; count (0);} else show_stats();}
else if (action==3) {if (stats_points >= 1) {wis++; stats_points--; count (0);} else show_stats();}
else show_stats ();
}
void iventary()
{
clean_screen();
cout << "Your itens:\t\t\tEquiped:\n";
cout << inv_list;
cout << "\n\nAction:\n\n0 - Back";
cin >> action;
if (action==0) char_opt();
else iventary();
}
void hunt ()
{
clean_screen ();
cout << "Chose mob dificult:\n";
cin >> action;
int rnd_mob=random(action-1,action+1);
mobname = mob_name (rnd_mob);
mlvl = mob_level (rnd_mob);
mtlife = mob_tlife (rnd_mob);
mdmg = mlvl*20;
mclife = mtlife;
mmin_dmg = (int)mdmg-random(0,20)*mlvl;
mmax_dmg = (int)mdmg+random(0,20)*mlvl;
if (mlvl==0) { cout << "You have found any mob.\n\nEnter 0 to continue."; cin >> action; main_screen ();}
else cout << "You have faced a " << mobname << ".\nDo you want:\n0 - Run! \n1 - Fight!\n";
cin >> action;
if (action!=1) main_screen ();
else fight ();
}
void fight ()
{
float dmg = random(min_dmg_i,max_dmg_i)+dmg_bonus-mlvl*3;
float mob_dmg = random(mmin_dmg,mmax_dmg)-armor/mlvl/level;
clean_screen();
atack:
if (clife<=0) dead ();
if (mclife<=0) reward ();
cout << "Your turn:\n";
cout << "\nHP: " << clife << "/" << tlife << "\t MP: " << cmana << "/" << tmana;
cout << "\n" << mobname << " HP: " << mclife << "/" << mtlife;
cout << "\nAction:\n\n0 - Atack\n1 - Skills\n2 - Itens\n";
cin >> action;
if (action==0)
{
clean_screen ();
if (random(0,100)<=critical-mlvl*2) { dmg*=2; cout << "CRITICAL HIT!\n";}
mclife-=dmg;
cout << "You have dealt " << dmg << " damage to " << mobname << ".\nEnter 0 to continue.";
cin >> action;
if (random(0,100)<=datack-mlvl*2) {clean_screen(); cout << "DOUBLE ATACK!\n"; goto atack;}
}
if (mclife<=0) reward ();
clife-=mdmg-armor;
clean_screen ();
cout << "Mob turn." << "\nHe dealt " << mob_dmg << " to you.\n" << ".\nEnter 0 to continue.";
cin >> action;
fight ();
}
void city ()
{
clean_screen();
clife=tlife;
cmana=tmana;
cout << "You're now in your home city. Life and MP recovered\n\nAction:\n\n0 - Back\n1 - NPC's\n2 - Quest\n";
cin >> action;
if (action==0) main_screen ();
else if (action==1) npcs ();
else if (action==2) quest ();
}
void npcs ()
{
clean_screen();
cout << "NPC's:\n\n0 - Back\n1 - Blacksmith\n2 - City mayor\n";
cin >> action;
if (action==0) city ();
else if (action==1) blacksmith ();
else if (action==2) mayor ();
else npcs ();
}
void blacksmith ()
{
clean_screen ();
cout << "Hello there, what can i do for you?\n\nAction:\n\n0 - Back\n1 - Trade\n";
cin >> action;
if (action==0) npcs ();
else if (action==1) trade();
else blacksmith ();
}
void trade()
{
clean_screen();
cout << "Equipment:\t\t\tPrice:\n\n";
cout << sell_list;
cout << "\nAction:\n\n0 - Back";
cin >> action;
if (action==0) blacksmith();
else trade();
}
void mayor ()
{
clean_screen ();
cout << "Who are you?\n\nAction:\n\n0 - Back";
cin >> action;
if (action==0) npcs();
else mayor ();
}
void quest ()
{
clean_screen ();
cout << "Any by now.\n\nAction:\n\n0 - Back";
cin >> action;
if (action==0) city ();
else quest ();
}
void reward ()
{
clean_screen ();
float dificult = mlvl/level;
int chance = random(0,100);
int new_xp = random (mlvl*20,mlvl*30);
int new_gold = random (mlvl*5,mlvl*40);
xp+=new_xp;
gold+=new_gold;
cout << "You have killed " << mobname << ".\n";
cout << "Fight dificult: " << dificult;
cout << "\n\nXP + " << new_xp;
cout << "\nGold + " << new_gold;
if (chance <= 30*dificult) cout << "\n\nYou have found a iten.";
cout << "\n\nEnter 0 to continue.";
cin >> action;
main_screen ();
}
void dead()
{
clean_screen ();
cout << "You have died, and you loose " << level*30 <<" XP and " << level*50 << " Gold. You will be ressurected with half of your life\n\n";
xp -=level*30;
if (xp<0) xp = 0;
gold -= level*50;
if (gold<0) gold = 0;
clife = tlife/2;
cout << "Enter 0 to continue.";
cin >> action;
main_screen();
}
int end()
{
clean_screen ();
cout << "Do you realy want to exit?\n0 - No\n1 - Yes\n";
cin >> action;
if (action!=1) main_screen ();
else return 0;
}
//MOB DATABASE
char* mob_name (int a){
if (a==0) return "any mob";
else if (a==1) return "Slime";
else if (a==2) return "Hawk";
else if (a==3) return "Turtle";
else if (a==4) return "Snake";
else if (a==5) return "Giant Spider";
else if (a==6) return "Mad Dog";
else if (a==7) return "Wolf";
else if (a==8) return "Bronze Wolf";
else if (a==9) return "Silver Wolf";
else if (a==10) return "Gold Wolf";
else return "any mob";
}
int mob_level (int a){
if (a==0) return 0;
else if (a==1) return 1;
else if (a==2) return 2;
else if (a==3) return 3;
else if (a==4) return 4;
else if (a==5) return 5;
else if (a==6) return 6;
else if (a==7) return 7;
else if (a==8) return 8;
else if (a==9) return 9;
else if (a==10) return 10;
else return 0;
}
int mob_tlife (int a){
if (a==0) return 0;
else if (a==1) return 100;
else if (a==2) return 150;
else if (a==3) return 190;
else if (a==4) return 250;
else if (a==5) return 320;
else if (a==6) return 400;
else if (a==7) return 480;
else if (a==8) return 600;
else if (a==9) return 800;
else if (a==10) return 1000;
else return 0;
}
|
the binary v1: Download
the binary v2: Download
the binary v2.1:Download
the binary v2.2:Download
Older codes:Download
please give me ideas to improve the best rpg! and my code writing too...
_________________
"I finally started thinking outside of the box, only to find myself in a larger box."
Last edited by educofu on Wed Nov 03, 2010 2:19 pm; edited 6 times in total |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8579 Location: 127.0.0.1
|
Posted: Fri Oct 22, 2010 4:07 pm Post subject: |
|
|
- Learn some sort of graphics rendering.
- Add visuals.
- Remove the label 'best' since a bias opinion does not label your "game".
- Learn how to avoid goto.
_________________
- Retired. |
|
Back to top |
|
 |
FETT How do I cheat?
Reputation: 0
Joined: 11 Sep 2010 Posts: 5
|
Posted: Sun Oct 24, 2010 12:29 am Post subject: |
|
|
Looks good. You seem to understand the basic grammar well. The only thing I have to say though is why not make your each block of your program into functions. Each related action should be in its own seperate function. This way you can avoid the goto();. By making loops and conditions that include recrusive function calls.... It makes your code look more professionl.
Code: |
int main(){
displaymessage();
function1(choice);
return 0;}
|
Thats all your main should really look like.
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
|
Back to top |
|
 |
educofu Expert Cheater
Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Mon Oct 25, 2010 11:55 am Post subject: |
|
|
i rly cant understand functions... can some 1 give a example with my code? maybe after understanding it i may find some sense in Jani's image...
_________________
"I finally started thinking outside of the box, only to find myself in a larger box." |
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Oct 27, 2010 10:20 am Post subject: |
|
|
educofu wrote: | i rly cant understand functions... can some 1 give a example with my code? | Functions aren't very complex.. Code: | // Function prototype
int avg(int a, int b);
int main(int argc, char *argv[])
{
int a = 4, b = 6;
int c = 0;
c = avg(4, 6);
// c equals to 5 now.
return 0;
}
// Full function declaration
int avg(int a, int b)
{
return (a+b)/2;
} |
educofu wrote: | maybe after understanding it i may find some sense in Jani's image... | The point of the comic is that using goto will cause the code to jump to an unknown environment where anything can happen = hard to follow -> it's bad practice. Google for "goto bad practice" to get more detailed answers. Don't get the replies wrong, goto is totally valid keyword and all, but it's just hard to read and manage code with gotos.
|
|
Back to top |
|
 |
educofu Expert Cheater
Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Wed Oct 27, 2010 10:57 am Post subject: |
|
|
uhm... got it... just have to start working with it!
thanks Jani.
--edit:
update to v2 alpha. add ALOT of stuff...
i know its laking somethings (working on it).
NOW i'd like you guys to test and tell me what i can improve in-game
also, do someone know how to make that "hide" (aka. spoiler) buttons like other forums?
_________________
"I finally started thinking outside of the box, only to find myself in a larger box." |
|
Back to top |
|
 |
Hurr™ How do I cheat?
Reputation: -1
Joined: 05 Apr 2010 Posts: 5 Location: Sweden.
|
Posted: Wed Oct 27, 2010 12:32 pm Post subject: |
|
|
Nice, looks good, never really got into C++...
|
|
Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Wed Oct 27, 2010 10:48 pm Post subject: |
|
|
Perhaps you can add classes/objects and format your code with proper spacing.
Last edited by nwongfeiying on Sun Oct 31, 2010 8:10 am; edited 1 time in total |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8579 Location: 127.0.0.1
|
Posted: Thu Oct 28, 2010 3:10 pm Post subject: |
|
|
You need to go back and learn the basics. You are still using goto. Not to mention you are using it for no reason at all as well since it isn't even needed in the spots you are using it.
http://www.cplusplus.com/doc/tutorial/
That's a good site to get started.
_________________
- Retired. |
|
Back to top |
|
 |
educofu Expert Cheater
Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Thu Oct 28, 2010 4:24 pm Post subject: |
|
|
@wiccaan: some goto's were there just for further use.
added v2.1: "TSIGN" : (This Sh*t Is Geting Nice!)
added alot of stuff like random things...
improved and restructured the code.
g2g now but i think this topic is kind huge, so im going to upload the older versions in ifile.it later.
_________________
"I finally started thinking outside of the box, only to find myself in a larger box." |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8579 Location: 127.0.0.1
|
Posted: Thu Oct 28, 2010 6:34 pm Post subject: |
|
|
educofu wrote: | @wiccaan: some goto's were there just for further use.
added v2.1: "TSIGN" : (This Sh*t Is Geting Nice!)
added alot of stuff like random things...
improved and restructured the code.
g2g now but i think this topic is kind huge, so im going to upload the older versions in ifile.it later. |
Goto isn't needed for repeated usage of things, you don't need it at all for this "game". As I said, you should go relearn the basics.
_________________
- Retired. |
|
Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 891
|
Posted: Thu Oct 28, 2010 9:54 pm Post subject: |
|
|
A book on C++ would be a good start. There are some available on the internet for free. You need to be thinking your algorithms through a bit, too. Your random function, for example, could be written a little better. As it is, I think it has the possibility to loop for quite a long time. If you want a value between A and B, maybe it would be better to divide your random number by B and use the remainder (modulo) as the result.
|
|
Back to top |
|
 |
educofu Expert Cheater
Reputation: 3
Joined: 21 Aug 2009 Posts: 171 Location: Brazil,MG,OP
|
Posted: Thu Oct 28, 2010 10:42 pm Post subject: |
|
|
@justa_dude
good exemple u got there... going to make my random function better next release ( i was worried with that lots of loops too ) . going to sleep now... 01:15 AM in brazil; going to work on that later xD.
edit--
also i need a example of "in/out" of files in c++ to make a save/load.
edit2--
uhm... cant figure out how to make the random function with modulo
(my function gives a random number in a range(maybe remainder+minimum?))...
not worried too much with it right now since its working fine...
add v2.2> more random things and funnier to play, some bugs fixed.
_________________
"I finally started thinking outside of the box, only to find myself in a larger box." |
|
Back to top |
|
 |
|