1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-09-01 17:14:15 -04:00

Continue to move code to prepstr() and friends

This commit is contained in:
John Zaitseff 2011-08-15 17:07:00 +10:00
parent 3ad62688da
commit 0c319738a8
2 changed files with 68 additions and 67 deletions

View File

@ -254,7 +254,8 @@ static int ask_number_players (void)
widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]), widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]),
"Enter number of players [^{1^}-^{%d^}] " "Enter number of players [^{1^}-^{%d^}] "
"or ^{<C>^} to continue a game: ", MAX_PLAYERS); "or ^{<C>^} to continue a game: ", MAX_PLAYERS);
maxwidth = MAX(widthbuf[0], widthbuf[1]) + 5; assert(lines == 1 || lines == 2);
maxwidth = ((lines == 1) ? widthbuf[0] : MAX(widthbuf[0], widthbuf[1])) + 5;
newtxwin(lines + 4, maxwidth, 3, WCENTER, true, attr_normal_window); newtxwin(lines + 4, maxwidth, 3, WCENTER, true, attr_normal_window);
pr_left(curwin, 2, 2, chbuf, lines, widthbuf); pr_left(curwin, 2, 2, chbuf, lines, widthbuf);
@ -318,7 +319,8 @@ int ask_game_number (void)
widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]), widthbuf, sizeof(widthbuf) / sizeof(widthbuf[0]),
"Enter game number [^{1^}-^{9^}] " "Enter game number [^{1^}-^{9^}] "
"or ^{<CTRL><C>^} to cancel: "); "or ^{<CTRL><C>^} to cancel: ");
maxwidth = MAX(widthbuf[0], widthbuf[1]) + 5; assert(lines == 1 || lines == 2);
maxwidth = ((lines == 1) ? widthbuf[0] : MAX(widthbuf[0], widthbuf[1])) + 5;
newtxwin(lines + 4, maxwidth, 6, WCENTER, true, attr_normal_window); newtxwin(lines + 4, maxwidth, 6, WCENTER, true, attr_normal_window);
pr_left(curwin, 2, 2, chbuf, lines, widthbuf); pr_left(curwin, 2, 2, chbuf, lines, widthbuf);

View File

