Play my game, check my code!

Hi, I'm a novice programmer hoping to be able to survive CS courses when I get into college. It would really mean a lot to me if you looked over my code and offered some tips, also go ahead and give my little text based game a shot. :)

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <cstdlib>
#include <iostream>

using namespace std;

void clear()//Shortening common syntax
{
   
     cout << "\n\n";     
     system("PAUSE");
     system("cls");   
     
 }

int main(int argc, char *argv[])
{
    int game = 1;//UI stuff open *
    int choice = 0;
    int arena = false;//UI stuff close
    
    int lvl = 1;//Default Player Difficulty Level
    int wlvl = 1;//Default Weapon Prices
    int alvl = 1;//Default Armor Prices 
    int price = 20;//Default price
    
    char name[20];//Player Stats open *
    int hp = 100;
    int d = 10;
    int a = 10;
    int prot = 2;
    int gold = 100;
    int alive = true;//Player Stats close
    
    int mhp = 100;//Opponent Stats open *
    int md = 10;
    int ma = 10;//Opponent Stats close
    
    
    cout << "Welcome to Jake's Battle Sim! \n\n";
    cout << "Enter name now! \n\n";
    cin >> name;
    
    clear();
    
    cout << "Please note that it is recommended to visit the store before battle. \n\n";
    
    clear();
    
    while (true)
    {//main loop open *
    
    prot = 2 + (2*alvl);//Defence bonus from armor to player
    hp = 100 + (20*alvl);//Adds armor bonus to player
    d = 10 + (10*wlvl);//Adds weapon bonus to player
    
    mhp = 100 * lvl;
    md = 10 * lvl;
    
    alive = true;
    
    cout << "Hello "<< name << " what would you like to do? \n\n [1] Arena \n\n [2] Store \n\n [3] Exit\n\n";
    cout << "Player Health: "<< hp << "     Player Damage: " << d << "     Gold: "<< gold <<".\n\n";
    cin >> choice; 
    
    system ("cls");
    
    if (choice == 1)
    {//Arena Loop open *
         
         cout << "Welcome to the Arena "<< name << "! Prepare for DEATH! \n\n";      
         arena = 1;
         clear();
         
         while (arena == true) 
         {// Main Battle Loop open *
               
               cout << "Your opponent prepares for attack, how do you respond " << name << "!? \n\n [1] Attack \n\n [2] Defend \n\n [3] Run! \n\n";
               cin >> choice;
               system("cls");
               
               if (choice == true)
               {//Attack Loop open *
               
               hp = hp - md;
               mhp = mhp - d;
                      
               cout << "You Attacked! \n\n Enemy Health: "<< mhp <<"\n\n Your Health: "<< hp <<"\n\n";
               clear();
                          
                          }//Attack Loop close 
               else if (choice == 2)
               {//Defend Loop open *
               
             
               mhp = mhp - (d/2);
               hp = hp - md; 
               hp = hp + prot;
               cout << "You Defended! \n\n Enemy Health: "<< mhp <<"\n\n Your Health: "<< hp <<"\n\n";     
               clear();
               
               }//Defend Loop close
               
               else if (choice == 3)
               {//Run Loop open *
               
               cout << "You Run."; 
               arena = false;
                    
                    }//Run Loop close
               

               
                   
                    if (hp || mhp <= 0)
                    {//Victory/Loss check open *
                    
                      if (hp <= 0)
                      {//Loss Check open *
                           
                             cout << "You lose! \n\n -" << 10*lvl << " gold";
                             gold = gold - (10*lvl);
                             alive = false;
                             arena = false; 
                             lvl = lvl - 1;
                             clear();
                             }//Loss check close
                      if (mhp <= 0 && alive == true)
                      {//Victory check open *
                              
                              cout << "You won! \n\n " << 20*lvl << " gold";
                              gold = gold + (30*lvl);
                              lvl = lvl + 1;
                              arena = false; 
                              clear();
                              }//Victory check close
                              
                      }//Victory/Loss check close
                      
                      
                      
               
               }//Main Battle Loop close
               
               }//Arena Loop close
               
    else if (choice == 2) 
    {//Store Loop open *
         
         cout << "Welcome "<< name << " to the store! \n\n";
         clear();
         cout << "How can I help you? \n\n [1] Armor \n\n [2] Weapons \n\n";
         
         cin >> choice;
         
         if (choice == 1)
         {//Upgrade armor loop open *
         
         price = price * alvl;
         
         if (gold >= price)
         {//Purchase Success open *      
         system("cls");
         cout << "You upgraded Armor!\n\n";
         alvl = alvl + 1;
         gold = gold - price;
         cout << "Gold: " << gold <<".\n\n";
         }//Purchase Success close
         else if (gold <= price)
         {
         cout << "Sorry, you don't have the cash. \n\n";
         clear();
              }
                   price = 20; }//Upgrade armor loop close

  else if (choice == 2)
         {//Upgrade weapon loop open *
         
         price = price * wlvl;
         
         if (gold >= price)
         {//Purchase Success open *      
         system ("cls");
         cout << "You upgraded Weapon!\n\n";
         wlvl = wlvl + 1;
         gold = gold - price;
         cout << "Gold: " << gold <<".\n\n";
         }//Purchase Success close
         else if (gold <= price)
         {
         cout << "Sorry, you don't have the cash. \n\n";
         clear();
              }
                   price = 20; }//Upgrade weapon loop close
                   
              else if (choice == 3);
              {
                   
                   cout << "You exit the store";
                   clear();
                   
               }

         }//Store Loop close
    else if (choice ==3)
    {//Exit Loop open *
         
         cout << "You Exit.";
         clear();
         return 0;
         
         }//Exit Loop close
         
         //clear();
         
          }//main loop close
    
    return 0;
}
One tip: Maybe, separate the code into function, instead of all being together in the main function.

