Browse Source

Cleanup switches in main loop to reuse code

In both invaders and bossrush, I merged the two switch that get executed when the match finishes to
reduce the amount of identical code written twice. For the same reasons,
I also merged the two 'y' and 'n' cases.
Matteo Zeccoli Marazzini 4 years ago
parent
commit
4f804defc6
2 changed files with 34 additions and 132 deletions
  1. 16 69
      bossrush.cpp
  2. 18 63
      invaders.cpp

+ 16 - 69
bossrush.cpp

@@ -259,87 +259,34 @@ int main(int argc,char** argv)
 			}
 
 		///////ENDGAME CHECKS               
-		if(boss1.health<1 && num==4)  					//YOU WON!!
+		if((boss1.health<1 && num==4) || gameover(player1,bombs))
 		{
-			Victory_bossrush(boss1.name,score,level,chflag);
-			
 			WINDOW *replay;
-			
-			replay=newwin(3,25,26,20);
-			box(replay,ACS_VLINE,ACS_HLINE);
-			
-			switch(playagain(replay))	// playagain() returns 'y'/'n'/'q'
+			if (boss1.health<1 && num==4)
 			{
-				case 'y':
-					delwin(replay);
-					timeout(500);
-					score=0;
-					erase();
-					refresh();
-					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(level<3)
-						player1.weaponclass=2;
-					erase();
-					refresh();
-					continue;
-				case 'n':
-					delwin(replay);
-					timeout(500);
-					score=0;
-					erase();
-					refresh();
-					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(level<3)
-						player1.weaponclass=2;
-					erase();
-					refresh();
-					continue;
-				case 'q':
-					endwin();
-					cout<<lightgreen<<"Game exited correctly."<<none<<endl;
-					return 0;
+				Victory_bossrush(boss1.name,score,level,chflag);
+				replay=newwin(3,25,26,20);
+			} else
+			{
+				Defeat(score);
+				replay=newwin(3,25,17,20);
 			}
-		}
-
-		if(gameover(player1,bombs))
-		{	
-			Defeat(score);
-			
-			WINDOW* replay;
-		
-			replay=newwin(3,25,17,20);
 			box(replay,ACS_VLINE,ACS_HLINE);
-			
-			switch(playagain(replay))	// playagain() returns 'y'/'n'/'q'
+
+			switch(char choice = playagain(replay))	// playagain() returns 'y'/'n'/'q'
 			{
 				case 'y':
-					delwin(replay);
-					timeout(500);
-					score=0;
-					erase();
-					refresh();
-					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(level<3)
-						player1.weaponclass=2;
-					erase();
-					refresh();
-					continue;
 				case 'n':
 					delwin(replay);
 					timeout(500);
 					score=0;
 					erase();
 					refresh();
-					level = choose_level_bossrush(commands);
-					setup_level_bossrush(level, shootrate, refresh_time);
+					if(choice=='n')
+					{
+						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;
@@ -347,7 +294,7 @@ int main(int argc,char** argv)
 						player1.weaponclass=2;
 					erase();
 					refresh();
-					continue;
+					continue;	// Go to the beginning of the main loop
 				case 'q':
 					endwin();
 					cout<<lightgreen<<"Game exited correctly."<<none<<endl;

+ 18 - 63
invaders.cpp

@@ -241,96 +241,51 @@ int main(int argc,char** argv)
 		interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score);
 
 		///////ENDGAME CHECKS               
-		if(boss1.health<1)  					//YOU WON!!
+		if(boss1.health<1 || gameover(player1,bombs))
 		{
 			/*if(sound){
 				pkill_music(thread[0]);	
 				win_theme = pthread_create(&thread[2],NULL,pwin_theme,NULL);
 			}*/
-				
-			Victory(boss1.name,score,level,chflag);
-			
+
 			WINDOW *replay;
-			
-			replay=newwin(3,25,26,20);
-			box(replay,ACS_VLINE,ACS_HLINE);
-		
-			switch(playagain(replay))	// playagain() returns 'y', 'n' or 'q'
+			if(boss1.health<1)	// YOU WON
 			{
-				case 'y':
-					delwin(replay);
-					timeout(500);
-					score=0;
-					erase();
-					refresh();
-					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
-					erase();
-					refresh();
-					continue;
-				case 'n':
-					delwin(replay);
-					timeout(500);
-					score=0;
-					erase();
-					refresh();
-					level = choose_level(commands);     	// get desired level
-					setup_level(level, shootrate, refresh_time, boss1, ENEMY_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
-					erase();
-					refresh();
-					continue;
-				case 'q':
-					endwin();
-					cout<<lightgreen<<"Game exited correctly."<<none<<endl;
-					return 0;
+				Victory(boss1.name,score,level,chflag);
+				replay=newwin(3,25,26,20);
+			} else			// YOU LOST
+			{
+				Defeat(score);
+				replay=newwin(3,25,17,20);
 			}
-		}
-				
-			
-		if(gameover(player1,bombs))
-		{
-			Defeat(score);
-			
-			WINDOW* replay;
-		
-			replay=newwin(3,25,17,20);
 			box(replay,ACS_VLINE,ACS_HLINE);
-			
-			switch(playagain(replay))                           //playagain() returns 'y', 'n' or 'q'
+
+			switch(char choice = playagain(replay))	// playagain() returns 'y', 'n' or 'q'
 			{
 				case 'y':
-					delwin(replay);
-					timeout(500);
-					score=0;
-					erase();
-					refresh();
-					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
-					erase();
-					refresh();
-					continue;
 				case 'n':
 					delwin(replay);
 					timeout(500);
 					score=0;
 					erase();
 					refresh();
-					level = choose_level(commands);     	// get desired level
-					setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
+					if(choice=='n')
+					{
+						level = choose_level(commands);     	// get desired level
+						setup_level(level, shootrate, refresh_time, boss1, ENEMY_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
 					erase();
 					refresh();
-					continue;
+					continue;	// Go to the beginning of the main loop
 				case 'q':
 					endwin();
 					cout<<lightgreen<<"Game exited correctly."<<none<<endl;
 					return 0;
 			}
 		}
-		
+
 		draw(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1);
 		write_score(Score,score);