Browse Source

Make walls_num a local variable

Make setup_level() and setup_level_bossrush() set the walls_num variable.
Make reset() accept the walls_num variable as an argument, to allow it
to create the correct number of walls. Finally remove the globl
WALLS_NUM variable and add the walls_num local variable to invader's and
bossrush's main().
Matteo Zeccoli Marazzini 7 months ago
parent
commit
c072eaaf48
5 changed files with 23 additions and 29 deletions
  1. 6 7
      bossrush.cpp
  2. 0 2
      definitions.hpp
  3. 9 8
      functions.cpp
  4. 3 3
      functions.hpp
  5. 5 9
      invaders.cpp

+ 6 - 7
bossrush.cpp

@@ -75,6 +75,7 @@ int main(int argc,char** argv)
 	//PARAMETERS/UTILITIES
 	double shootrate;				  	//probability of an enemy shooting a bomb 
 	double poweruprate;					//probability of a powerup being dropped
+	int walls_num;						//number of walls
 	int command;						//keyboard input
 	int score=0;					   	//score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
 	int level=1;						//difficulty level
@@ -130,19 +131,17 @@ int main(int argc,char** argv)
 	init_pair(8,COLOR_GREEN,COLOR_GREEN);
 	init_pair(9,COLOR_BLACK,COLOR_GREEN);
 	
-	WALLS_NUM=3;
-	
-	w_vec walls(WALLS_NUM);
+	w_vec walls(walls_num);
 	
 	Score=newwin(3,10,R/3,C+3);
 	BossHP=newwin(3,15,R/3-3,C+3);
 	
 	int i=0;
 	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);
+		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);
 
 	level = choose_level_bossrush(commands);
-	setup_level_bossrush(level, shootrate, poweruprate, refresh_time);
+	setup_level_bossrush(level, shootrate, poweruprate, walls_num, refresh_time);
 	
 	player1.set_commands(commands);
 	
@@ -291,8 +290,8 @@ int main(int argc,char** argv)
 					refresh();
 					if(choice=='n')
 						level = choose_level_bossrush(commands);
-					setup_level_bossrush(level, shootrate, poweruprate, refresh_time);
-					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					setup_level_bossrush(level, shootrate, poweruprate, walls_num, refresh_time);
+					reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, chflag); // reset box, player, enemy and deletes all bullets and bombs
 					resetbosses(bosses,boss1,player1);
 					num=0;
 					if(level<3)

+ 0 - 2
definitions.hpp

@@ -87,8 +87,6 @@
 #define backcyan "\033[46m"
 #define backgreen "\033[1;42m" 
 
-static int WALLS_NUM;
-
 extern std::mutex m_player;
 extern std::mutex m_enemies;
 extern std::mutex m_bombs;

+ 9 - 8
functions.cpp

@@ -294,7 +294,7 @@ int choose_level(int* commands)
 	return input - '0';
 }
 
-void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int* commands)
+void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int& walls_num, int* commands)
 {
 	switch(level)        //setting game parameters
 	{
@@ -308,6 +308,7 @@ void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t,
 			bossname=bossname+RECORD_DIR+BOSS_FILE1+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=30;
+			walls_num = 3;
 			break;
 		}
 		case 2:
@@ -320,6 +321,7 @@ void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t,
 			bossname=bossname+RECORD_DIR+BOSS_FILE2+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=40;
+			walls_num = 2;
 			break;
 		}
 		case 3:
@@ -332,6 +334,7 @@ void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t,
 			bossname=bossname+RECORD_DIR+BOSS_FILE3+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=45;
+			walls_num = 2;
 			break;
 		}
 		case 4:
@@ -344,6 +347,7 @@ void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t,
 			bossname=bossname+RECORD_DIR+BOSS_FILE4+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=50;
+			walls_num = 2;
 			break;
 		}
 	}
@@ -458,7 +462,7 @@ bool gameover(player& _player,b_list& bombs)		//Checks if a bomb/enemy/boss hit
 }
 
 