@ -208,6 +208,8 @@ void select_moves (void)
selection_t get_move (void) selection_t get_move (void)
{ {
selection_t selection = SEL_NONE; selection_t selection = SEL_NONE;
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
if (quit_selected || abort_game) { if (quit_selected || abort_game) {
@ -231,34 +233,31 @@ selection_t get_move (void)
werase(curwin); werase(curwin);
box(curwin, 0, 0); box(curwin, 0, 0);
wmove(curwin, 2, 2); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
attrpr(curwin, attr_keycode, "<1>"); 1, getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " Display stock portfolio"); "^{<1>^} Display stock portfolio");
pr_left(curwin, 2, 2, chbuf, lines, &width);
wmove(curwin, 3, 2); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
attrpr(curwin, attr_keycode, "<2>"); 1, getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " Declare bankruptcy"); "^{<2>^} Declare bankruptcy");
pr_left(curwin, 3, 2, chbuf, lines, &width);
wmove(curwin, 2, 42); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
attrpr(curwin, attr_keycode, "<3>"); 1, getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " Save and end the game"); "^{<3>^} Save and end the game");
pr_left(curwin, 2, getmaxx(curwin) / 2, chbuf, lines, &width);
wmove(curwin, 3, 42); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
attrpr(curwin, attr_keycode, "<CTRL><C>"); 1, getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " Quit the game"); "^{<CTRL><C>^} Quit the game");
pr_left(curwin, 3, getmaxx(curwin) / 2, chbuf, lines, &width);
mvwaddstr(curwin, 1, 9, "Select move "); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, attr_choice,
waddstr(curwin, "["); 1, getmaxx(curwin) / 2 - 4, &width, 1,
attrpr(curwin, attr_choice, "%c", MOVE_TO_KEY(0)); "Select move [^[%c^]-^[%c^]/^{1^}-^{3^}/^{<CTRL><C>^}]: ",
waddstr(curwin, "-"); MOVE_TO_KEY(0), MOVE_TO_KEY(NUMBER_MOVES - 1));
attrpr(curwin, attr_choice, "%c", MOVE_TO_KEY(NUMBER_MOVES - 1)); pr_right(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
waddstr(curwin, "/");
attrpr(curwin, attr_keycode, "1");
waddstr(curwin, "-");
attrpr(curwin, attr_keycode, "3");
waddstr(curwin, "/");
attrpr(curwin, attr_keycode, "<CTRL><C>");
waddstr(curwin, "]: ");
curs_set(CURS_ON); curs_set(CURS_ON);
wrefresh(curwin); wrefresh(curwin);
@ -271,8 +270,10 @@ selection_t get_move (void)
selection = KEY_TO_MOVE(key); selection = KEY_TO_MOVE(key);
curs_set(CURS_OFF); curs_set(CURS_OFF);
waddstr(curwin, "Move "); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_choice, 0,
attrpr(curwin, attr_choice, "%c", key); 1, getmaxx(curwin) / 2 - 4, &width, 1,
"Move ^{%c^}", key);
pr_left(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
} else { } else {
switch (key) { switch (key) {
case '1': case '1':
@ -285,20 +286,22 @@ selection_t get_move (void)
selection = SEL_BANKRUPT; selection = SEL_BANKRUPT;
curs_set(CURS_OFF); curs_set(CURS_OFF);
wattron(curwin, A_BOLD); lines = prepstr(chbuf, BUFSIZE, attr_normal,
waddstr(curwin, "<2>"); attr_normal | A_BOLD, 0, 1,
wattroff(curwin, A_BOLD); getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " (Declare bankruptcy)"); "^{<2>^} (Declare bankruptcy)");
pr_left(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
break; break;
case '3': case '3':
selection = SEL_SAVE; selection = SEL_SAVE;
curs_set(CURS_OFF); curs_set(CURS_OFF);
wattron(curwin, A_BOLD); lines = prepstr(chbuf, BUFSIZE, attr_normal,
waddstr(curwin, "<3>"); attr_normal | A_BOLD, 0, 1,
wattroff(curwin, A_BOLD); getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " (Save and end the game)"); "^{<3>^} (Save and end the game)");
pr_left(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
break; break;
case KEY_ESC: case KEY_ESC:
@ -310,10 +313,11 @@ selection_t get_move (void)
selection = SEL_QUIT; selection = SEL_QUIT;
curs_set(CURS_OFF); curs_set(CURS_OFF);
wattron(curwin, A_BOLD); lines = prepstr(chbuf, BUFSIZE, attr_normal,
waddstr(curwin, "<CTRL><C>"); attr_normal | A_BOLD, 0, 1,
wattroff(curwin, A_BOLD); getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, " (Quit the game)"); "^{<CTRL><C>^} (Quit the game)");
pr_left(curwin, 1, getmaxx(curwin) / 2, chbuf, lines, &width);
break; break;
default: default:
@ -327,13 +331,10 @@ selection_t get_move (void)
mvwhline(curwin, 3, 2, ' ' | attr_normal, getmaxx(curwin) - 4); mvwhline(curwin, 3, 2, ' ' | attr_normal, getmaxx(curwin) - 4);
// Ask the player to confirm their choice // Ask the player to confirm their choice
wattrset(curwin, attr_normal); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
mvwaddstr(curwin, 2, 22, "Are you sure? "); 1, getmaxx(curwin) / 2 - 4, &width, 1,
waddstr(curwin, "["); "Are you sure? [^{Y^}/^{N^}] ");
attrpr(curwin, attr_keycode, "Y"); pr_right(curwin, 2, getmaxx(curwin) / 2, chbuf, lines, &width);
waddstr(curwin, "/");
attrpr(curwin, attr_keycode, "N");
waddstr(curwin, "] ");
wrefresh(curwin); wrefresh(curwin);
if (! answer_yesno(curwin)) { if (! answer_yesno(curwin)) {
@ -346,9 +347,6 @@ selection_t get_move (void)
if (game_loaded) { if (game_loaded) {
// Save the game to the same game number // Save the game to the same game number
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
lines = prepstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1, lines = prepstr(chbuf, BUFSIZE, attr_status_window, 0, 0, 1,
WIN_COLS - 7, &width, 1, WIN_COLS - 7, &width, 1,
"Saving game %d... ", game_num); "Saving game %d... ", game_num);
@ -360,26 +358,30 @@ selection_t get_move (void)
deltxwin(); deltxwin();
txrefresh(); txrefresh();
free(chbuf);
} }
if (! saved) { if (! saved) {
// Ask which game to save
int key; int key;
bool done; bool done;
int widthbuf[2];
int maxwidth;
// Ask which game to save
newtxwin(6, 54, 8, WCENTER, true, attr_normal_window);
center(curwin, 1, attr_title, " Save Game "); lines = prepstr(chbuf, BUFSIZE, attr_normal, attr_keycode, 0,
mvwaddstr(curwin, 3, 2, "Enter game number "); sizeof(widthbuf) / sizeof(widthbuf[0]),
waddstr(curwin, "["); WIN_COLS - 7, widthbuf,
attrpr(curwin, attr_keycode, "1"); sizeof(widthbuf) / sizeof(widthbuf[0]),
waddstr(curwin, "-"); "Enter game number [^{1^}-^{9^}] "
attrpr(curwin, attr_keycode, "9"); "or ^{<CTRL><C>^} to cancel: ");
waddstr(curwin, "]"); assert(lines == 1 || lines == 2);
waddstr(curwin, " or "); maxwidth = ((lines == 1) ? widthbuf[0] :
attrpr(curwin, attr_keycode, "<CTRL><C>"); MAX(widthbuf[0], widthbuf[1])) + 5;
waddstr(curwin, " to cancel: ");
newtxwin(lines + 4, maxwidth, 8, WCENTER, true,
attr_normal_window);
pr_left(curwin, 2, 2, chbuf, lines, widthbuf);
curs_set(CURS_ON); curs_set(CURS_ON);
wrefresh(curwin); wrefresh(curwin);
@ -413,9 +415,6 @@ selection_t get_move (void)
if (key != KEY_CANCEL) { if (key != KEY_CANCEL) {
// Try to save the game, if possible // Try to save the game, if possible
chtype *chbuf = xmalloc(BUFSIZE * sizeof(chtype));
int lines, width;
game_num = key - '0'; game_num = key - '0';
lines = prepstr(chbuf, BUFSIZE, attr_status_window, lines = prepstr(chbuf, BUFSIZE, attr_status_window,
@ -429,7 +428,6 @@ selection_t get_move (void)
deltxwin(); deltxwin();
txrefresh(); txrefresh();
free(chbuf);
} }
deltxwin(); // "Enter game number" window deltxwin(); // "Enter game number" window
@ -448,6 +446,7 @@ selection_t get_move (void)
} }
} }
free(chbuf);
return selection; return selection;
} }