tictactoe

Tic Tac Toe on the terminal using ANSI Escape Codes
Index Commits Files Refs README LICENSE
commit 540f980328e54584c6876520c1d8dd0f4b064053
parent ebc5259d87e306e666a666b67bfb94418451c6aa
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Mon,  1 Apr 2024 17:04:43 -0300

Use a `switch` case to handle game status

Diffstat:
Mtictactoe.cpp | 21+++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/tictactoe.cpp b/tictactoe.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;
     }
 }