I have time to complete this, I have to add 2 void functions, 2 value returning functions, 1 structure and on array. I just need some ideas on where to start, what should I convert to functions, how should I instill stuctures and arrays to this particular program. I'm going to add another character, another menu. Any guidance on how to polish this code up would be greatly appreciated.
#include<iostream>
#include<ctime>
#include<fstream>
using namespace std;
int main()
{
// Declare and initialize variables
string fileUname = " ";
string uname = " ";
int fileScore = 0;
int charaChoice = 0;
int mainChoice = 0;
int weapChoice = 0;
int score = 0;
int harry = 100;
int yoda = 200;
int gandolf = 300;
int wand = 0;
int lSaber = 0;
int magic;
int weapRand = 0, i=0;
int magicRand = 0, y=0;
// Introduction
cout<< "****************************\n";
cout<< "*********BE A HERO**********\n";
cout<< "****************************\n\n\n\n";
// Prompt user for name
cout << "Enter your name: ";
getline(cin,uname);
// Display main menu around a Do while loop
do{
cout<<"Please choose from the following menu\n";
cout<<"\t1) See rules\n";
cout<<"\t2) Play game\n";
cout<<"\t3) Exit\n";
cout<<"Enter choice here "<<uname<<": ";
cin>>mainChoice;
switch(mainChoice)
{
case 1:
cout<<"\tChoose 2 to start playing the game then pick a character and weapon\n\n\n";
break;
case 2:
cout<<"\tGet ready for your adventure!!!\n";
break;
case 3:
cout<<"\tThanks for playing!!!Bye\n";
return 0;
break;
default:
cout<<"Invalid choice please choose from the menu.\n";
}
}
while(mainChoice!=2);
// Display character menu
cout<<"Please choose from one of the following heroes\n";
cout<<"\t1) Harry Potter\n";
cout<<"\t2) Yoda\n";
cout<<"\t3) Gandolf\n";
cout<<"Enter choice here "<<uname<<": ";
cin>>charaChoice;
switch(charaChoice){
case 1:
charaChoice = harry;
cout<<"Expelliarmus!!!\n";
break;
case 2:
charaChoice = yoda;
cout<<"Do or do not! There is no try!\n";
break;
case 3:
charaChoice = gandolf;
cout<<"YOU SHALL NOT PASS!!!!!\n";
break;
default:
cout<< "Invalid choice choose again: ";
}
// cout << charaChoice<<endl;
// Display weapon menu
do{
cout<<"Please choose one from the following weapons\n";
cout<<"\t1) Wand\n";
cout<<"\t2) Light Saber\n";
cout<<"\t3) Magic\n";
cout<<"\t4) Finish\n";
cout<<"Enter choice here "<<uname<<": ";
cin>>weapChoice;
// if statement : if the user picks Flamethrower or Bow and arrow
// randomly generate number from 1-4 for practice session
if (weapChoice == 1){
weapChoice = wand;
srand(time(NULL)); //seed random number generator
//generate 4 numbers
// for(i=1; i<=4; i++){
weapRand = rand()%4 + 1; //generate a random number from 1 to 4
// }
if (weapRand == 1){
cout<< "You hit Target A, 100 points"<<endl;
charaChoice = charaChoice + 100;
}
else if (weapRand == 2){
cout<< "You hit Target B, 200 points"<<endl;
charaChoice = charaChoice + 200;
}
else if (weapRand == 3){
cout<< "You hit Target C, 300 points"<<endl;
charaChoice = charaChoice + 300;
}
else
cout<<"You missed the targets"<<endl;
// cout<<weapRand<<endl;
}
if (weapChoice == 2){
weapChoice = lSaber;
srand(time(NULL)); //seed random number generator
//generate 4 numbers
{
weapRand = rand()%4 + 1; //generate a random number from 1 to 4
if (weapRand == 1){
cout<< "You hit Target A, 100 points"<<endl;
charaChoice = charaChoice + 100;
}
else if (weapRand == 2){
cout<< "You hit Target B, 200 points"<<endl;
charaChoice = charaChoice + 200;
}
else if (weapRand == 3){
cout<< "You hit Target C, 300 points"<<endl;
charaChoice = charaChoice + 300;
}
else
cout<<"You missed the targets"<<endl;
// cout<<weapRand<<endl;
}
}
// if the user chooses magic user will randomly generate #
// between 1-2 : if 1 increment 200 points if 2 decrement 100 points
if (weapChoice == 3){
srand(time(NULL)); //seed random number generator
magicRand = rand()%2 + 1;
//generate 2 numbers
// }
// magicRand = rand()%2 + 1; //generate a random number from 1 to 2
if (magicRand == 1){
cout<< "Your magic worked, 200 points!!"<<endl;
charaChoice = charaChoice + 200;
}
else if (magicRand == 2){
cout<< "Your magic didn't work, minus 100 points"<<endl;
charaChoice = charaChoice + 200;
}
// cout<<magicRand<<endl;
}
//weapChoice = magic;
}
while(weapChoice != 4);
cout << "Your score is "<<charaChoice<<endl;
// if score reaches below 1 or after 10 tries end game
// (outside of program) create text file "highscore.txt" containing first name and value 0
// read in highscore.txt
// if score is greater than value in file replace with users name and score else do not change the file
ifstream infile;
infile.open("highscore.txt", ios::in);
infile>>fileUname>>fileScore;
infile.close();
ofstream outfile;
if(charaChoice > fileScore)
outfile.open("highscore.txt", ios::out);
outfile<<uname<<" "<<charaChoice<<endl;
outfile.close();
return 0;
}