Text-based Game

I'm halfway into my code, and I'm stuck, I'm not sure how to finish this project. I'm currently quarantined at home and don't have anyone to ask for help. If I were to get a few pointers or tips, I would greatly appreciate it.

"The instructions for this assignment are pretty open. Follow them carefully, but be ready to solve challenges and demonstrate your own creativity.

1) You're going to need to create one player character. The user should get to name them and define at least one other attribute. The character should have at least three stats: one offense, one defense, and one that equates to a health meter of some kind. Those stats should be determined with the use of a function that generates random numbers within a given range. Make sure that values are not TOO low. For instance, you don't want a full range of 0 to 100 for initial health.

2) You're going to need to create one computer character. The name should be randomized from a vector of at least ten options. Whatever other attribute the player gets to set should be similarly randomized with a vector here. This character should have the same three stats as the player character and they should be set using the same function as the player's.

3) You will need a function that displays each character's information. After the characters are ready, run that function.

4) You will need a function that asks the user if they are ready to continue. It should use a WHILE loop to ensure that it receives valid input or re-prompts if it does not. After displaying the character information using the function from Element 3, call this function.

5) You now need to run combat. The combat should take place within a WHILE loop that runs as long both players have health left. The combat should use a combination of offensive stats and defensive stats and random numbers. How exactly that works out and how damage is taken away from health is up to you. Each round, the player should attack first. If the computer is still alive, it should attack. If the player is still alive, another round should commence.

Here's an example:

playerOffense = 8
playerDefense = 5
playerHealth = 12
computerOffense = 6
computerDefense = 7
computerHealth = 9

playerOffense + roll() = 14
computerDefense + roll() = 8

computerHealth = computerHealth - 8 = 1

The fight goes on.

6) At the end of the fight, whichever player is alive should be declared the winner. There should be accommodation for a tie or error, as well."



And here's my code that I have so far:

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
 // I'm limited to using these two libraries.
#include <iostream>
#include <vector>

int stats(int offense, int defense, int health) {

  int statPoints = rand() % 50 + 50;

  return 0;

}

void computerName() {
 
 std::vector<std::string> computerNames = {"Neloth", "Cicero", "Hamelyn", "Wylandriah", "Knjakr", "Sheogorath", "Paarthurnax", "Hadvar", "Brynjolf", "Brenuin"};;
 std::cout<<computerNames[rand() % 10];
 
}

void computerAttribute() {

  std::vector<std::string> attributes = {"Strength", "Dexterity", "Constitution", "Intelligence", "Wisdom", "Charisma"};;
  std::cout<<attributes[rand() % 10];

}

std::string user(std::string userName, std::string userAtt) {

  

}

int main() {

  srand (time(NULL));

  int userOffense;
  int userDefense;
  int userHealth;
  int computerOffense;
  int computerDefense;
  int computerHealth;
  std::string userCharacterName;
  std::string userAttribute;

  std::cout << "What will be the name of your character?\n";
  std::cin >> userCharacterName;
  std::cout << "Alright, give your character an attribute.\n" << "Examples: strength, dexterity, constitution, intelligence, wisdom, charisma\n";
  std::cin >> userAttribute;
  
  std::cout << "Your name is " << userCharacterName << " that has an attribute of " << userAttribute << ".\n";

  computerName();
  computerAttribute();

}
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
As formatted:

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
#include <iostream>
#include <vector>

int stats(int offense, int defense, int health) {
	int statPoints = rand() % 50 + 50;

	return 0;
}

void computerName() {
	srand(time(NULL));

	std::vector<std::string> computerNames = {"Neloth", "Cicero", "Hamelyn", "Wylandriah", "Knjakr", "Sheogorath", "Paarthurnax", "Hadvar", "Brynjolf", "Brenuin"};;
	std::cout << computerNames[rand() % 10];
}

void computerAttribute() {
	srand(time(NULL));

	std::vector<std::string> attributes = {"Strength", "Dexterity", "Constitution", "Intelligence", "Wisdom", "Charisma"};;
	std::cout << attributes[rand() % 10];
}

std::string user(std::string userName, std::string userAtt) {

}

