Browse Source

Split choose_level_bossrush and make level an int

I split the function choose_level_bossrush into two functions with
different purposes. I also made level an integer.
Matteo Zeccoli Marazzini 4 years ago
parent
commit
e3f0c18bf7
3 changed files with 30 additions and 21 deletions
  1. 11 8
      bossrush.cpp
  2. 16 12
      functions.cpp
  3. 3 1
      functions.hpp

+ 11 - 8
bossrush.cpp

@@ -70,7 +70,7 @@ int main(int argc,char** argv)
 	double shootrate;				  	//probability of an enemy shooting a bomb 
 	int command;						//keyboard input
 	int score=0;					   	//score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
-	char level='1';						//difficulty level
+	int level=1;						//difficulty level
 	
 	create_std_bosses();
 	std::string bossname1=getenv("HOME");
@@ -134,11 +134,12 @@ int main(int argc,char** argv)
 	for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i)				//creating walls (in a quite symmetric pattern)
 		it->create((i+1)*(C/(3*WALLS_NUM+1))+i*(2*C/(3*WALLS_NUM+1)),2*R/3,(int)min(6,2*C/(3*WALLS_NUM+1)),2,3);
 
-	choose_level_bossrush(shootrate,refresh_time,level,commands);
+	level = choose_level_bossrush(commands);
+	setup_level_bossrush(level, shootrate, refresh_time);
 	
 	player1.set_commands(commands);
 	
-	if(atoi(&level)<3)
+	if(level<3)
 		player1.weaponclass=2;
 
 	erase();
@@ -230,7 +231,7 @@ int main(int argc,char** argv)
 		
 		if(boss1.health<=0)
 			if(num<4){
-				score+=250*atoi(&level)*(num+1);
+				score+=250*level*(num+1);
 				for(int i=max(0,boss1.y-1);i<min(R-1,boss1.y+boss1.height+1);i++)
 					for(int j=0;j<C;j++)
 						mvaddch(i,j,' ');
@@ -274,11 +275,12 @@ int main(int argc,char** argv)
 				score=0;
 				erase();
 				refresh();
-				choose_level_bossrush(shootrate, refresh_time,level,commands);
+				level = choose_level_bossrush(commands);
+				setup_level_bossrush(level, shootrate, refresh_time);
 				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
 				resetbosses(bosses,boss1,player1);
 				num=0;
-				if(atoi(&level)<3)
+				if(level<3)
 					player1.weaponclass=2;
 				erase();
 				refresh();
@@ -308,11 +310,12 @@ int main(int argc,char** argv)
 				score=0;
 				erase();
 				refresh();
-				choose_level_bossrush(shootrate, refresh_time, level,commands);
+				level = choose_level_bossrush(commands);
+				setup_level_bossrush(level, shootrate, refresh_time);
 				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag);       // reset box, player, enemy and delete all bullets and bombs
 				resetbosses(bosses,boss1,player1);
 				num=0;
-				if(atoi(&level)<3)
+				if(level<3)
 					player1.weaponclass=2;
 				erase();
 				refresh();

+ 16 - 12
functions.cpp

@@ -1367,10 +1367,11 @@ void pkill_music(pthread_t thread){
 				
 //=========BOSSRUSH FUNCTIONS=======================
 
-void choose_level_bossrush(double& shootr, int& refresh_t, char& level,int* commands)	//returns ENEMY_NUM
+int choose_level_bossrush(int* commands)
 {
 	std::string phrase;
 	phrase=choose_phrase();
+	char input;
 	
 	do{
 		print_title();
@@ -1398,37 +1399,40 @@ void choose_level_bossrush(double& shootr, int& refresh_t, char& level,int* comm
 
 		timeout(80);
 		
-		level=tolower(getch());
+		input=tolower(getch());
 	
-		if(!(atoi(&level)==1 || atoi(&level)==2 || atoi(&level)==3 || atoi(&level)==4 || level=='i' || level=='h' || level=='r' || level=='q') && level!=ERR){
+		if(!(input=='1' || input=='2' || input=='3' || input=='4' || input=='i' || input=='h' || input=='r' || input=='q') && input!=ERR){
 			attron(COLOR_PAIR(2));
 			mvprintw(14,0,"Bad input. Please choose a number within 1 and 4.");
 			attron(COLOR_PAIR(0));	
 		}
 		
-		if(level=='i')
+		if(input=='i')
 			print_info();
 		
-		else if(level=='h')
+		else if(input=='h')
 			print_scores_bossrush();
 		
-		else if(level=='r')
+		else if(input=='r')
 			phrase=choose_phrase();
 		
-		else if(level=='c')
+		else if(input=='c')
 			change_commands(commands);
 			
-		else if(level=='q'){
+		else if(input=='q'){
 			endwin();
 			cout<<lightgreen<<"Game exited correctly."<<none<<endl;
 			exit(1);
 		}
 		
-	}while(!(atoi(&level)==1 || atoi(&level)==2 || atoi(&level)==3 || atoi(&level)==4));
-	
-	refresh_t=0;
+	}while(!(input=='1' || input=='2' || input=='3' || input=='4'));
 	
-	switch(atoi(&level))        //setting game parameters
+	return input - '0';
+}
+
+void setup_level_bossrush(int level, double& shootr, int& refresh_t)
+{
+	switch(level)        //setting game parameters
 	{
 		case 1:
 		{

+ 3 - 1
functions.hpp

@@ -77,7 +77,9 @@ void Victory(std::string,int,char,int);
 
 void Defeat(int);
 
-void choose_level_bossrush(double& shootr,int& refresh_t,char& level,int* commands);
+int choose_level_bossrush(int* commands);
+
+void setup_level_bossrush(int level, double& shootr,int& refresh_t);
 
 void resetbosses(boss* Bosses,boss& boss1,player& player1);