mirror of
https://git.zap.org.au/git/trader.git
synced 2024-12-04 14:46:45 -05:00
Implement the get_move() function
This commit is contained in:
parent
6a55745d5d
commit
0d4f7cc226
156
src/game.c
156
src/game.c
@ -874,11 +874,163 @@ void select_moves (void)
|
|||||||
quit_selected = false;
|
quit_selected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------
|
||||||
|
Function: get_move - Wait for the player to enter their move
|
||||||
|
Arguments: (none)
|
||||||
|
Returns: (nothing)
|
||||||
|
|
||||||
|
This function displays the galaxy map and the current moves, then waits
|
||||||
|
for the player to select one of the moves. On entry, current_player
|
||||||
|
points to the current player; quit_selected and/or abort_game may be
|
||||||
|
true (if so, get_move() justs returns without waiting for the player to
|
||||||
|
select a move). On exit, selection contains the choice made by the
|
||||||
|
player.
|
||||||
|
*/
|
||||||
|
|
||||||
void get_move (void)
|
void get_move (void)
|
||||||
{
|
{
|
||||||
// @@@ To be written
|
int i, x, y;
|
||||||
show_map(true);
|
|
||||||
|
|
||||||
|
if (quit_selected || abort_game) {
|
||||||
|
selection = SEL_QUIT_GAME;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
selection = SEL_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display map without closing window
|
||||||
|
show_map(false);
|
||||||
|
|
||||||
|
// Display current move choices on the galaxy map
|
||||||
|
for (i = 0; i < NUMBER_MOVES; i++) {
|
||||||
|
mvwaddch(curwin, game_move[i].y + 3, game_move[i].x * 2 + 2,
|
||||||
|
MOVE_TO_KEY(i) | ATTR_MAP_CHOICE);
|
||||||
|
}
|
||||||
|
wrefresh(curwin);
|
||||||
|
|
||||||
|
// Show menu of choices for the player
|
||||||
|
newtxwin(5, 80, LINE_OFFSET + 19, COL_CENTER(80));
|
||||||
|
while (selection == SEL_NONE) {
|
||||||
|
wbkgd(curwin, ATTR_NORMAL_WINDOW);
|
||||||
|
werase(curwin);
|
||||||
|
box(curwin, 0, 0);
|
||||||
|
|
||||||
|
wmove(curwin, 2, 2);
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "<1>");
|
||||||
|
waddstr(curwin, " Display stock portfolio");
|
||||||
|
|
||||||
|
wmove(curwin, 3, 2);
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "<2>");
|
||||||
|
waddstr(curwin, " Declare bankruptcy");
|
||||||
|
|
||||||
|
wmove(curwin, 2, 41);
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "<3>");
|
||||||
|
waddstr(curwin, " Save and end the game");
|
||||||
|
|
||||||
|
wmove(curwin, 3, 40);
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "<ESC>");
|
||||||
|
waddstr(curwin, " Quit the game");
|
||||||
|
|
||||||
|
mvwaddstr(curwin, 1, 2, " Select move ");
|
||||||
|
waddstr(curwin, "[");
|
||||||
|
attrpr(curwin, ATTR_MAP_CHOICE, "%c", MOVE_TO_KEY(0));
|
||||||
|
waddstr(curwin, "-");
|
||||||
|
attrpr(curwin, ATTR_MAP_CHOICE, "%c", MOVE_TO_KEY(NUMBER_MOVES - 1));
|
||||||
|
waddstr(curwin, "/");
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "1");
|
||||||
|
waddstr(curwin, "-");
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "3");
|
||||||
|
waddstr(curwin, "/");
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "<ESC>");
|
||||||
|
waddstr(curwin, "]: ");
|
||||||
|
|
||||||
|
curs_set(CURS_ON);
|
||||||
|
wrefresh(curwin);
|
||||||
|
|
||||||
|
// Get the actual selection made by the player
|
||||||
|
while (selection == SEL_NONE) {
|
||||||
|
int key = tolower(gettxchar(curwin));
|
||||||
|
|
||||||
|
if (IS_MOVE_KEY(key)) {
|
||||||
|
selection = KEY_TO_MOVE(key);
|
||||||
|
|
||||||
|
curs_set(CURS_OFF);
|
||||||
|
waddstr(curwin, "Move ");
|
||||||
|
attrpr(curwin, ATTR_MAP_CHOICE, "%c", key);
|
||||||
|
} else {
|
||||||
|
switch (key) {
|
||||||
|
case '1':
|
||||||
|
curs_set(CURS_OFF);
|
||||||
show_status(current_player);
|
show_status(current_player);
|
||||||
|
curs_set(CURS_ON);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '2':
|
||||||
|
selection = SEL_MAKE_BANKRUPT;
|
||||||
|
|
||||||
|
curs_set(CURS_OFF);
|
||||||
|
wattron(curwin, A_BOLD);
|
||||||
|
waddstr(curwin, "<2>");
|
||||||
|
wattroff(curwin, A_BOLD);
|
||||||
|
waddstr(curwin, " (Declare bankruptcy)");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '3':
|
||||||
|
selection = SEL_SAVE_GAME;
|
||||||
|
|
||||||
|
curs_set(CURS_OFF);
|
||||||
|
wattron(curwin, A_BOLD);
|
||||||
|
waddstr(curwin, "<3>");
|
||||||
|
wattroff(curwin, A_BOLD);
|
||||||
|
waddstr(curwin, " (Save and end the game)");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KEY_ESC:
|
||||||
|
case KEY_CTRL('C'):
|
||||||
|
case KEY_CTRL('\\'):
|
||||||
|
selection = SEL_QUIT_GAME;
|
||||||
|
|
||||||
|
curs_set(CURS_OFF);
|
||||||
|
wattron(curwin, A_BOLD);
|
||||||
|
waddstr(curwin, "<ESC>");
|
||||||
|
wattroff(curwin, A_BOLD);
|
||||||
|
waddstr(curwin, " (Quit the game)");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
beep();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the menu choices
|
||||||
|
wattrset(curwin, ATTR_NORMAL_WINDOW);
|
||||||
|
for (y = 2; y < 4; y++) {
|
||||||
|
wmove(curwin, y, 2);
|
||||||
|
for (x = 2; x < getmaxx(curwin) - 2; x++) {
|
||||||
|
waddch(curwin, ' ' | ATTR_NORMAL_WINDOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wrefresh(curwin);
|
||||||
|
|
||||||
|
// Ask the player to confirm their choice
|
||||||
|
mvwaddstr(curwin, 2, 2, " Are you sure? ");
|
||||||
|
waddstr(curwin, "[");
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "Y");
|
||||||
|
waddstr(curwin, "/");
|
||||||
|
attrpr(curwin, ATTR_KEYCODE_STR, "N");
|
||||||
|
waddstr(curwin, "] ");
|
||||||
|
|
||||||
|
if (! answer_yesno(curwin)) {
|
||||||
|
selection = SEL_NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deltxwin(); // "Select move" window
|
||||||
|
deltxwin(); // Galaxy map window
|
||||||
|
txrefresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_move (void)
|
void process_move (void)
|
||||||
|
@ -57,6 +57,8 @@ player_info_t player[MAX_PLAYERS]; // Array of players
|
|||||||
map_val_t galaxy_map[MAX_X][MAX_Y]; // Map of the galaxy
|
map_val_t galaxy_map[MAX_X][MAX_Y]; // Map of the galaxy
|
||||||
move_rec_t game_move[NUMBER_MOVES]; // Current moves
|
move_rec_t game_move[NUMBER_MOVES]; // Current moves
|
||||||
|
|
||||||
|
sel_val_t selection; // Move selected by current player
|
||||||
|
|
||||||
int max_turn; // Max. number of turns in game
|
int max_turn; // Max. number of turns in game
|
||||||
int turn_number;
|
int turn_number;
|
||||||
int number_players;
|
int number_players;
|
||||||
|
@ -118,9 +118,21 @@ typedef struct move_rec {
|
|||||||
} move_rec_t;
|
} move_rec_t;
|
||||||
|
|
||||||
#define MOVE_TO_KEY(m) ((m) + 'a')
|
#define MOVE_TO_KEY(m) ((m) + 'a')
|
||||||
#define KEY_TO_MOVE(k) \
|
#define KEY_TO_MOVE(k) ((k) - 'a')
|
||||||
((tolower(k) < 'a' || tolower(k) >= MOVE_TO_KEY(NUMBER_MOVES)) ? \
|
#define IS_MOVE_KEY(k) ((k) >= 'a' && (k) < MOVE_TO_KEY(NUMBER_MOVES))
|
||||||
ERR : (tolower(k) - 'a'));
|
|
||||||
|
|
||||||
|
// Player moves / selection values
|
||||||
|
typedef enum sel_val {
|
||||||
|
SEL_MOVE_FIRST = 0,
|
||||||
|
SEL_MOVE_LAST = NUMBER_MOVES - 1,
|
||||||
|
SEL_MOVE_NUMBER_MOVES = NUMBER_MOVES,
|
||||||
|
|
||||||
|
SEL_MAKE_BANKRUPT, // Player wishes to give up
|
||||||
|
SEL_SAVE_GAME, // Save and end the game
|
||||||
|
SEL_QUIT_GAME, // Just end the game
|
||||||
|
SEL_NONE = -1 // Nothing yet selected
|
||||||
|
} sel_val_t;
|
||||||
|
|
||||||
|
|
||||||
// Company names
|
// Company names
|
||||||
@ -136,6 +148,8 @@ extern player_info_t player[MAX_PLAYERS]; // Array of players
|
|||||||
extern map_val_t galaxy_map[MAX_X][MAX_Y]; // Map of the galaxy
|
extern map_val_t galaxy_map[MAX_X][MAX_Y]; // Map of the galaxy
|
||||||
extern move_rec_t game_move[NUMBER_MOVES]; // Current moves
|
extern move_rec_t game_move[NUMBER_MOVES]; // Current moves
|
||||||
|
|
||||||
|
extern sel_val_t selection; // Move selected by current player
|
||||||
|
|
||||||
extern int max_turn; // Max. number of turns in game
|
extern int max_turn; // Max. number of turns in game
|
||||||
extern int turn_number;
|
extern int turn_number;
|
||||||
extern int number_players;
|
extern int number_players;
|
||||||
|
Loading…
Reference in New Issue
Block a user