int main() {
	int userOffense;
	int userDefense;
	int userHealth;
	int computerOffense;
	int computerDefense;
	int computerHealth;
	std::string userCharacterName;
	std::string userAttribute;

	std::cout << "What will be the name of your character?\n";
	std::cin >> userCharacterName;

	std::cout << "Alright, give your character an attribute.\n" << "Examples: strength, dexterity, constitution, intelligence, wisdom, charisma\n";
	std::cin >> userAttribute;

	std::cout << "Your name is " << userCharacterName << " that has an attribute of " << userAttribute << ".\n";

	computerName();
	computerAttribute();

}


Note that srand() is usually called only once at the beginning of main() as it sets the random number seed. time() requires #include <ctime> and srand()/rand() requires #include <cstdlib>

and I'm stuck


stuck at what? What the C++ question? What are trying to do that you can't?
Last edited on
I apologize that I forgot to format, but I updated my post. I'm also sorry for not specifying.
Don't go overboard on using formatting tags, please. Really don't enclose code tags (and code) inside other tags, it makes the code hard to read.

Have you learned about classes/structs yet? They would make this assignment easier.

Have you learned about the C++ random number library?

A very minimal start:
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
#include <iostream>
#include <string>
#include <random>    // http://www.cplusplus.com/reference/random/

// https://www.learncpp.com/cpp-tutorial/introduction-to-structs-members-and-member-selection/
struct Entity
{
   std::string Name;
   int         Hit_Points;
   int         Attack;
   int         Defense;
};

int main()
{
   // http://www.cplusplus.com/reference/random/default_random_engine/
   // http://www.cplusplus.com/reference/random/random_device/
   std::default_random_engine rng(std::random_device {} ());

   // http://www.cplusplus.com/reference/random/uniform_int_distribution/
   std::uniform_int_distribution<int> dis(50, 100);

   // create a player and an enemy
   Entity player;
   Entity enemy;

   std::cout << "What is your name, Bold Adventurer? ";
   std::getline(std::cin, player.Name);  // https://www.cplusplus.com/reference/string/string/getline/

   std::cout << "\nWelcome, " << player.Name << "!\n";

   // assign some random values to the player's attributes
   player.Hit_Points = dis(rng);
   player.Attack     = dis(rng);
   player.Defense    = dis(rng);

   std::cout << "You have " << player.Hit_Points << " HPs, an attack of " << player.Attack
             << " and a defense of " << player.Defense << ".\n";
}
What is your name, Bold Adventurer? Sir Robin

Welcome, Sir Robin!
You have 69 HPs, an attack of 89 and a defense of 77.
Shouldn't stats return statPoints instead of 0 ?

Inside your main you should have a while loop.
1
2
3
4
5
6
7
8
9
10
while(!gameOver)
{
  // players attacks computer
  // if(computer is !dead
  //   computer attacks player
  // else
  //   message that player won and quit game
  // if player is dead
  // show info and quit game 
}


Did you learn about structs or classes ?
To see how this kind of game works do a search for "RPG C++ Console"
Maybe you find some easy game to study.
I apologize once again and I should have mentioned it in the original post, but I'm only limited to using #include <iostream> and <vectors>.
If you are allowed to use two headers then you can't use srand/rand since they are part of <cstdlib.>
With some compilers you may get away with not explicitly including cstdlib as it could be included via another header. So not including cstdlib may work - but not recommended.
if you lack random you can cook up something that will do. Its stupid to have to do that.
is there something you are still stuck on?
I finally understand functions and managed to finish the project!

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <vector>
#include <cstdlib>

int offenseStats(int offense) {

  int offensePoints = rand() % 50 + 50;

  return offensePoints;

}

int defenseStats(int defense) {

  int defensePoints = rand() % 50 + 50;

  return defensePoints;

}

int healthStats(int health) {

  int healthPoints = rand() % 50 + 50;

  return healthPoints;

}

void computerName() {
 
 std::vector<std::string> computerNames = {"Neloth", "Cicero", "Hamelyn", "Wylandriah", "Knjakr", "Sheogorath", "Paarthurnax", "Hadvar", "Brynjolf", "Brenuin"};;
 std::cout<<computerNames[rand() % 10];
 
}

void computerAttribute() {

  std::vector<std::string> attributes = {"strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma"};;
  std::cout<<attributes[rand() % 10];

}

