commit 91b7f4419bd470ea646174f738d42227a8c1b73a
parent 53c244f09bddfb8ff826ba539582fa8cd0f348af
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Mon, 1 Apr 2024 17:18:47 -0300
Use a `switch` case to handle game status
Diffstat:
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/guias/2/ej12.cpp b/guias/2/ej12.cpp
@@ -62,26 +62,35 @@ void game_clear_board(void) {
void game_player_status_msg() {
std::ostringstream current_player_string;
+ unsigned int player_won;
key_string.assign("\033[0J");
- if(game_status == PLAYING) {
+ switch(game_status) {
+ case PLAYING:
player_status = std::string("Player ");
current_player_string << current_player+1;
player_status += current_player_string.str();
player_status += "\033[0J";
- }
- else if (game_status == WIN) {
- unsigned int player_won = ((current_player == PLAYER_1) ? PLAYER_2 : PLAYER_1);
+ break;
+ case WIN:
+ player_won = ((current_player == PLAYER_1) ? PLAYER_2 : PLAYER_1);
player_status = std::string("Player ");
current_player_string << player_won+1;
player_status += current_player_string.str();
player_status += " Wins";
+ key_string.assign("Press `r` to restart\033[0J");
game_status = HALT;
- }
- else if (game_status == DRAFT) {
+ break;
+ case DRAFT:
player_status.assign("Draft\033[0J");
key_string.assign("Press `r` to restart\033[0J");
game_status = HALT;
+ break;
+ case HALT:
+ key_string.assign("Press `r` to restart\033[0J");
+ break;
+ default:
+ break;
}
}