Console Game Project Help

So as a first timer C++ programmer I am trying to make a small game project to better my skills. I wanted to first share the code, then throw out a couple questions for anybody that wouldn't mind helping me out. =)

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <string>
#include <windows.h>
#include <string>

using namespace std;

//Functions------------------------------------------->

int lifepoints;

void checker ()
{
    if (lifepoints == 0)
    {
        cout << "\nLifepoints = 0.";
        cout << "\nYou have run out of Lifepoint's causing your character to die.";
        cout << "\nGame over.\n";
    }
    else
        cout << "\nYou currently have " << lifepoints << " remaining.\n";
        Sleep(500);


}

void pause()
{
    cout << "\n"; system("pause");
}


int main(void)
{

//Game Introduction------------------------------------------->

    lifepoints = 5000;

    cout << "Welcome to Survival Quest!" << endl;
    cout << "\nYou're goal is to get through a series of trials which will test your knowledge"
        << "\nand capabilities. For each section you pass, a bonus may be awarded. For each"
        << "\nsection you fail, you will lose a certain number of lifepoints. If your lifepoints"
        << "\nreach 0, you will lose the game."
        << endl;
        Sleep(500);
    cout << "\nYou have " << lifepoints << " lifepoints.\n"
        << "Remember, if these run out, you lose the game.\n";
        Sleep(500);

    cout << "------------------------------------------------------------------------------------------";


//Player information ------------------------------------------->

    string name;

    cout << "\n\nLets start with the basics! What is your name? ";
    cout <<"\nName: ";
    cin >> name;

    cout << "\nHello " << name << "!" << endl;

    cout << "\nAs a player you get to choose between 3 individual skills which each grant you a single ability.\n"
        << "\nPlease choose from the following:\n";

    int skill = 0;

    do
    {
        cout << "1) Select Strength (grants 1000 extra LP)\n"
            << "2) Select Wisdom (Avoids two survival questions within the game)\n"
            << "3) Select Willpower (Triggers a 'single use' recovery question should you hit 0 lifepoints)\n";
        cin >> skill;
    }

    while (skill < 1 || skill > 3);
        if (skill == 1)
        {
            lifepoints = lifepoints + 1000;
            cout << "Your lifepoints are now at " << lifepoints << ".";
            Sleep(500);
        }
        else if (skill == 2)
        {
            cout << "\nYou selected wisdom. You will now avoid two survival questions.\n";
            Sleep(500);
        }
        else
        {
            cout << "\nYou selected willpower. You are now eligible for a sudden death recovery question should"
                << "\nyour life points reach 0.\n";
            Sleep(500);
        }

    pause();


cout << "\n------------------------------------------------------------------------------------------\n";
//First Section ------------------------------------------->

    cout << "\nYou begin your adventure wondering through a forest.\n";
    Sleep(500);
    cout << "The trees in your surrounding area are as big as a\n";
    Sleep(500);
    cout << "cabin and as high as the clouds. As you round yet another\n";
    Sleep(500);
    cout << "bend you realize you are being tracked. When you turn\n";
    Sleep(500);
    cout << "around you notice, in fright, a wolf hunting you...\n\n";
    pause();

    int response = 0;

        do
        {
            cout << "\nWhat action would you like to take? \n";
                Sleep(500);
            cout << " 1) Attack the beast! \n";
                Sleep(500);
            cout << " 2) Wait for response... \n";
                Sleep(500);
            cout << " 3) Retreat from the encounter. \n\n";
            cout << "Answer: ";
            cin >> response;
        }

        while (response < 1 || response > 3);
            if (response == 1)
            {
                cout << "\nYou move to strike but the wolf is too quick for you!";
                Sleep(500);
                cout << "\nYou barely dodge a lethal blow however the wolf has";
                Sleep(500);
                cout << "\nleft a nasty injury upon you.";
                Sleep(500);

                cout << "\n\n--You lose 1000 lifepoints!--";
                lifepoints = lifepoints - 1000;
                checker();
                pause();
            }
            else if (response == 2)
            {
                cout << "\nIn your delay, the wolf suddenly strikes!";
                Sleep(500);
                cout << "\nFortunately your quick reflexes mostly prevent";
                Sleep(500);
                cout << "\nany serious injury.";
                Sleep(500);

                cout << "\n\n--You lose 400 lifepoints!--";
                lifepoints = lifepoints - 400;
                checker();
                pause();

            }
            else
            {
                cout << "\nAs you begin your retreat the wolf seems to back down.";
                Sleep(500);

                cout << "\n\n--You gain 200 lifepoints!--";
                lifepoints = lifepoints + 200;
                checker();
                pause();
            }

cout << "\n------------------------------------------------------------------------------------------\n";
//Second Section ------------------------------------------->



//End Main
}


My first question:

Assuming the player were to select "willpower" as the skill... how would I go about setting up a function (or at least some sort of operation) that allows for a single recovery question should "lifepoints" reach 0. But is only usable once. My function "void checker" is designed to end the game the moment lifepoints are at 0 but only runs when I tell it too. How can I modify this in conjunction with another function that allows you to recover from 0 only ONCE, and only if the user selects willpower as the skill.

Since its such a big question I figure ill save others for later as I continue to develop this little learning project. Thanks in advance!

You appear to be hard-coding parameters of the game into the game itself.

You need to devise a way whereby the questions and answers are external data that drive the game, rather than the game itself having to know questions, answers and responses.

Once you do that, you can extend that data structure and your program to support new and different features.
You should probably build a game loop that will run as long as the willpower is greater than zero. The way to do that is to build a class that has several methods, each one contain a part of the game story. You can also build another class that will return the value of willpower, which can be a private member variable. In main, you can have a while loop which will call the method to see if willpower is still greater than zero.
Line 10: Globals should be avoided.

Line 77,128: The while statement doesn't do anything. The ; terminates the loop.

Line 140,154,165: You call checker(), but checker() continues if lifepoints goes to zero. This won't end the game.

how would I go about setting up a function (or at least some sort of operation) that allows for a single recovery question should "lifepoints" reach 0.

You can add a bool variable that tracks whether the recovery question has been used or not. If the bool variable has been set, then the player "dies". The easiest way to do this is to simply call exit().




Topic archived. No new replies allowed.