void characterInfo() {

  int userOffense = offenseStats(userOffense);
  int userDefense = defenseStats(userDefense);
  int userHealth = healthStats(userHealth);
  int computerOffense = offenseStats(computerOffense);
  int computerDefense = defenseStats(computerDefense);
  int computerHealth = healthStats(computerHealth);
  
  if (userHealth < computerHealth) {
    std::cout << "\nI would just give up hope now.\n";
  }

  std::cout << "\nUser offense = " << userOffense << "\n";
  std::cout << "User defense = " << userDefense << "\n";
  std::cout << "User health = " << userHealth << "\n";
  std::cout << "Computer offense = " << computerOffense << "\n";
  std::cout << "Computer defense = " << computerDefense << "\n";
  std::cout << "Computer health = " << computerHealth << "\n";


}

void userQuestion() {

  characterInfo();

  std::cout << "\nWhat would you like to do?\n"
          "  [G]ive up.\n"
          "  [C]arry on.\n";

  std::string userChoice;
  std::cin >> userChoice;

  while (userChoice != "G" && userChoice != "g" && userChoice != "C" && userChoice != "c") {

    std::cout << "Please choose C or G.";

  }

  if (userChoice == "G") {

    std::cout << "\nUnderstandable, have a good day.";
    exit(0);

  }

  else if (userChoice == "g") {

    std::cout << "\nUnderstandable, have a good day.";
    exit(0);

  }

}

int main() {

  srand (time(NULL));

  int userOffense = offenseStats(userOffense);
  int userDefense = defenseStats(userDefense);
  int userHealth = healthStats(userHealth);
  int computerOffense = offenseStats(computerOffense);
  int computerDefense = defenseStats(computerDefense);
  int computerHealth = healthStats(computerHealth);
  int userHurtHealth = (userHealth - rand() % 30);
  int userHurtDefense = (userDefense - rand() % 10);
  int userHurtOffense = (userOffense - rand() % 10);
  std::string userCharacterName;
  std::string userAttribute;

  std::string mystr;
  std::cout << "Hello, Player, what is your name?\n";
  getline (std::cin, mystr);
  std::cout << "\nHello, " << mystr << ".\n";
  std::cout << "It is pretty sad that your talking to a computer. Is it not "<< mystr <<"?\n ";
  std::cout << "Give yourself an attribute.\n"
    "(strength, dexterity, constitution, intelligence, wisdom, charisma).\n";
  std::cin >> userAttribute;

  userQuestion();

  while (userHealth > 0 && computerHealth > 0) {

    std::cout << "\nYou quickly lunge at your opponent, managing to knock their health down to " << (computerHealth = computerHealth - rand() % 10) << ".\nYou also lowered your offense to " << (userOffense = userOffense - rand() % 10) << ", and your rival's defense to " << (computerDefense = computerDefense - rand() % 10) << "\n";
    std::cout << "Your enemy darts at you and knocks your health down to " << (userHealth = userHealth - rand() % 10) << ", while lowering their offense to " << (computerOffense = computerOffense - rand() % 10) << ", and your defense to " << (userDefense = userDefense - rand() % 10) << ".\n";

  }

  if (userHealth <= 0) {

    std::cout << "\nYour opponent has won. Try again next time";

  }
  else if (computerHealth <= 0) {

    std::cout << "\nCongratulations, you won!";

  }

  else if (userHealth == computerHealth) {

    std::cout << "There was a tie!";

  }

  else {

    std::cout << "There was an error!";

  }

}
Nice work!

some little things to help you in the future. What you have is fine, this is just some minor pro tips.

1) useless variables:
1
2
3
4
5
6
7
///consider not having an extra variable here and similar places:
int offenseStats(int offense) 
{
  return rand() % 50 + 50;
  int offensePoints = rand() % 50 + 50;
  return offensePoints;
}

also consider reuse. the above, if you passed in the values to % and add, would be reusable for any random thing, not just this program...

2) magic numbers.
^^^ same code snippet, wtf is 50? Why 50, and not 37?
consider making a named constant value that says what it means.

3) this can be one:
if (userChoice == "G")
else if (userChoice == "g")
is the same as
if (userChoice == "G" || userChoice == "g")

4) don't exit 0 if you don't have to. return ends main cleaner. Functions like userquestion can return a code if they error out, rather than exit there, pass back to main and exit is cleaner. Exit 0 and similar like terminate() should be used sparingly, not for handy quits, it matters later when destructors need to be called on objects.


Last edited on
Topic archived. No new replies allowed.