Text Based Game help

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <iostream>
#include <string>

int name();
int race();

int main()
{
    using namespace std;
    cout<<"Welcome...Who are you again?"<<endl;
    name();
    cout<<"And your race is? Elf, Human, or Dwarf."<<endl;
    race();
    system("PAUSE");
}

int name() //begin game, insert player name
{
    using namespace std;
    char NAME[30]; // sets the limit to how long the name can be
    cin>>NAME; //player name
    cout<<"Ah, Hello, "<<NAME<<". "<<endl;
    return 0;
}



int ELF()
{
     int hp=60;
     int mp=15;
     int lvl=1;
     int exp=0;
     int mhp=60;
     int mmp=15;
     int mexp=100;
     int STR=4;
     int DEF=6;
     int SPD=7;
     int INT=7;
     int stat_points=3;
     
}

int HUMAN()
{
     int hp=50;
     int mp=10;
     int lvl=1;
     int exp=0;
     int mhp=50;
     int mmp=10;
     int mexp=100;
     int STR=5;
     int DEF=5;
     int SPD=5;
     int INT=5;
     int stat_points=3;
}

int DWARF()
{
     int hp=55;
     int mp=15;
     int lvl=1;
     int exp=0;
     int mhp=55;
     int mmp=15;
     int mexp=100;
     int STR=7;
     int DEF=6;
     int SPD=3;
     int INT=4;
     int stat_points=3;
}

int race()
{
    using namespace std;
    char RACE[10];
    int ifs=0;
    int elses=0;
    cin>>RACE;
    if(RACE=="elf")
         ELF();
    else if(RACE=="human")
         HUMAN();
    if(RACE=="dwarf")
         DWARF();
    else
    {
          cout<<"Invalid response. Please try again."<<endl;
          race();
    }
}    
    


I'm trying to get the code to take the choice of race the person takes, and activate the specific code for that input. everything else works except the race() code.
all of those variables in your dwarf, elf and human functions are local variables i.e. they will die as soon as the function has finished. You need to assign those values to something.
Maybe create a class called PlayerCharacter or something like that?
Also you've told the compiler to return an integer for those functions but you dont actually return anything from those 3 functions.

edit: on line 14 you call race(), but you dont even have a mechanism to capture the user's input (what race he or she is), and then to pass this information into your race() method.
Last edited on
Lines 85, 87, 89 you're doing a string compare, but RACE is a character array. You need to change RACE to type string, or use the C-style strcmp function.
Hello hinotaiwildfire,
Iv recently gone back to writing code for entertainment, and i was wondering if you wanted any help with your text based games.

Im far from an expert, or master, but i definitally have alot of knowledge that i could push your way, and i think it would be fun =)

What your looking for is something like this though


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//First make a universal class that every race has in common (a body, or soul, or player)

struct Body{
//List everything a player will have
int health;
int speed;
int level;
string race;
};
//struct player comes before main, as a global variable



int main(){
//First, create your player
Body Player;
....
WELCOME TO THE GAME
....
....
NOW ENTER YOUR RACE

std::string RACE;
....
....

while(std::cin >> RACE)
{
if(RACE == "dwarf")
  DWARF(&Player);
  break;
ELSE IF(RACE == "elf")
  ELF(&Player);
  break;
ELSE IF(Race == "human")
  HUMAN(&Player);
  break;
ELSE
  cout << "Please enter a valid race"
}
//End of Main


And one of your functions for the races should look like this:
(This goes beforeoutsidemain, like you had it)

//This function takes a reference to a variable of type "Body", and returns nothing
(If you adjust the values of body, through reference, then the original gets affected and you wont need to return any values.)

void ELF(Body& user)  
{
  user.health = 60;
  user.speed = 7;
  user.level = 1;
  user.race = "Elf"
}

void HUMAN(Body& user)  
{
  user.health = 50;
  user.speed = 5;
  user.level = 1;
  user.race = "Elf"
}
.....
.....

 


Anyways, if u wanna make that game into a group project, id be willing to help you out a bit =] Shoot me a private message if ur interested
You would not need the &'s on lines 30, 33 and 36.
Agreed, I was questioning myself as well, thinking if I needed to include the & or not. So it would be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(std::cin >> RACE)
{
if(RACE == "dwarf")
  DWARF(Player);
  break;
ELSE IF(RACE == "elf")
  ELF(Player);
  break;
ELSE IF(Race == "human")
  HUMAN(Player);
  break;
ELSE
  cout << "Please enter a valid race"
}
@Chillieman : Actually, this is all just for fun and a way for me to learn. I'm the type of learner that needs to do not read... what i dont know/understand, i look up. doesnt help, then i ask. But i really do appreciate the offer.
No doubt, that writing code is the BEST way to learn code. the only reason I asked, is because once I was in a project with a group, and I learned a lot.

Maybe in the future.
Topic archived. No new replies allowed.