Seeking basic RPG Critique

It's really simple- you select a class, and that class affects how much damage you do to the one monster.
This is all procedural programming- would it have been a better idea to use objects? Could I have made better use of procedural functions? Should I have used a function? I'm seeking any sort of advice.

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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int x;
    int stats [2];
    int monsterhealth = 6 + rand() % (4 - 6);
    short chance = 3 + rand() % (1 - 3);

    cout << "Welcome to my RPG! Have fun!\n";
    cout << "Select whether you want to be a fighter, a rogue, or a wizard.\n";
    cout << "1. Fighter \n2. Rogue \n3. Wizard\n";
    cin >> x;
    if (x = 1)
    {
        stats[0]=3;stats[1]=2;stats[2]=1;
    }
    else if (x = 2)
    {
        stats[0]=1;stats[1]=3;stats[2]=2;
    }
    else if (x = 3)
    {
        stats[0]=1;stats[1]=2;stats[2]=3;
    }
    cout << "A Monster has appeared!\nWhat do you do?\n\n";
    while (monsterhealth > 0)
    {
        cout << "\n1. Hit it with your blade!\n2. Shoot it with your bow!\n3. Cast a spell at it!\n";
        cin >> x;
            if (x = 1)
            {
                monsterhealth = monsterhealth - (stats[0] + chance);
            }
            else if (x = 2)
            {
                monsterhealth = monsterhealth - (stats[1] + chance);
            }
            else if (x = 3)
            {
                monsterhealth = monsterhealth - (stats[2] + chance);
            }
        cout << "Monster's health:" << monsterhealth << "\n";
        if (monsterhealth > 0)
            {cout << "It's not dead yet! Have another go at it!\n";}
    }
    cout << "The monster has been defeated! Yeah!\nYou saved the princess. Hooray!\n";
    return 0;
}
Aaaaand right after posting that I read about switch cases. I was wondering if you always had to mess with ifs that much. :P
Last edited on
Hi Noah,

The code looks nice and clean. I haven't ran the code. Just looked.

How about use a switch statement for dedicating stats to the characters?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch(x)
{
	case 1 :
		stats[0]=3;stats[1]=2;stats[2]=1;
		break;
	case 2 :
		stats[0]=1;stats[1]=3;stats[2]=2;
		break;
	case 3 :
		stats[0]=1;stats[1]=2;stats[2]=3;
		break;
	default :
		cout << "That’s not a choice.\n";
}


Also, remember to seed your rand function or you'll fight the same fight every time!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
#include <ctime>	// lets you use 'time' function

using namespace std;

int main()
{
	int x;
	int stats [2];
	
	srand(time(0));	 // randomize timer algorithm to seconds
	int monsterhealth = 6 + rand() % (4 - 6);
	short chance = 3 + rand() % (1 - 3);
	...
Last edited on
Topic archived. No new replies allowed.