Also...

You Attacked! 

 Enemy Health: -20

 Your Health: 100



You won! 


 20 gold



:D



Edit: Very minor thing, but you might want to put a couple new lines before the welcome screen or something to avoid this:


You Run.Hello name what would you like to do?


Edit 2: You may also want to define the choice numbers like int store = 2;, instead of hard-coding it in. That way it will make it way more easy to customize it if you want in the future.
Last edited on
cout << "How can I help you? \n\n [1] Armor \n\n [2] Weapons \n\n";

Would be nice to know how much each costs before making a choice :)


Player Health: 120     Player Damage: 20     Gold: 100. //starting point

//shop for armor, weapons

Player Health: 140     Player Damage: 30     Gold: 60. //after store

//go to arena and commence attacking until

You Attacked! 
 Enemy Health: -20
 Your Health: 100

You won! 
 20 gold

// then game resets?
Player Health: 140     Player Damage: 30     Gold: 90. //extra 10 gold - I only won 20 after having 60? or should this have reset to 100? 
Last edited on
@Wildblue

First off, I just added the cost to the UI. :)

Second, health will reset after each battle is over however gold will be made through battling. The idea was to make this game open ended so the things you fight continuously get harder via the lvl multiplier. You take the gold and prepare to fight your next opponent, but when you lose a battle you go down a lvl to prevent you from getting stuck.

@programmerdog297

I have to admit I'm a bit embarrassed that I didn't catch the run output conflicting with the main menu. Easy fix.

Could you explain to me further how to avoid hard-coding? I think I understand conceptually but could you give me a simple example code?

Also, how do I separate them into multiple different functions? Is it basically what I did with the cls stuff?

To clarify, this is the cls stuff:
1
2
3
4
5
6
7
8
void clear()//Shortening common syntax
{
   
     cout << "\n\n";     
     system("PAUSE");
     system("cls");   
     
 }


Sorry my knowledge is extremely spotty, I've been able to program for years and I've known I want to do it as a career, but I haven't really gotten into it seriously yet.
Example of not hard-coding:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int arenaChoice = 1, storeChoice = 2, exitChoice = 3;

cout << "Hello "<< name << " what would you like to do? \n\n [" << arenaChoice << "] Arena \n\n [" << storeChoice << "] Store \n\n [" << exitChoice << "] Exit\n\n";

//...

if (choice == arenaChoice) {
    //...
}
else if (choice == storeChoice) {
    //...
} else if (choice == exitChoice) {
    //...
}


Is it basically what I did with the cls stuff?

Pretty much. Like if you choice "store", instead of putting the store code in that if statement, you could a function called something like "storeLoop()".
For more about functions: http://www.cplusplus.com/doc/tutorial/functions/
Alright thank you! I must have learned about them previously and just forgot them over time. (This is my first program in like 6 months.) Also, I have altered my code according to your instruction :P

The reason I didn't put the store and arena in different functions is because I couldn't figure out how to use integers and chars in multiple different functions.

For example, the gold in the players person is relevant at both the home screen and the store screen.
Topic archived. No new replies allowed.