-void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_list& bombs, w_vec& walls, b_list& powerups, r_list& rockets, char level,int& chflag)
+void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_list& bombs, w_vec& walls, int walls_num, b_list& powerups, r_list& rockets, int& chflag)
 {
 	
 	_player.x=C/2;				//resetting initial position and stats of player1
@@ -491,11 +495,7 @@ void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_lis
 	rockets.clear();
 	chflag=0;
 	
-	WALLS_NUM=2;				//resetting walls
-	
-	if(atoi(&level)==1) WALLS_NUM=3;
-	
-	walls.resize(WALLS_NUM);
+	walls.resize(walls_num);
 
 	for(int i=0;i<walls.size();i++)
 		walls[i].create((i+1)*(C/(3*walls.size()+1))+i*(2*C/(3*walls.size()+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls.size())+1)),2,5);
@@ -1439,7 +1439,7 @@ int choose_level_bossrush(int* commands)
 	return input - '0';
 }
 
-void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& refresh_t)
+void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& walls_num, int& refresh_t)
 {
 	switch(level)        //setting game parameters
 	{
@@ -1468,6 +1468,7 @@ void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& r
 			poweruprate = 0.01;
 		}
 	}
+	walls_num = 3;
 }
 
 void resetbosses(boss* Bosses,boss& boss1,player& player1){

+ 3 - 3
functions.hpp

@@ -64,7 +64,7 @@ void print_scores();
 
 int choose_level(int* commands);
 
-void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int* commands);
+void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int& walls_num, int* commands);
 
 void change_commands(int* commands);
 
@@ -74,7 +74,7 @@ char playagain(WINDOW*);
 
 bool gameover(player&,b_list&);
 
-void reset(player&, e_list &, boss&, b_list& bullets, b_list& bombs, w_vec& walls, b_list &powerups,r_list&,char level,int& chflag);
+void reset(player&, e_list &, boss&, b_list& bullets, b_list& bombs, w_vec& walls, int walls_num, b_list &powerups, r_list&, int& chflag);
 
 bool enemyalive(e_list &);
 
@@ -92,7 +92,7 @@ void Defeat(int);
 
 int choose_level_bossrush(int* commands);
 
-void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& refresh_t);
+void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& walls_num, int& refresh_t);
 
 void resetbosses(boss* Bosses,boss& boss1,player& player1);
 

+ 5 - 9
invaders.cpp

@@ -60,6 +60,7 @@ int main(int argc,char** argv)
 	double shootrate;				  	//probability of an enemy shooting a bomb 
 	double poweruprate;					//probability of a powerup being dropped
 	int enemy_num;						//number of enemies in the current match
+	int walls_num;						//number of walls
 	int command;						//keyboard input
 	int score=0;					   	//score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
 	int level=1;						//difficulty level
@@ -103,18 +104,13 @@ int main(int argc,char** argv)
 	
 	//////////PROGRAM START: creation of game objects and set of parameters
 	
-	WALLS_NUM=2;
-	
 	level = choose_level(commands);     	// get desired level
-	setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, commands);     	// set game parameters and boss
+	setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num, commands);     	// set game parameters and boss
 	load_enemies(enemies,enemy_num);
 	player1.set_commands(commands);
 	erase();
 	
-	if(level==1) WALLS_NUM=3;
-	
-	
-	w_vec walls(WALLS_NUM);
+	w_vec walls(walls_num);
 	
 	Score=newwin(3,10,R/3,C+3);
 	BossHP=newwin(3,15,R/3-3,C+3);
@@ -279,9 +275,9 @@ int main(int argc,char** argv)
 					refresh();
 					if(choice=='n')
 						level = choose_level(commands);     	// get desired level
-					setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, commands);     	// set game parameters and boss
+					setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num, commands);     	// set game parameters and boss
 					load_enemies(enemies,enemy_num);
-					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, chflag); // reset box, player, enemy and deletes all bullets and bombs
 					erase();
 					refresh();
 					continue;	// Go to the beginning of the main loop