Oct 7, 2017 at 10:28am UTC
Hello all. I am looking for helpful tips on how to make my program more efficient. i.e. utilizing pointers correctly, passing info, etc. I want to make sure that I am understanding how to utilize most of the techniques efficiently, and/or ways to implement what I am achieving more effectively, i.e. not using pointers where I shouldn't be.
** NOTE, I already know about the SYSTEM issue, I only used it in my code because it was easy and did the job. I also have not finished the project, just wanted to stop and get input for further suggestions before moving on, so there are some unused variables, etc.
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
Main.cpp
#include <iostream>
#include <string>
#include "CharacterCreation.h"
using namespace std;
//|-------------------------|//
//|////////--------/////////|//
//|BEGINNING OF YOUR PROGRAM|//
//|////////--------/////////|//
//|-------------------------|//
user_player::player1 addrp1;
user_player u_p1;
void quit_game();
void movement();
int main()
{
startover:
user_player::user_player();
u_p1.getName(&addrp1);
u_p1.getRace(&addrp1);
u_p1.getClass(&addrp1);
u_p1.startPotion(&addrp1);
u_p1.start_right_hand(&addrp1);
char response;
bool valid = false ;
do
{
cout << "CHARACTER" << endl;
cout << "--------------------------" << endl;
cout << "Character Name: " << addrp1.u_name;
cout << "\tCharacter Race: " << addrp1.u_race;
cout << "\tCharacter Class: " << addrp1.u_class << endl;
cout << "Health: " << addrp1._hlt << "\t\tAttack: " << addrp1._atk << "\t\tDefense: " << addrp1._def << endl
<< "Intelligence: " << addrp1._int << " \tMagic: " << addrp1._mag << "\t\tSpeed: " << addrp1._spd << endl;
cout << endl;
cout << "INVENTORY" << endl;
cout << "--------------------------" << endl;
cout << "Slot 1: " << addrp1.inventory[0] << " | Slot 2: " << addrp1.inventory[1] << " | Slot 3: " << addrp1.inventory[2] <<
" | Slot 4: " << addrp1.inventory[3] << " | Slot 5: " << addrp1.inventory[4] << endl;
cout << endl;
cout << "EQUIPMENT" << endl;
cout << "--------------------------" << endl;
cout << "Left Hand: " << addrp1.eq_lh.lh_wname << "\tRight Hand: " << addrp1.eq_rh.rh_wname << "\tHeadware: " << addrp1.eq_head.head_name << endl
<< "Shoes: " << addrp1.eq_shoe.shoes_name << "\t\tArmor: " << addrp1.eq_arm.arm_name << endl;
cout << endl;
cout << "Are you sure about your choices? (y/n)" << endl;
cin >> response;
if (response == 'n' || response == 'N' )
{
goto startover;
}
else if (response == 'y' || response == 'Y' )
{
cout << "Let's begin your journey!" ;
pause;
pause;
valid = true ;
clearscr;
}
else
{
cout << "You did not chose a valid option!" ;
pause;
pause;
clearscr;
}
} while (valid != true );
movement();
quit_game();
return 0;
}
void quit_game()
{
clearscr;
cout << "Thanks for playing!" << endl;
pause;
pause;
exit(0);
}
void movement()
{
char get_key;
bool cont = true ;
//This is the players starting position
int POS[2] = { 0 ,0 };
do
{
cout << "Please enter (N)orth, (S)outh, (E)ast, (W)est, (P)erson, or (Q)uit." << endl;
cout << POS[0] << ", " << POS[1];
cout << endl;
cout << "What do you want to do: " ;
cin >> get_key;
if (get_key == 'N' || get_key == 'n' )
{
POS[0] += 1;
clearscr;
}
else if (get_key == 'S' || get_key == 's' )
{
POS[0] -= 1;
clearscr;
}
else if (get_key == 'W' || get_key == 'w' )
{
POS[1] += 1;
clearscr;
}
else if (get_key == 'E' || get_key == 'e' )
{
POS[1] -= 1;
clearscr;
}
else if (get_key == 'P' || get_key == 'p' )
{
clearscr;
u_p1.display_stats(&addrp1);
}
else if (get_key == 'Q' || get_key == 'q' )
{
cont = false ;
}
else
{
cout << "You did not enter a valid option." ;
pause;
pause;
}
} while (cont != false );
}
Last edited on Oct 7, 2017 at 10:35am UTC
Oct 7, 2017 at 10:29am UTC
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
#ifndef CHARACTERCREATION_H
#define CHARACTERCREATION_H
#include <iostream>
#include <string>
#define clearscr system("cls");
#define pause std::cin.get();
using namespace std;
class user_player
{
private :
public :
///////////////////////////////////////
//DEFINITION OF YOUR PLAYER STRUCTURE//
///////////////////////////////////////
struct player1
{
//Person
string u_name;
string u_class;
string u_race;
//stats
double _hlt;
double _atk;
double _def;
double _int;
double _mag;
double _spd;
//Inventory
string inventory[5] = { "Empty" , "Empty" , "Empty" , "Empty" , "Empty" };
//Equipment
struct eq_left_hand
{
string lh_wname = " Nothing Equipped" ;
int lh_atk = 0;
int lh_def = 0;
int lh_mag = 0;
}eq_lh;
struct eq_right_hand
{
string rh_wname = "Nothing Equipped" ;
int rh_atk = 0;
int rh_def = 0;
int rh_mag = 0;
}eq_rh;
struct eq_shoes
{
string shoes_name = "Nothing Equipped" ;
int shoes_def = 0;
int shoes_spd = 0;
}eq_shoe;
struct eq_armor
{
string arm_name = "Nothing Equipped" ;
int arm_def = 0;
int arm_spd = 0;
}eq_arm;
struct eq_headware
{
string head_name = "Nothing Equipped" ;
int head_def = 0;
int head_mag = 0;
int head_spd = 0;
}eq_head;
}p1;
//constructor
user_player::user_player()
{
p1.u_name = "Null" ;
p1.u_race = "Null" ;
p1.u_class = "Null" ;
p1._hlt = 0;
p1._atk = 0;
p1._def = 0;
p1._int = 0;
p1._mag = 0;
p1._spd = 0;
}
//destructor
user_player::~user_player()
{
}
////////////////////////////
//// MEMBER FUNCTIONS ////
////////////////////////////
void getName(player1 * p1)
{
clearscr;
cout << "Hello traveler, what is your name? " ;
cin >> p1->u_name;
cout << "Nice to meet you " << p1->u_name << "!" ;
pause;
pause;
}
void getRace(player1 * p1)
{
int answer;
bool valid = false ;
char yn;
bool val_yn = false ;
clearscr;
do
{
cout << "Alright " << p1->u_name << ", what race are you? Please enter 1, 2, or 3.\n"
"1. Human\n"
"2. Goblin\n"
"3. Elf" << endl;
cin >> answer;
switch (answer)
{
case 1:
do
{
cout << "You chose human. Are you sure about your choice? (y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_race = "Human" ;
p1->_hlt = 25;
p1->_atk = 5;
p1->_def = 4;
p1->_int = 3;
p1->_mag = 3;
p1->_spd = 4;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
} while (val_yn != true );
break ;
case 2:
do
{
cout << "You chose goblin. Are you sure about your choice?(y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_race = "Goblin" ;
p1->_hlt = 27;
p1->_atk = 7;
p1->_def = 6;
p1->_int = 1;
p1->_mag = 2;
p1->_spd = 4;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n: " ;
pause;
pause;
}
} while (val_yn != true );
break ;
case 3:
do
{
cout << "You chose elf. Are you sure about your choice?(y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_race = "Elf" ;
p1->_hlt = 22;
p1->_atk = 3;
p1->_def = 2;
p1->_int = 4;
p1->_mag = 5;
p1->_spd = 6;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
}while (val_yn != true );
break ;
default :
cout << endl;
cout << "You did not chose a valid option. Please choose again. \n" ;
pause;
pause;
break ;
}
clearscr;
} while (valid != true );
}
Last edited on Oct 7, 2017 at 10:33am UTC
Oct 7, 2017 at 10:31am UTC
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
void getRace(player1 * p1)
{
int answer;
bool valid = false ;
char yn;
bool val_yn = false ;
clearscr;
do
{
cout << "Alright " << p1->u_name << ", what race are you? Please enter 1, 2, or 3.\n"
"1. Human\n"
"2. Goblin\n"
"3. Elf" << endl;
cin >> answer;
switch (answer)
{
case 1:
do
{
cout << "You chose human. Are you sure about your choice? (y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_race = "Human" ;
p1->_hlt = 25;
p1->_atk = 5;
p1->_def = 4;
p1->_int = 3;
p1->_mag = 3;
p1->_spd = 4;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
} while (val_yn != true );
break ;
case 2:
do
{
cout << "You chose goblin. Are you sure about your choice?(y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_race = "Goblin" ;
p1->_hlt = 27;
p1->_atk = 7;
p1->_def = 6;
p1->_int = 1;
p1->_mag = 2;
p1->_spd = 4;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n: " ;
pause;
pause;
}
} while (val_yn != true );
break ;
case 3:
do
{
cout << "You chose elf. Are you sure about your choice?(y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_race = "Elf" ;
p1->_hlt = 22;
p1->_atk = 3;
p1->_def = 2;
p1->_int = 4;
p1->_mag = 5;
p1->_spd = 6;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
}while (val_yn != true );
break ;
default :
cout << endl;
cout << "You did not chose a valid option. Please choose again. \n" ;
pause;
pause;
break ;
}
clearscr;
} while (valid != true );
}
void getClass(player1 * p1)
{
int answer;
bool valid = false ;
char yn;
bool val_yn = false ;
clearscr;
do {
cout << "Alright " << p1->u_name << ", what race are you? Please enter 1, 2, or 3.\n"
"1. Warrior\n"
"2. Hunter\n"
"3. Mage" << endl;
cin >> answer;
switch (answer)
{
case 1:
do
{
cout << "You chose warrior. Are you sure about your choice? (y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_class = "Warrior" ;
p1->_atk += 2;
p1->_def += 2;
p1->_spd += 1;
val_yn = true ;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
} while (val_yn != true );
break ;
case 2:
do
{
cout << "You chose hunter. Are you sure about your choice?(y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_class = "Hunter" ;
p1->_atk += 1;
p1->_def += 1;
p1->_spd += 3;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
} while (val_yn != true );
break ;
case 3:
do
{
cout << "You chose mage. Are you sure about your choice?(y/n)" << endl;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->u_class = "Mage" ;
p1->_int += 3;
p1->_mag += 3;
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << endl;
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option. Please enter y or n." ;
pause;
pause;
}
} while (val_yn != true );
break ;
default :
cout << endl;
cout << "You did not chose a valid option. Please choose again. \n" ;
pause;
pause;
break ;
}
clearscr;
}while (valid != true );
}
void startPotion(player1 * p1)
{
int choice;
bool valid = false ;
char yn;
do
{
cout << "Alright, you are being given a choice between a magic potion or a healing potion. Which do you choose?\n"
"1. Magic potion\n"
"2. Healing potion" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "You have chosen magic potion, is this correct? (y/n): " ;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->inventory[0] = { "Magic Potion" };
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option." ;
pause;
pause;
break ;
}
case 2:
cout << "You have chosen healing potion, is this correct? (y/n): " ;
cin >> yn;
if (yn == 'y' || yn == 'Y' )
{
p1->inventory[0] = { "Healing Potion" };
valid = true ;
break ;
}
else if (yn == 'n' || yn == 'N' )
{
cout << "Please make your selection." ;
pause;
pause;
break ;
}
else
{
cout << "You did not chose a valid option." ;
pause;
pause;
break ;
}
}
clearscr;
} while (valid != true );
}
Last edited on Oct 7, 2017 at 10:33am UTC
Oct 7, 2017 at 10:32am UTC
One obvious improvement would be to use a switch control instead of cascading if's.
See: http://www.cplusplus.com/doc/tutorial/control/
Another one is to convert to uppercase instead of the ||'s lookup toupper() function.
Oct 7, 2017 at 11:29am UTC
A few tips to consider:
1. Functions should only perform 1 operation, maybe 2 at most.
2. Keep code line widths to 80 characters maximum.
3. If you find you have to nest if, else if, else a lot and especially if they are nested in other control structures, then the chances are you're going to lose track of the logic of the choices and there's a better, simpler and clearer way to doo it
Last edited on Oct 14, 2017 at 4:55pm UTC
Oct 7, 2017 at 12:39pm UTC
Thanks for the input, I picked it up a while ago and things got real so I had to drop it, and I have been relearning for about 2 months so I am still getting used to everything again. Just thought I would try to utilize what I know and try to make something, I know I have other methods to learn and will get to them. Thanks for the pointers I will re-evaluate my code and see if I can clean some things up
Last edited on Oct 7, 2017 at 11:09pm UTC
Oct 7, 2017 at 1:18pm UTC
edit: I just realized POS is just position, but you have other places with hard constants. You can do this for equipment lists, etc.
hard constants are hard to understand when you come back to a program after just a few weeks.
things like POS[0] = something;
is not so hot.
you can name them:
enum pos_stuff
{
zero, //these auto increment, the first is 0, the second is 1, ...
one, //but you want names that represent what it is, str, hit_points, whatever it *means* here.
two,
hit_points,
pos_max; //this will auto grow your arrays if you need to insert a value:
};
type POS[pos_max];
for(z = 0; z < pos_max; z++) //if you insert a value in the enum, these 2 lines are still fine!!!
and do this:
POS[hit_points] = 53;
the enum trick works with fixed-size vectors, the newer arrays, and map containers as well. Any place where you have a chunk of known at compile time data. For large programs you should wrap the enums in a namespace.
Last edited on Oct 7, 2017 at 1:22pm UTC