diff options
author | Alex <colomar.6.4.3@gmail.com> | 2019-04-08 02:10:16 +0100 |
---|---|---|
committer | Alex <colomar.6.4.3@gmail.com> | 2019-04-08 02:10:16 +0100 |
commit | 217ad40401acf5b158e97cbd97586c13a15c8f4b (patch) | |
tree | 6cbb7cdbb9bc4631af39cb3b5df9f910cada93ce | |
parent | 0d1a43dfe1a0eb070b158aadb8d77a281370c4d4 (diff) |
Fix style
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 12 | ||||
m--------- | libalx | 0 | ||||
-rw-r--r-- | modules/game/src/game.c | 27 | ||||
-rw-r--r-- | modules/game/src/game_iface.c | 30 | ||||
-rw-r--r-- | modules/menu/src/menu_clui.c | 52 | ||||
-rw-r--r-- | modules/menu/src/menu_gui.c | 61 | ||||
-rw-r--r-- | modules/menu/src/menu_iface.c | 41 | ||||
-rw-r--r-- | modules/menu/src/menu_tui.c | 21 | ||||
-rw-r--r-- | modules/menu/src/parser.c | 41 | ||||
-rw-r--r-- | modules/player/src/player_clui.c | 109 | ||||
-rw-r--r-- | modules/player/src/player_gui.c | 39 | ||||
-rw-r--r-- | modules/player/src/player_iface.c | 89 | ||||
-rw-r--r-- | modules/player/src/player_tui.c | 76 | ||||
-rw-r--r-- | modules/save/src/save.c | 23 | ||||
-rw-r--r-- | modules/save/src/score.c | 168 | ||||
-rw-r--r-- | modules/xyzzy/src/xyzzy.c | 15 | ||||
-rw-r--r-- | src/main.c | 8 |
18 files changed, 333 insertions, 480 deletions
@@ -2,3 +2,4 @@ *.s bin/mine-sweeper bin/mine-sweeper.exe +Checkstyle/* @@ -233,12 +233,12 @@ install: uninstall @echo " Install:" @echo " MKDIR $(INSTALL_BIN_DIR)/" $(Q)mkdir -p $(DESTDIR)/$(INSTALL_BIN_DIR)/ - @echo " CP $(BIN_NAME)" - $(Q)cp $(BIN_DIR)/$(BIN_NAME) $(DESTDIR)/$(INSTALL_BIN_DIR)/ + @echo " CP -v $(BIN_NAME)" + $(Q)cp -v $(BIN_DIR)/$(BIN_NAME) $(DESTDIR)/$(INSTALL_BIN_DIR)/ @echo " MKDIR $(INSTALL_SHARE_DIR)/$(SHARE_DIR)/" $(Q)mkdir -p $(DESTDIR)/$(INSTALL_SHARE_DIR)/$(SHARE_DIR)/ - @echo " CP -r share/*" - $(Q)cp -r ./share/* $(DESTDIR)/$(INSTALL_SHARE_DIR)/$(SHARE_DIR)/ + @echo " CP -rv share/*" + $(Q)cp -r -v ./share/* $(DESTDIR)/$(INSTALL_SHARE_DIR)/$(SHARE_DIR)/ @echo " MKDIR $(INSTALL_VAR_DIR)/$(VAR_DIR)/" $(Q)mkdir -p $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/ @@ -246,8 +246,8 @@ install: uninstall $(Q)mkdir $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/boards_intermediate/ $(Q)mkdir $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/boards_expert/ $(Q)mkdir $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/boards_custom/ - @echo " CP -r var/*" - $(Q)cp -r ./var/* $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/ + @echo " CP -rv var/*" + $(Q)cp -r -v ./var/* $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/ @echo " CHOWN $(INSTALL_VAR_DIR)/$(VAR_DIR)/" $(Q)chown root:games -R $(DESTDIR)/$(INSTALL_VAR_DIR)/$(VAR_DIR)/ @echo " CHMOD $(INSTALL_VAR_DIR)/$(VAR_DIR)/" diff --git a/libalx b/libalx -Subproject b1316de661a72f13381bed965ac60e001bf750d +Subproject c142cca0332b768ae381cc938e542bef0d1d620 diff --git a/modules/game/src/game.c b/modules/game/src/game.c index be749aa..1f912dd 100644 --- a/modules/game/src/game.c +++ b/modules/game/src/game.c @@ -6,14 +6,13 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <stdbool.h> #include <stdlib.h> #include <time.h> #include <unistd.h> -/* libalx --------------------------------------------------------------------*/ + #include "libalx/alx_seed.h" -/* Project -------------------------------------------------------------------*/ + #include "save.h" #include "game.h" @@ -93,15 +92,12 @@ void game_action (int action, int row, int col) case GAME_ACT_STEP: game_step(row, col); break; - case GAME_ACT_FLAG: game_flag(row, col); break; - case GAME_ACT_FLAG_POSSIBLE: game_possible(row, col); break; - case GAME_ACT_RM_FLAG: game_rmflag(row, col); break; @@ -188,7 +184,6 @@ static void game_step (int r, int c) case GAME_USR_POSSIBLE: game_discover(r, c); break; - case GAME_USR_CLEAR: game_big_step(r, c); break; @@ -208,12 +203,10 @@ static void game_discover (int r, int c) game_board.usr[r][c] = GAME_USR_CLEAR; game_board.clr++; - if (game_board.clr == safe_fields) { + if (game_board.clr == safe_fields) game_board.state = GAME_STATE_SAFE; - - } else if (game_board.gnd[r][c] == GAME_MINE_NO) { + else if (game_board.gnd[r][c] == GAME_MINE_NO) game_discover_recursive(r, c); - } } } @@ -237,9 +230,8 @@ static void game_big_step (int r, int c) int cnt; cnt = game_count_flags(r, c); - if (cnt && (game_board.gnd[r][c] == cnt)) { + if (cnt && (game_board.gnd[r][c] == cnt)) game_step_recursive(r, c); - } } static int game_count_flags (int r, int c) @@ -253,9 +245,8 @@ static int game_count_flags (int r, int c) for (j = c - 1; j < c + 2; j++) { if (i >= 0 && i < game_board.rows && j >= 0 && j < game_board.cols) { - if (game_board.usr[i][j] == GAME_USR_FLAG) { + if (game_board.usr[i][j] == GAME_USR_FLAG) cnt++; - } } } } @@ -344,9 +335,8 @@ static void game_all_flags (int r, int c) int cnt; cnt = game_count_nclear(r, c); - if (cnt && (game_board.gnd[r][c] == cnt)) { + if (cnt && (game_board.gnd[r][c] == cnt)) game_flag_recursive(r, c); - } } static int game_count_nclear (int r, int c) @@ -360,9 +350,8 @@ static int game_count_nclear (int r, int c) for (j = c - 1; j < c + 2; j++) { if (i >= 0 && i < game_board.rows && j >= 0 && j < game_board.cols) { - if (game_board.usr[i][j] != GAME_USR_CLEAR) { + if (game_board.usr[i][j] != GAME_USR_CLEAR) cnt++; - } } } } diff --git a/modules/game/src/game_iface.c b/modules/game/src/game_iface.c index 4fc602d..a2df636 100644 --- a/modules/game/src/game_iface.c +++ b/modules/game/src/game_iface.c @@ -6,10 +6,9 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <stdbool.h> #include <time.h> -/* Project -------------------------------------------------------------------*/ + #include "player_iface.h" #include "save.h" #include "score.h" @@ -144,23 +143,18 @@ static void game_iface_act (void) case GAME_IFACE_STATE_PLAYING: game_iface_playing_act(); break; - case GAME_IFACE_STATE_CHEATED: game_iface_cheated_act(); break; - case GAME_IFACE_STATE_XYZZY: game_iface_xyzzy_act(); break; - case GAME_IFACE_STATE_PAUSE: game_iface_pause_act(); break; - case GAME_IFACE_STATE_SAFE: game_iface_safe_act(); break; - case GAME_IFACE_STATE_GAMEOVER: game_iface_gameover_act(); break; @@ -174,19 +168,15 @@ static void game_iface_playing_act (void) case GAME_IFACE_ACT_PLAY: game_iface_act_game(); break; - case GAME_IFACE_ACT_PAUSE: game_iface_pause(); break; - case GAME_IFACE_ACT_XYZZY_ON: game_iface_xyzzy_on(); break; - case GAME_IFACE_ACT_SAVE: save_game_file(saved_path); break; - case GAME_IFACE_ACT_QUIT: game_iface_quit(); break; @@ -456,28 +446,25 @@ static void game_iface_update_vis (int r, int c) field_vis = GAME_IFACE_VIS_KBOOM; break; case GAME_USR_HIDDEN: - if (game_board.gnd[r][c] >= GAME_MINE_YES) { + if (game_board.gnd[r][c] >= GAME_MINE_YES) field_vis = GAME_IFACE_VIS_HIDDEN_MINE; - } else { + else field_vis = GAME_IFACE_VIS_HIDDEN_SAFE; - } break; case GAME_USR_CLEAR: field_vis = GAME_IFACE_VIS_0 + game_board.gnd[r][c]; break; case GAME_USR_FLAG: - if (game_board.gnd[r][c] >= GAME_MINE_YES) { + if (game_board.gnd[r][c] >= GAME_MINE_YES) field_vis = GAME_IFACE_VIS_FLAG; - } else { + else field_vis = GAME_IFACE_VIS_FLAG_FALSE; - } break; case GAME_USR_POSSIBLE: - if (game_board.gnd[r][c] >= GAME_MINE_YES) { + if (game_board.gnd[r][c] >= GAME_MINE_YES) field_vis = GAME_IFACE_VIS_POSSIBLE; - } else { + else field_vis = GAME_IFACE_VIS_POSSIBLE_FALSE; - } break; } break; @@ -547,9 +534,8 @@ static void game_iface_clean_in (void) int j; for (i = 0; i < game_board.rows; i++) { - for (j = 0; j < game_board.cols; j++) { + for (j = 0; j < game_board.cols; j++) game_iface_in.act_game[i][j] = GAME_IFACE_GAME_ACT_FOO; - } } game_iface_in.action = GAME_IFACE_ACT_FOO; } diff --git a/modules/menu/src/menu_clui.c b/modules/menu/src/menu_clui.c index 582c90d..d3f8d8b 100644 --- a/modules/menu/src/menu_clui.c +++ b/modules/menu/src/menu_clui.c @@ -6,12 +6,11 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <math.h> #include <stdlib.h> -/* libalx --------------------------------------------------------------------*/ + #include "libalx/io/alx_input.h" -/* Project -------------------------------------------------------------------*/ + #include "about.h" #include "game_iface.h" // #include "save.h" @@ -24,17 +23,17 @@ /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ - # define ROWS_CLUI_MAX (99) +#define ROWS_CLUI_MAX (99) #if (ROWS_CLUI_MAX > ROWS_MAX) -# error "rows max (clui)" + #error "rows max (clui)" #endif - # define COLS_CLUI_MAX (26) +#define COLS_CLUI_MAX (26) #if (COLS_CLUI_MAX > COLS_MAX) -# error "cols max (clui)" + #error "cols max (clui)" #endif - # define BUFF_SIZE (1024) +#define BUFF_SIZE (1024) /****************************************************************************** @@ -56,12 +55,10 @@ void menu_clui (void) c = 'n'; printf("Read 'Disclaimer of warranty'? (yes/NO): "); - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } - if (sscanf(buff, " %c", &c) != 1) { + if (sscanf(buff, " %c", &c) != 1) goto err_sscanf; - } if (c == 'y' || c == 'Y') { printf(" >yes\n"); print_share_file(SHARE_DISCLAIMER); @@ -71,12 +68,10 @@ void menu_clui (void) c = 'n'; printf("Read 'License'? (yes/NO): "); - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } - if (sscanf(buff, " %c", &c) != 1) { + if (sscanf(buff, " %c", &c) != 1) goto err_sscanf; - } if (c == 'y' || c == 'Y') { printf(" >yes\n"); print_share_file(SHARE_LICENSE); @@ -96,12 +91,10 @@ void menu_clui (void) #endif c = 'n'; printf("New game or load game? (NEW/load): "); - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } - if (sscanf(buff, " %c", &c) != 1) { + if (sscanf(buff, " %c", &c) != 1) goto err_sscanf; - } if (c == 'l' || c == 'L') { printf(" >load\n"); menu_clui_load(); @@ -136,12 +129,10 @@ static void menu_clui_rand (void) c = 'n'; printf("Set seed for random generator? (yes/NO): "); - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } - if (sscanf(buff, " %c", &c) != 1) { + if (sscanf(buff, " %c", &c) != 1) goto err_sscanf; - } if (c == 'y' || c == 'Y') { printf(" >yes\n"); seed = alx_getint(-INFINITY, 1, INFINITY, "Seed:", NULL); @@ -152,12 +143,10 @@ static void menu_clui_rand (void) c = 'b'; printf("Level? (BEGINNER/intermediate/(expert)/custom): "); - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } - if (sscanf(buff, " %c", &c) != 1) { + if (sscanf(buff, " %c", &c) != 1) goto err_sscanf; - } if (c == 'i' || c == 'I') { printf(" >intermediate\n"); menu_iface_variables.level = GAME_IFACE_LEVEL_INTERMEDIATE; @@ -166,7 +155,6 @@ static void menu_clui_rand (void) printf(" >expert\n"); menu_iface_variables.level = GAME_IFACE_LEVEL_EXPERT_INV; menu_clui_start(); - } else if (c == 'c' || c == 'C') { printf(" >custom\n"); menu_iface_variables.level = GAME_IFACE_LEVEL_CUSTOM; @@ -221,12 +209,10 @@ static void menu_clui_start (void) c = 'm'; printf("Play again? (MENU/play/exit): "); - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } - if (sscanf(buff, " %c", &c) != 1) { + if (sscanf(buff, " %c", &c) != 1) goto err_sscanf; - } if (c == 'p' || c == 'P') { printf(" >play\n"); menu_clui_start(); diff --git a/modules/menu/src/menu_gui.c b/modules/menu/src/menu_gui.c index fdc40fc..01f89c2 100644 --- a/modules/menu/src/menu_gui.c +++ b/modules/menu/src/menu_gui.c @@ -6,15 +6,14 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <gtk/gtk.h> #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -/* libalx --------------------------------------------------------------------*/ + #include "libalx/io/alx_input.h" -/* Project -------------------------------------------------------------------*/ + #include "about.h" #include "game_iface.h" #include "save.h" @@ -282,21 +281,22 @@ static void callback_entry_dbl (GtkWidget *widget, { struct Entry_dbl_Data *entry; const char *str; - int err; char buff [LINE_SIZE]; entry = ((struct Entry_dbl_Data *)data); str = gtk_entry_get_text(GTK_ENTRY(entry->ptr)); - err = alx_sscan_dbl(entry->num, entry->min, entry->def, entry->max, str); + if (alx_sscan_dbl(entry->num, entry->min, entry->def, entry->max, str)) + goto err; - if (err) { - (void)snprintf(buff, LINE_SIZE, "Error %i", err); - gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff); - gtk_editable_select_region(GTK_EDITABLE(entry->ptr), - 0, GTK_ENTRY(entry->ptr)->text_length); - } + gtk_main_quit(); + return; +err: + (void)snprintf(buff, LINE_SIZE, "Error %i", err); + gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff); + gtk_editable_select_region(GTK_EDITABLE(entry->ptr), + 0, GTK_ENTRY(entry->ptr)->text_length); gtk_main_quit(); } @@ -305,25 +305,25 @@ static void callback_entry_int (GtkWidget *widget, { struct Entry_int_Data *entry; const char *str; - int err; int64_t Z; char buff [LINE_SIZE]; entry = ((struct Entry_int_Data *)data); str = gtk_entry_get_text(GTK_ENTRY(entry->ptr)); - err = alx_sscan_i64(&Z, entry->min, entry->def, entry->max, str); - - if (err) { - (void)snprintf(buff, LINE_SIZE, "Error %i", err); - gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff); - gtk_editable_select_region(GTK_EDITABLE(entry->ptr), - 0, GTK_ENTRY(entry->ptr)->text_length); - } else { - *(entry->num) = Z; - } + if (alx_sscan_i64(&Z, entry->min, entry->def, entry->max, str)) + goto err; + *(entry->num) = Z; gtk_main_quit(); + return; + +err: + (void)snprintf(buff, LINE_SIZE, "Error %i", err); + gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff); + gtk_editable_select_region(GTK_EDITABLE(entry->ptr), + 0, GTK_ENTRY(entry->ptr)->text_length); + gtk_main_quit(); } static void callback_entry_fname (GtkWidget *widget, @@ -331,21 +331,22 @@ static void callback_entry_fname (GtkWidget *widget, { struct Entry_fname_Data *entry; const char *str; - int err; char buff [LINE_SIZE]; entry = ((struct Entry_fname_Data *)data); str = gtk_entry_get_text(GTK_ENTRY(entry->ptr)); - err = alx_sscan_fname(entry->fpath, entry->fname, true, str); + if (alx_sscan_fname(entry->fpath, entry->fname, true, str)) + goto err; - if (err) { - snprintf(buff, LINE_SIZE, "Error %i", err); - gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff); - gtk_editable_select_region(GTK_EDITABLE(entry->ptr), - 0, GTK_ENTRY(entry->ptr)->text_length); - } + gtk_main_quit(); + return; +err: + (void)snprintf(buff, LINE_SIZE, "Error %i", err); + gtk_entry_set_text(GTK_ENTRY(entry->ptr), buff); + gtk_editable_select_region(GTK_EDITABLE(entry->ptr), + 0, GTK_ENTRY(entry->ptr)->text_length); gtk_main_quit(); } diff --git a/modules/menu/src/menu_iface.c b/modules/menu/src/menu_iface.c index 92e0351..3927ca7 100644 --- a/modules/menu/src/menu_iface.c +++ b/modules/menu/src/menu_iface.c @@ -44,10 +44,8 @@ void menu_iface_init_iface (void) switch (menu_iface_mode) { case MENU_IFACE_CLUI: break; - case MENU_IFACE_TUI: break; - case MENU_IFACE_GUI: menu_gui_init(); break; @@ -60,10 +58,8 @@ void menu_iface_cleanup (void) switch (menu_iface_mode) { case MENU_IFACE_CLUI: break; - case MENU_IFACE_TUI: break; - case MENU_IFACE_GUI: menu_gui_cleanup(); break; @@ -82,33 +78,28 @@ void menu_iface_board (int *level, int *rows, int *cols, int *mines) *cols = GAME_IFACE_LEVEL_BEGINNER_COLS; *mines = GAME_IFACE_LEVEL_BEGINNER_MINES; break; - case GAME_IFACE_LEVEL_INTERMEDIATE: *rows = GAME_IFACE_LEVEL_INTERMEDIATE_ROWS; *cols = GAME_IFACE_LEVEL_INTERMEDIATE_COLS; *mines = GAME_IFACE_LEVEL_INTERMEDIATE_MINES; break; - case GAME_IFACE_LEVEL_EXPERT: *rows = GAME_IFACE_LEVEL_EXPERT_ROWS; *cols = GAME_IFACE_LEVEL_EXPERT_COLS; *mines = GAME_IFACE_LEVEL_EXPERT_MINES; break; - case GAME_IFACE_LEVEL_EXPERT_INV: *rows = GAME_IFACE_LEVEL_EXPERT_COLS; *cols = GAME_IFACE_LEVEL_EXPERT_ROWS; *mines = GAME_IFACE_LEVEL_EXPERT_MINES; break; - case GAME_IFACE_LEVEL_CUSTOM: *rows = menu_iface_variables.rows; *cols = menu_iface_variables.cols; *mines = menu_iface_variables.p * (*rows) * (*cols); /* at least one safe field */ - if ((*mines) == (*rows) * (*cols)) { + if ((*mines) == (*rows) * (*cols)) (*mines)--; - } break; } } @@ -118,23 +109,21 @@ void menu_iface (void) start_mode = START_RAND; - if (!flag_exit) { - switch (menu_iface_mode) { - case MENU_IFACE_FOO: - break; - - case MENU_IFACE_CLUI: - menu_clui(); - break; + if (flag_exit) + return; - case MENU_IFACE_TUI: - menu_tui(); - break; - - case MENU_IFACE_GUI: - menu_gui(); - break; - } + switch (menu_iface_mode) { + case MENU_IFACE_FOO: + break; + case MENU_IFACE_CLUI: + menu_clui(); + break; + case MENU_IFACE_TUI: + menu_tui(); + break; + case MENU_IFACE_GUI: + menu_gui(); + break; } } diff --git a/modules/menu/src/menu_tui.c b/modules/menu/src/menu_tui.c index d864ffc..a39caea 100644 --- a/modules/menu/src/menu_tui.c +++ b/modules/menu/src/menu_tui.c @@ -6,14 +6,13 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <math.h> #include <ncurses.h> #include <stdbool.h> #include <stdlib.h> -/* libalx --------------------------------------------------------------------*/ + #include "libalx/curses/alx_ncur.h" -/* Project -------------------------------------------------------------------*/ + #include "about.h" #include "game_iface.h" #include "save.h" @@ -27,17 +26,17 @@ /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ - # define ROWS_TUI_MAX (22) +#define ROWS_TUI_MAX (22) #if (ROWS_TUI_MAX > ROWS_MAX) -# error "rows max (tui)" + #error "rows max (tui)" #endif - # define COLS_TUI_MAX (33) +#define COLS_TUI_MAX (33) #if (COLS_TUI_MAX > COLS_MAX) -# error "cols max (tui)" + #error "cols max (tui)" #endif - # define BUFF_SIZE_TEXT (1048576) +#define BUFF_SIZE_TEXT (1048576) /****************************************************************************** @@ -149,7 +148,7 @@ static void menu_tui_continue (void) /* Input box */ w2 = w - 8; r2 = r + h - 5; - static const char *txt[] = {"File name:"}; + static const char *const txt[] = {"File name:"}; /* Menu */ wh = true; @@ -318,7 +317,7 @@ static void menu_tui_custom (void) /* Input box */ w2 = w - 8; r2 = r + h - 5; - static const char *txt[] = { + static const char *const txt[] = { "Rows:", "Columns:", "Proportion:" @@ -391,7 +390,7 @@ static void menu_tui_devel (void) /* Input box */ w2 = w - 8; r2 = r + h - 5; - static const char *txt[] = {"Seed:"}; + static const char *const txt[] = {"Seed:"}; /* Menu */ win = newwin(h, w, r, c); diff --git a/modules/menu/src/parser.c b/modules/menu/src/parser.c index f6151be..cff49e9 100644 --- a/modules/menu/src/parser.c +++ b/modules/menu/src/parser.c @@ -133,7 +133,8 @@ static void parse_rows (char *argument) { menu_iface_variables.rows = atoi(argument); - if (menu_iface_variables.rows < 2 || menu_iface_variables.rows > ROWS_MAX) { + if ((menu_iface_variables.rows < 2) || + (menu_iface_variables.rows > ROWS_MAX)) { printf("--rows argument not valid\n"); printf("It must be an integer [%i U %i]\n", 2, ROWS_MAX); exit(EXIT_FAILURE); @@ -144,7 +145,8 @@ static void parse_columns (char *argument) { menu_iface_variables.cols = atoi(argument); - if (menu_iface_variables.cols < 2 || menu_iface_variables.cols > COLS_MAX) { + if ((menu_iface_variables.cols < 2) || + (menu_iface_variables.cols > COLS_MAX)) { printf("--columns argument not valid\n"); printf("It must be an integer [%i U %i]\n", 2, COLS_MAX); exit(EXIT_FAILURE); @@ -157,16 +159,19 @@ static void parse_file (char *argument) // FIXME fp = fopen(argument, "r"); - if (!fp) { - printf("--file argument not valid\n"); - printf("It must be a valid file name (relative to saved dir)\n"); - exit(EXIT_FAILURE); - } else { - fclose(fp); + if (!fp) + goto err_fp; + fclose(fp); - saved_path[0] = '\0'; - snprintf(saved_name, FILENAME_MAX, argument); - } + saved_path[0] = '\0'; + snprintf(saved_name, FILENAME_MAX, argument); + + return; + +err_fp: + printf("--file argument not valid\n"); + printf("It must be a valid file name (relative to saved dir)\n"); + exit(EXIT_FAILURE); } static void parse_iface (char *argument) @@ -174,9 +179,11 @@ static void parse_iface (char *argument) menu_iface_mode = atoi(argument); player_iface_mode = menu_iface_mode; - if (menu_iface_mode < MENU_IFACE_CLUI || menu_iface_mode > MENU_IFACE_GUI) { + if ((menu_iface_mode < MENU_IFACE_CLUI) || + (menu_iface_mode > MENU_IFACE_GUI)) { printf("--iface argument not valid\n"); - printf("It must be an integer [%i U %i]\n", MENU_IFACE_CLUI, MENU_IFACE_GUI); + printf("It must be an integer [%i U %i]\n", + MENU_IFACE_CLUI, MENU_IFACE_GUI); exit(EXIT_FAILURE); } } @@ -185,7 +192,8 @@ static void parse_proportion (char *argument) { menu_iface_variables.p = atof(argument); - if (menu_iface_variables.p < 0 || menu_iface_variables.p > 1) { + if ((menu_iface_variables.p < 0) || + (menu_iface_variables.p > 1)) { printf("--proportion argument not valid\n"); printf("It must be a real [0 U 1]\n"); exit(EXIT_FAILURE); @@ -204,9 +212,10 @@ static void parse_start (char *argument) { start_mode = atoi(argument); - if (start_mode < START_FOO || start_mode > START_LOAD) { + if ((start_mode < START_FOO) || (start_mode > START_LOAD)) { printf("--start argument not valid\n"); - printf("It must be an integer [%i U %i]\n", START_FOO, START_LOAD); + printf("It must be an integer [%i U %i]\n", + START_FOO, START_LOAD); exit(EXIT_FAILURE); } } diff --git a/modules/player/src/player_clui.c b/modules/player/src/player_clui.c index 9805cc2..7ff3e8c 100644 --- a/modules/player/src/player_clui.c +++ b/modules/player/src/player_clui.c @@ -6,10 +6,9 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <stdbool.h> #include <stdio.h> -/* Project -------------------------------------------------------------------*/ + #include "game_iface.h" #include "player_iface.h" @@ -126,12 +125,10 @@ static void show_board_start(const struct Player_Iface_Position *position, const char *title, const char *subtitle) { - printf("________________________________________" - "________________________________________\n"); + printf("________________________________________________________________________________\n"); board_loop_start(position); printf("%s - %s\n", subtitle, title); - printf("----------------------------------------" - "----------------------------------------\n"); + printf("--------------------------------------------------------------------------------\n"); } static void board_loop_start(const struct Player_Iface_Position *position) @@ -169,12 +166,10 @@ static void show_board (const struct Game_Iface_Out *board, const char *title, const char *subtitle) { - printf("________________________________________" - "________________________________________\n"); + printf("________________________________________________________________________________\n"); board_loop(board, position); printf("%s - %s\n", subtitle, title); - printf("----------------------------------------" - "----------------------------------------\n"); + printf("--------------------------------------------------------------------------------\n"); } @@ -214,75 +209,57 @@ static char set_char (int game_iface_visible) case GAME_IFACE_VIS_KBOOM: c = PLAYER_CLUI_CHAR_KBOOM; break; - case GAME_IFACE_VIS_HIDDEN_FIELD: c = PLAYER_CLUI_CHAR_HIDDEN_FIELD; break; - case GAME_IFACE_VIS_HIDDEN_MINE: c = PLAYER_CLUI_CHAR_HIDDEN_MINE; break; - case GAME_IFACE_VIS_HIDDEN_SAFE: c = PLAYER_CLUI_CHAR_HIDDEN_SAFE; break; - case GAME_IFACE_VIS_SAFE_MINE: c = PLAYER_CLUI_CHAR_SAFE_MINE; break; - case GAME_IFACE_VIS_0: c = PLAYER_CLUI_CHAR_0; break; - case GAME_IFACE_VIS_1: c = PLAYER_CLUI_CHAR_1; break; - case GAME_IFACE_VIS_2: c = PLAYER_CLUI_CHAR_2; break; - case GAME_IFACE_VIS_3: c = PLAYER_CLUI_CHAR_3; break; - case GAME_IFACE_VIS_4: c = PLAYER_CLUI_CHAR_4; break; - case GAME_IFACE_VIS_5: c = PLAYER_CLUI_CHAR_5; break; - case GAME_IFACE_VIS_6: c = PLAYER_CLUI_CHAR_6; break; - case GAME_IFACE_VIS_7: c = PLAYER_CLUI_CHAR_7; break; - case GAME_IFACE_VIS_8: c = PLAYER_CLUI_CHAR_8; break; - case GAME_IFACE_VIS_FLAG: c = PLAYER_CLUI_CHAR_FLAG; break; - case GAME_IFACE_VIS_FLAG_FALSE: c = PLAYER_CLUI_CHAR_FLAG_FALSE; break; - case GAME_IFACE_VIS_POSSIBLE: c = PLAYER_CLUI_CHAR_POSSIBLE; break; - case GAME_IFACE_VIS_POSSIBLE_FALSE: c = PLAYER_CLUI_CHAR_POSSIBLE_FALSE; break; - default: c = PLAYER_CLUI_CHAR_ERROR; break; @@ -306,29 +283,25 @@ static int usr_input (void) /* Wait for input */ buff[0] = '\0'; c = '\0'; - if (!fgets(buff, BUFF_SIZE, stdin)) { + if (!fgets(buff, BUFF_SIZE, stdin)) goto err_fgets; - } p = buff; /* Interpret input */ - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; switch (c) { /* Escape sequence */ case 27: /* Arrows */ - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; switch (c) { case 91: - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; switch (c) { case 65: @@ -395,28 +368,23 @@ static int usr_input (void) case 'x': /* Special sequence "xyzzy" */ - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; if (c == 'y') { - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; if (c == 'z') { - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; if (c == 'z') { - if (sscanf(p, "%c", &c) != 1) { + if (sscanf(p, "%c", &c) != 1) goto err_sscanf; - } p++; - if (c == 'y') { + if (c == 'y') action = PLAYER_IFACE_ACT_XYZZY_ON; - } } } } @@ -446,7 +414,6 @@ static int usr_input (void) err_sscanf: err_fgets: - return action; } @@ -460,23 +427,18 @@ static void show_help (const struct Game_Iface_Out *board) case GAME_IFACE_STATE_PLAYING: show_help_play(); break; - case GAME_IFACE_STATE_PAUSE: show_help_pause(); break; - case GAME_IFACE_STATE_XYZZY: show_help_xyzzy(); break; - case GAME_IFACE_STATE_CHEATED: show_help_cheat(); break; - case GAME_IFACE_STATE_SAFE: show_help_safe(); break; - case GAME_IFACE_STATE_GAMEOVER: show_help_gameover(); break; @@ -486,65 +448,50 @@ static void show_help (const struct Game_Iface_Out *board) static void show_help_start (void) { - printf("Move " "|Step " "|Quit " "|Confirm\n"); - printf(" %c%c%c%c %c%c%c%c| %c ""| %c ""| Enter\n", - 'h','j','k','l', - '<','v','^','>', - '+', 'q'); + printf("Move |Step |Quit |Confirm\n"); + printf(" hjkl <v^>| + | q | Enter\n"); } static void show_help_play (void) { - printf("Move " "|Step " "|Flag |? " "|Remove |Pause " "|Save " "|Quit " "|Confirm\n"); - printf(" %c%c%c%c %c%c%c%c| %c ""| Space| %c""| BS | %c ""| %c ""| %c ""| Enter\n", - 'h','j','k','l', - '<','v','^','>', - '+', 'f', 'p', 's', 'q'); + printf("Move |Step |Flag |? |Remove |Pause |Save |Quit |Confirm\n"); + printf(" hjkl <v^>| + | Space| f| BS | p | s | q | Enter\n"); } static void show_help_pause (void) { - printf("Continue " "|Save " "|Quit " "|Confirm\n"); - printf(" %c ""| %c ""| %c ""| Enter", - 'p', 's', 'q'); + printf("Continue |Save |Quit |Confirm\n"); + printf(" p | s | q | Enter"); } static void show_help_xyzzy (void) { - printf("XYZZY |Move " "|Step " "|Flag |? " "|Remove |Save " "|Quit " "|Confirm\n"); - printf(" 0 1 2| %c%c%c%c %c%c%c%c| %c ""| Space| %c""| BS | %c ""| %c ""| Enter\n", - 'h','j','k','l', - '<','v','^','>', - '+', 'f', 's', 'q'); + printf("XYZZY |Move |Step |Flag |? |Remove |Save |Quit |Confirm\n"); + printf(" 0 1 2| hjkl <v^>| + | Space| f| BS | s | q | Enter\n"); } static void show_help_cheat (void) { - printf("Move " "|Step " "|Flag |? " "|Remove |Save " "|Quit " "|Confirm\n"); - printf(" %c%c%c%c %c%c%c%c| %c ""| Space| %c""| BS | %c ""| %c ""| Enter\n", - 'h','j','k','l', - '<','v','^','>', - '+', 'f', 's', 'q'); + printf("Move |Step |Flag |? |Remove |Save |Quit |Confirm\n"); + printf(" hjkl <v^>| + | Space| f| BS | s | q | Enter\n"); } static void show_help_safe (void) { - printf("Save " "|Quit " "|Confirm\n"); - printf(" %c ""| %c ""| Enter\n", - 's', 'q'); + printf("Save |Quit |Confirm\n"); + printf(" s | q | Enter\n"); } static void show_help_gameover (void) { - printf("Quit " "|Confirm\n"); - printf(" %c ""| Enter\n", - 'q'); + printf("Quit |Confirm\n"); + printf(" q | Enter\n"); } diff --git a/modules/player/src/player_gui.c b/modules/player/src/player_gui.c index 9403384..fcbad71 100644 --- a/modules/player/src/player_gui.c +++ b/modules/player/src/player_gui.c @@ -6,15 +6,14 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <math.h> #include <stdbool.h> #include <stdio.h> -/* Packages ------------------------------------------------------------------*/ + #include <gtk/gtk.h> -/* libalx ------------------------------------------------------------------*/ + #include "libalx/io/alx_input.h" -/* Project -------------------------------------------------------------------*/ + #include "game_iface.h" #include "menu_gui.h" #include "player_iface.h" @@ -459,9 +458,8 @@ static void show_board (const struct Game_Iface_Out *board, gtk_label_set_text(GTK_LABEL(label_stit.ptr), label_stit.text); /* Timeout */ - if (board->state == GAME_IFACE_STATE_PLAYING) { + if (board->state == GAME_IFACE_STATE_PLAYING) timeout.id = g_timeout_add_seconds(1, callback_timeout, (void *)&timeout); - } /* Refresh */ gtk_widget_show_all(box_board); @@ -649,23 +647,18 @@ static void show_help (const struct Game_Iface_Out *board) case GAME_IFACE_STATE_PLAYING: show_help_play(); break; - case GAME_IFACE_STATE_PAUSE: show_help_pause(); break; - case GAME_IFACE_STATE_XYZZY: show_help_xyzzy(); break; - case GAME_IFACE_STATE_CHEATED: show_help_cheat(); break; - case GAME_IFACE_STATE_SAFE: show_help_safe(); break; - case GAME_IFACE_STATE_GAMEOVER: show_help_gameover(); break; @@ -808,9 +801,8 @@ static gboolean callback_field (GtkWidget *widget, { struct Field_Data *field_ij; - if (state == GAME_IFACE_STATE_PLAYING && timeout.id) { + if (state == GAME_IFACE_STATE_PLAYING && timeout.id) g_source_remove(timeout.id); - } field_ij = ((struct Field_Data *)data); *(field_ij->row) = field_ij->r; @@ -819,16 +811,14 @@ static gboolean callback_field (GtkWidget *widget, switch (event->button) { case 1: //1 is left mouse btn - if (event->type == GDK_BUTTON_PRESS) { + if (event->type == GDK_BUTTON_PRESS) *(field_ij->act) = PLAYER_IFACE_ACT_STEP; - } break; case 3: //3 is right mouse btn - if (event->type == GDK_BUTTON_PRESS) { + if (event->type == GDK_BUTTON_PRESS) *(field_ij->act) = PLAYER_IFACE_ACT_FLAG; - } break; } @@ -843,19 +833,18 @@ static gboolean callback_ebox (GtkWidget *widget, { struct EBox_Data *ebox; - if ((state == GAME_IFACE_STATE_PLAYING) && timeout.id) { + if ((state == GAME_IFACE_STATE_PLAYING) && timeout.id) g_source_remove(timeout.id); - } ebox = ((struct EBox_Data *)data); switch (event->button) { case 1: //1 is left mouse btn - if (event->type == GDK_BUTTON_PRESS) { + if (event->type == GDK_BUTTON_PRESS) *(ebox->act) = ebox->val; - } else if (event->type == GDK_2BUTTON_PRESS) { - } + else if (event->type == GDK_2BUTTON_PRESS) + ; /* TODO */ break; case 3: @@ -872,9 +861,8 @@ static void callback_button (GtkWidget *widget, void *data) { struct Button_Data *button; - if ((state == GAME_IFACE_STATE_PLAYING) && timeout.id) { + if ((state == GAME_IFACE_STATE_PLAYING) && timeout.id) g_source_remove(timeout.id); - } button = ((struct Button_Data *)data); *(button->act) = button->val; @@ -887,9 +875,8 @@ static gboolean callback_tbutton (GtkWidget *widget, void *data) { struct TButton_Data *tbutton; - if ((state == GAME_IFACE_STATE_PLAYING) && timeout.id) { + if ((state == GAME_IFACE_STATE_PLAYING) && timeout.id) g_source_remove(timeout.id); - } tbutton = ((struct TButton_Data *)data); diff --git a/modules/player/src/player_iface.c b/modules/player/src/player_iface.c index a454f82..537afb0 100644 --- a/modules/player/src/player_iface.c +++ b/modules/player/src/player_iface.c @@ -6,11 +6,9 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <stdio.h> -/* Project -------------------------------------------------------------------*/ - #include "game_iface.h" + #include "game_iface.h" #include "player_clui.h" #include "player_tui.h" #include "player_gui.h" @@ -28,7 +26,7 @@ ******* variables ************************************************************ ******************************************************************************/ /* Global --------------------------------------------------------------------*/ -int player_iface_mode; + int player_iface_mode; /* Static --------------------------------------------------------------------*/ static struct Player_Iface_Position player_iface_position; static int player_action; @@ -137,6 +135,10 @@ void player_iface (const struct Game_Iface_Out *out, struct Game_Iface_In *in) { char title[TITLE_SIZE]; + char subtitle[TITLE_SIZE]; + int hours; + int mins; + int secs; /* Title */ switch (out->state) { @@ -157,10 +159,6 @@ void player_iface (const struct Game_Iface_Out *out, break; } /* Subtitle */ - char subtitle[TITLE_SIZE]; - int hours; - int mins; - int secs; if (score->time != CHEATED) { hours = ((int)score->time / 3600); mins = (((int)score->time % 3600) / 60); @@ -183,12 +181,10 @@ void player_iface (const struct Game_Iface_Out *out, player_clui(out, &player_iface_position, title, subtitle, &player_action); break; - case PLAYER_IFACE_TUI: player_tui(out, &player_iface_position, title, subtitle, &player_action); break; - case PLAYER_IFACE_GUI: player_gui(out, &player_iface_position, title, subtitle); break; @@ -199,15 +195,14 @@ void player_iface (const struct Game_Iface_Out *out, void player_iface_save_name (const char *fpath, char *fname, int destsize) { + switch (player_iface_mode) { case PLAYER_IFACE_CLUI: player_clui_save_name(fpath, fname, destsize); break; - case PLAYER_IFACE_TUI: player_tui_save_name(fpath, fname, destsize); break; - case PLAYER_IFACE_GUI: player_gui_save_name(fpath, fname, destsize); break; @@ -216,15 +211,14 @@ void player_iface_save_name (const char *fpath, char *fname, int destsize) void player_iface_score_name (char *player_name, int destsize) { + switch (player_iface_mode) { case PLAYER_IFACE_CLUI: player_clui_score_name(player_name, destsize); break; - case PLAYER_IFACE_TUI: player_tui_score_name(player_name, destsize); break; - case PLAYER_IFACE_GUI: player_gui_score_name(player_name, destsize); break; @@ -233,14 +227,13 @@ void player_iface_score_name (char *player_name, int destsize) void player_iface_cleanup (void) { + switch (player_iface_mode) { case PLAYER_IFACE_CLUI: break; - case PLAYER_IFACE_TUI: player_tui_cleanup(); break; - case PLAYER_IFACE_GUI: player_gui_cleanup(); break; @@ -257,6 +250,7 @@ void player_iface_cleanup (void) * * * * * * * * * */ static void player_iface_act (struct Game_Iface_In *in) { + switch (player_action) { case PLAYER_IFACE_ACT_STEP: case PLAYER_IFACE_ACT_FLAG: @@ -264,7 +258,6 @@ static void player_iface_act (struct Game_Iface_In *in) case PLAYER_IFACE_ACT_RM_FLAG: player_iface_act_play(in); break; - case PLAYER_IFACE_ACT_PAUSE: case PLAYER_IFACE_ACT_SAVE: case PLAYER_IFACE_ACT_XYZZY_ON: @@ -275,23 +268,18 @@ static void player_iface_act (struct Game_Iface_In *in) case PLAYER_IFACE_ACT_QUIT: player_iface_act_game(in); break; - case PLAYER_IFACE_ACT_MOVE_UP: player_iface_move_up(); break; - case PLAYER_IFACE_ACT_MOVE_DOWN: player_iface_move_down(); break; - case PLAYER_IFACE_ACT_MOVE_RIGHT: player_iface_move_right(); break; - case PLAYER_IFACE_ACT_MOVE_LEFT: player_iface_move_left(); break; - case PLAYER_IFACE_ACT_HIGHLIGHT: highlight_cursor(); break; @@ -300,27 +288,23 @@ static void player_iface_act (struct Game_Iface_In *in) static void player_iface_act_start (void) { + switch (player_action) { case PLAYER_IFACE_ACT_STEP: case PLAYER_IFACE_ACT_QUIT: break; - case PLAYER_IFACE_ACT_MOVE_UP: player_iface_move_up(); break; - case PLAYER_IFACE_ACT_MOVE_DOWN: player_iface_move_down(); break; - case PLAYER_IFACE_ACT_MOVE_RIGHT: player_iface_move_right(); break; - case PLAYER_IFACE_ACT_MOVE_LEFT: player_iface_move_left(); break; - case PLAYER_IFACE_ACT_HIGHLIGHT: highlight_cursor(); break; @@ -332,25 +316,21 @@ static void player_iface_act_start (void) * * * * * * * * * */ static void player_iface_act_play (struct Game_Iface_In *in) { + const int r = player_iface_position.row; + const int c = player_iface_position.col; + switch (player_action) { case PLAYER_IFACE_ACT_STEP: - in->act_game[player_iface_position.row][player_iface_position.col] = - GAME_IFACE_GAME_ACT_STEP; + in->act_game[r][c] = GAME_IFACE_GAME_ACT_STEP; break; - case PLAYER_IFACE_ACT_FLAG: - in->act_game[player_iface_position.row][player_iface_position.col] = - GAME_IFACE_GAME_ACT_FLAG; + in->act_game[r][c] = GAME_IFACE_GAME_ACT_FLAG; break; - case PLAYER_IFACE_ACT_FLAG_POSSIBLE: - in->act_game[player_iface_position.row][player_iface_position.col] = - GAME_IFACE_GAME_ACT_FLAG_POSSIBLE; + in->act_game[r][c] = GAME_IFACE_GAME_ACT_FLAG_POSSIBLE; break; - case PLAYER_IFACE_ACT_RM_FLAG: - in->act_game[player_iface_position.row][player_iface_position.col] = - GAME_IFACE_GAME_ACT_RM_FLAG; + in->act_game[r][c] = GAME_IFACE_GAME_ACT_RM_FLAG; break; } @@ -359,35 +339,29 @@ static void player_iface_act_play (struct Game_Iface_In *in) static void player_iface_act_game (struct Game_Iface_In *in) { + switch (player_action) { case PLAYER_IFACE_ACT_PAUSE: in->action = GAME_IFACE_ACT_PAUSE; break; - case PLAYER_IFACE_ACT_SAVE: in->action = GAME_IFACE_ACT_SAVE; break; - case PLAYER_IFACE_ACT_XYZZY_ON: in->action = GAME_IFACE_ACT_XYZZY_ON; break; - case PLAYER_IFACE_ACT_XYZZY_OFF: in->action = GAME_IFACE_ACT_XYZZY_OFF; break; - case PLAYER_IFACE_ACT_XYZZY_LIN: in->action = GAME_IFACE_ACT_XYZZY_LIN; break; - case PLAYER_IFACE_ACT_XYZZY_P: in->action = GAME_IFACE_ACT_XYZZY_P; break; - case PLAYER_IFACE_ACT_XYZZY_NP: in->action = GAME_IFACE_ACT_XYZZY_NP; break; - case PLAYER_IFACE_ACT_QUIT: in->action = GAME_IFACE_ACT_QUIT; break; @@ -399,42 +373,43 @@ static void player_iface_act_game (struct Game_Iface_In *in) * * * * * * * * * */ static void player_iface_move_up (void) { - if (player_iface_position.row) { + + if (player_iface_position.row) (player_iface_position.row)--; - } else { + else player_iface_position.row = player_iface_position.rows - 1; - } } static void player_iface_move_down (void) { - if (player_iface_position.row != player_iface_position.rows - 1) { + + if (player_iface_position.row != player_iface_position.rows - 1) (player_iface_position.row)++; - } else { + else player_iface_position.row = 0; - } } static void player_iface_move_right (void) { - if (player_iface_position.col != player_iface_position.cols - 1) { + + if (player_iface_position.col != player_iface_position.cols - 1) (player_iface_position.col)++; - } else { + else player_iface_position.col = 0; - } } static void player_iface_move_left (void) { - if (player_iface_position.col) { + + if (player_iface_position.col) (player_iface_position.col)--; - } else { + else player_iface_position.col = player_iface_position.cols - 1; - } } static void highlight_cursor (void) { + player_iface_position.highlight = !player_iface_position.highlight; } diff --git a/modules/player/src/player_tui.c b/modules/player/src/player_tui.c index 86e91f2..37dccfb 100644 --- a/modules/player/src/player_tui.c +++ b/modules/player/src/player_tui.c @@ -6,11 +6,10 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <ncurses.h> -/* libalx ------------------------------------------------------------------*/ + #include "libalx/curses/alx_ncur.h" -/* Project -------------------------------------------------------------------*/ + #include "game_iface.h" #include "player_iface.h" @@ -28,7 +27,7 @@ ******* variables ************************************************************ ******************************************************************************/ /* Global --------------------------------------------------------------------*/ -bool flag_color; + bool flag_color; /* Static --------------------------------------------------------------------*/ static WINDOW *win_board; static WINDOW *win_help; @@ -232,9 +231,8 @@ static void board_loop_start(const struct Player_Iface_Position *position) } /* Highlight cursor */ - if (position->highlight) { + if (position->highlight) highlight_cursor(c, position); - } } /* * * * * * * * * * @@ -320,15 +318,13 @@ static void highlight_cursor(int c, l = 2 + 2 * position->col; pair = PAIR_HILITE; - if (flag_color) { + if (flag_color) wattron(win_board, A_BOLD | COLOR_PAIR(pair)); - } mvwaddch(win_board, k, l - 1, '<'); mvwaddch(win_board, k, l, c); mvwaddch(win_board, k, l + 1, '>'); - if (flag_color) { + if (flag_color) wattroff(win_board, A_BOLD | COLOR_PAIR(pair)); - } } static int set_char (int tile) @@ -458,15 +454,13 @@ static void show_char (int row, int col, int c) break; } - if (flag_color) { + if (flag_color) wattron(win_board, A_BOLD | COLOR_PAIR(pair)); - } mvwaddch(win_board, row, col - 1, ' '); mvwaddch(win_board, row, col, c); mvwaddch(win_board, row, col + 1, ' '); - if (flag_color) { + if (flag_color) wattroff(win_board, A_BOLD | COLOR_PAIR(pair)); - } } /* * * * * * * * * * @@ -515,9 +509,8 @@ static int usr_input (void) c = wgetch(win_board); if (c == 'z') { c = wgetch(win_board); - if (c == 'y') { + if (c == 'y') action = PLAYER_IFACE_ACT_XYZZY_ON; - } } } } @@ -572,33 +565,34 @@ static int usr_input (void) * * * * * * * * * */ static void show_help (const struct Game_Iface_Out *board) { - if (last_help != board->state) { - werase(win_help); - - switch (board->state) { - case GAME_IFACE_STATE_PLAYING: - show_help_play(); - break; - case GAME_IFACE_STATE_PAUSE: - show_help_pause(); - break; - case GAME_IFACE_STATE_XYZZY: - show_help_xyzzy(); - break; - case GAME_IFACE_STATE_CHEATED: - show_help_cheat(); - break; - case GAME_IFACE_STATE_SAFE: - show_help_safe(); - break; - case GAME_IFACE_STATE_GAMEOVER: - show_help_gameover(); - break; - } + if (last_help == board->state) { + return; - wrefresh(win_help); - last_help = board->state; + werase(win_help); + + switch (board->state) { + case GAME_IFACE_STATE_PLAYING: + show_help_play(); + break; + case GAME_IFACE_STATE_PAUSE: + show_help_pause(); + break; + case GAME_IFACE_STATE_XYZZY: + show_help_xyzzy(); + break; + case GAME_IFACE_STATE_CHEATED: + show_help_cheat(); + break; + case GAME_IFACE_STATE_SAFE: + show_help_safe(); + break; + case GAME_IFACE_STATE_GAMEOVER: + show_help_gameover(); + break; } + + wrefresh(win_help); + last_help = board->state; } static void show_help_start (void) diff --git a/modules/save/src/save.c b/modules/save/src/save.c index 5e2c647..d1245e0 100644 --- a/modules/save/src/save.c +++ b/modules/save/src/save.c @@ -7,15 +7,14 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -/* Linux ---------------------------------------------------------------------*/ + /* mkdir */ #include <sys/stat.h> -/* Project -------------------------------------------------------------------*/ + #include "game.h" #include "player_iface.h" @@ -125,16 +124,14 @@ void load_game_file (void) fscanf(fp, " gnd"); for (i = 0; i < game_board.rows; i++) { fscanf(fp, " %i", &game_board.gnd[i][0]); - for (j = 1; j < game_board.cols; j++) { + for (j = 1; j < game_board.cols; j++) fscanf(fp, ",%i", &game_board.gnd[i][j]); - } } fscanf(fp, " usr"); for (i = 0; i < game_board.rows; i++) { fscanf(fp, " %i", &game_board.usr[i][0]); - for (j = 1; j < game_board.cols; j++) { + for (j = 1; j < game_board.cols; j++) fscanf(fp, ",%i", &game_board.usr[i][j]); - } } fscanf(fp, " flags %i", &game_board.flags); fscanf(fp, " cleared %i", &game_board.clr); @@ -163,9 +160,8 @@ void save_game_file (char *filepath) bool x; /* Don't change saved_name variable if not in default dir */ - if (filepath != NULL) { + if (filepath) snprintf(old_saved, FILENAME_MAX, "%s", saved_name); - } /* Default path & name */ save_clr(); @@ -223,17 +219,15 @@ void save_game_file (char *filepath) fprintf(fp, "gnd\n"); for (i = 0; i < game_board.rows; i++) { fprintf(fp, "%i", game_board.gnd[i][0]); - for (j = 1; j < game_board.cols; j++) { + for (j = 1; j < game_board.cols; j++) fprintf(fp, ",%i", game_board.gnd[i][j]); - } fprintf(fp, "\n"); } fprintf(fp, "usr\n"); for (i = 0; i < game_board.rows; i++) { fprintf(fp, "%i", game_board.usr[i][0]); - for (j = 1; j < game_board.cols; j++) { + for (j = 1; j < game_board.cols; j++) fprintf(fp, ",%i", game_board.usr[i][j]); - } fprintf(fp, "\n"); } fprintf(fp, "flags %i\n", game_board.flags); @@ -243,9 +237,8 @@ void save_game_file (char *filepath) } /* Don't change saved_name if saving in non-default dir */ - if (filepath != NULL) { + if (filepath) snprintf(saved_name, FILENAME_MAX, "%s", old_saved); - } return; diff --git a/modules/save/src/score.c b/modules/save/src/score.c index 055b8f4..21f7602 100644 --- a/modules/save/src/score.c +++ b/modules/save/src/score.c @@ -81,11 +81,11 @@ void score_init (void) goto err_path; } snprintf(var_hiscores_beginner_name, FILENAME_MAX, "%s", - HISCORES_BEGINNER_NAME); + HISCORES_BEGINNER_NAME); snprintf(var_hiscores_intermediate_name, FILENAME_MAX, "%s", - HISCORES_INTERMEDIATE_NAME); + HISCORES_INTERMEDIATE_NAME); snprintf(var_hiscores_expert_name, FILENAME_MAX, "%s", - HISCORES_EXPERT_NAME); + HISCORES_EXPERT_NAME); return; @@ -102,6 +102,7 @@ void save_score (const struct Game_Iface_Score *game_iface_score) char file_name [FILENAME_MAX]; char player_name [BUFF_SIZE]; FILE *fp; + time_t date_secs; /* File name */ switch (game_iface_score->level) { @@ -134,7 +135,6 @@ void save_score (const struct Game_Iface_Score *game_iface_score) } /* Date & time */ - time_t date_secs; date_secs = time(NULL); date_format = localtime(&date_secs); @@ -142,32 +142,28 @@ void save_score (const struct Game_Iface_Score *game_iface_score) player_iface_score_name(player_name, BUFF_SIZE); /* Write to file (append) */ - errno = 0; + errno = 0; fp = fopen(file_name, "a"); - if (fp) { - fprintf(fp, "\n"); - fprintf(fp, "name %s\n", player_name); - fprintf(fp, "date %i\n", (int)date_secs); - fprintf(fp, "{\n"); - fprintf(fp, " isdst %i\n", date_format->tm_isdst); - fprintf(fp, " yday %i\n", date_format->tm_yday); - fprintf(fp, " wday %i\n", date_format->tm_wday); - fprintf(fp, " year %i\n", date_format->tm_year); - fprintf(fp, " mon %i\n", date_format->tm_mon); - fprintf(fp, " mday %i\n", date_format->tm_mday); - fprintf(fp, " hour %i\n", date_format->tm_hour); - fprintf(fp, " min %i\n", date_format->tm_min); - fprintf(fp, " sec %i\n", date_format->tm_sec); - fprintf(fp, "}\n"); - fprintf(fp, "time %i\n", game_iface_score->time); - fprintf(fp, "clicks %i\n", game_iface_score->clicks); - fprintf(fp, "file %s\n", saved_name); - - fclose(fp); - } else { - printf("errno: %i", errno); - exit(EXIT_FAILURE); - } + if (!fp) + goto err_fp; + fprintf(fp, "\n"); + fprintf(fp, "name %s\n", player_name); + fprintf(fp, "date %i\n", (int)date_secs); + fprintf(fp, "{\n"); + fprintf(fp, " isdst %i\n", date_format->tm_isdst); + fprintf(fp, " yday %i\n", date_format->tm_yday); + fprintf(fp, " wday %i\n", date_format->tm_wday); + fprintf(fp, " year %i\n", date_format->tm_year); + fprintf(fp, " mon %i\n", date_format->tm_mon); + fprintf(fp, " mday %i\n", date_format->tm_mday); + fprintf(fp, " hour %i\n", date_format->tm_hour); + fprintf(fp, " min %i\n", date_format->tm_min); + fprintf(fp, " sec %i\n", date_format->tm_sec); + fprintf(fp, "}\n"); + fprintf(fp, "time %i\n", game_iface_score->time); + fprintf(fp, "clicks %i\n", game_iface_score->clicks); + fprintf(fp, "file %s\n", saved_name); + fclose(fp); return; @@ -175,6 +171,12 @@ void save_score (const struct Game_Iface_Score *game_iface_score) err_path: printf("Path is too large and has been truncated\n"); printf("Score could not be saved!\n"); + return; + +err_fp: + printf("errno: %i", errno); + printf("Score could not be saved!\n"); + return; } void snprint_scores (char *dest, int destsize) @@ -244,63 +246,63 @@ static void snprint_scores_file (char *dest, int destsize, char *file_name) char file [FILENAME_MAX]; fp = fopen(file_name, "r"); - - if (fp) { - /* Title */ - fscanf(fp, "%[^\n]s", title); - /* For some reason, a space after "%[^\n]s" doesn't skip spaces */ - fscanf(fp, " "); + if (!fp) + return; + + /* Title */ + fscanf(fp, "%[^\n]s", title); + /* For some reason, a space after "%[^\n]s" doesn't skip spaces */ + fscanf(fp, " "); + + /* Print */ + (void)snprintf(dest, destsize, + "_______________________________________________________\n" + "%s\n\n" + "name date clicks time file\n\n", + title); + + while ((c = getc(fp)) != EOF) { + ungetc(c, fp); + + /* Read */ + fscanf(fp, "name %s ", name); + fscanf(fp, "date %*i "); + fscanf(fp, "{ "); + fscanf(fp, " isdst %*i "); + fscanf(fp, " yday %*i "); + fscanf(fp, " wday %*i "); + fscanf(fp, " year %i ", &year); + fscanf(fp, " mon %i ", &mon); + fscanf(fp, " mday %i ", &day); + fscanf(fp, " hour %*i "); + fscanf(fp, " min %*i "); + fscanf(fp, " sec %*i "); + fscanf(fp, "} "); + fscanf(fp, "time %i ", &time); + fscanf(fp, "clicks %i ", &clicks); + fscanf(fp, "file %s ", file); + + /* Adjust date & time */ + year += 1900; + hours = (time / 3600); + mins = ((time % 3600) / 60); + secs = (time % 60); /* Print */ - (void)snprintf(dest, destsize, - "_______________________________________________________\n" - "%s\n\n" - "name date clicks time file\n\n", - title); - - while ((c = getc(fp)) != EOF){ - ungetc(c, fp); - - /* Read */ - fscanf(fp, "name %s ", name); - fscanf(fp, "date %*i "); - fscanf(fp, "{ "); - fscanf(fp, " isdst %*i "); - fscanf(fp, " yday %*i "); - fscanf(fp, " wday %*i "); - fscanf(fp, " year %i ", &year); - fscanf(fp, " mon %i ", &mon); - fscanf(fp, " mday %i ", &day); - fscanf(fp, " hour %*i "); - fscanf(fp, " min %*i "); - fscanf(fp, " sec %*i "); - fscanf(fp, "} "); - fscanf(fp, "time %i ", &time); - fscanf(fp, "clicks %i ", &clicks); - fscanf(fp, "file %s ", file); - - /* Adjust date & time */ - year += 1900; - hours = (time / 3600); - mins = ((time % 3600) / 60); - secs = (time % 60); - - /* Print */ - (void)snprintf(tmp, BUFF_SIZE_TEXT, - "%s" - "%s\n" - " %4i/%2i/%2i %i %i:%02i:%02i \t%s\n\n", - dest, - name, - year, 1 + mon, day, - clicks, - hours, mins, secs, - file); - (void)snprintf(dest, BUFF_SIZE_TEXT, "%s", tmp); - } - - fclose(fp); + (void)snprintf(tmp, BUFF_SIZE_TEXT, + "%s" + "%s\n" + " %4i/%2i/%2i %i %i:%02i:%02i \t%s\n\n", + dest, + name, + year, 1 + mon, day, + clicks, + hours, mins, secs, + file); + (void)snprintf(dest, BUFF_SIZE_TEXT, "%s", tmp); } + + fclose(fp); } diff --git a/modules/xyzzy/src/xyzzy.c b/modules/xyzzy/src/xyzzy.c index 6b7ad0a..4e0cb66 100644 --- a/modules/xyzzy/src/xyzzy.c +++ b/modules/xyzzy/src/xyzzy.c @@ -6,9 +6,8 @@ /****************************************************************************** ******* headers ************************************************************** ******************************************************************************/ -/* Standard C ----------------------------------------------------------------*/ #include <stdbool.h> -/* Project -------------------------------------------------------------------*/ + #include "game_iface.h" #include "xyzzy.h" @@ -44,9 +43,8 @@ int xyzzy_lin (const struct Game_Iface_Out *out, struct Game_Iface_In *in) { - if (!x) { + if (!x) x = 1; - } if (step_notflag) { xyzzy_step_all(out, in); @@ -64,9 +62,8 @@ int xyzzy_p (const struct Game_Iface_Out *out, struct Game_Iface_In *in) { - if (!x) { + if (!x) x = (out->rows * out->cols) / 2; - } xyzzy_lin(out, in); @@ -85,9 +82,8 @@ static void xyzzy_step_all (const struct Game_Iface_Out *out, for (i = 0; i < out->rows; i++) { for (j = 0; j < out->cols; j++) { - if (out->usr[i][j] == GAME_IFACE_USR_CLEAR) { + if (out->usr[i][j] == GAME_IFACE_USR_CLEAR) in->act_game[i][j] = GAME_IFACE_GAME_ACT_STEP; - } } } } @@ -100,9 +96,8 @@ static void xyzzy_flag_all (const struct Game_Iface_Out *out, for (i = 0; i < out->rows; i++) { for (j = 0; j < out->cols; j++) { - if (out->usr[i][j] == GAME_IFACE_USR_CLEAR) { + if (out->usr[i][j] == GAME_IFACE_USR_CLEAR) in->act_game[i][j] = GAME_IFACE_GAME_ACT_FLAG; - } } } } @@ -25,8 +25,8 @@ /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ -void init_all (int *argc, char *(*argv[])); -void cleanup (void); +static void init_all (int *argc, char *(*argv[])); +static void cleanup (void); /****************************************************************************** @@ -55,7 +55,7 @@ int main (int argc, char *argv[]) /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ -void init_all (int *argc, char *(*argv[])) +static void init_all (int *argc, char *(*argv[])) { /* Init gtk & curses */ gtk_init_check(argc, argv); @@ -82,7 +82,7 @@ void init_all (int *argc, char *(*argv[])) menu_iface_init_iface(); } -void cleanup (void) +static void cleanup (void) { /* Clean iface */ menu_iface_cleanup(); |