1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Implement the select_moves() function

This function select random moves for the current player.
This commit is contained in:
John Zaitseff 2011-07-14 21:44:18 +10:00
parent 546c80053f
commit e46c45d52e
2 changed files with 104 additions and 10 deletions

View File

@ -130,6 +130,13 @@ static const int game_file_crypt_key[] = {
save_game_printf("%s", _var)
/************************************************************************
* Internal function declarations *
************************************************************************/
int cmp_game_move (const void *a, const void *b);
/************************************************************************
* Game function definitions *
************************************************************************/
@ -806,9 +813,65 @@ bool save_game (int num)
}
/*-----------------------------------------------------------------------
Function: select_moves - Select NUMBER_MOVES random moves
Arguments: (none)
Returns: (nothing)
This function selects NUMBER_MOVES random moves and stores them in the
game_move[] array. If there are less than NUMBER_MOVES empty spaces in
the galaxy map, the game is automatically finished by setting
quit_selected to true.
*/
void select_moves (void)
{
// @@@ To be written
int count;
int x, y, i, j;
int tx, ty;
bool unique;
// How many empty spaces are there in the galaxy map?
count = 0;
for (x = 0; x < MAX_X; x++) {
for (y = 0; y < MAX_Y; y++) {
if (galaxy_map[x][y] == MAP_EMPTY) {
count++;
}
}
}
if (count < NUMBER_MOVES) {
quit_selected = true;
return;
}
// Generate unique random moves
for (i = 0; i < NUMBER_MOVES; i++) {
do {
do {
tx = randi(MAX_X);
ty = randi(MAX_Y);
} while (galaxy_map[tx][ty] != MAP_EMPTY);
unique = true;
for (j = i - 1; j >= 0; j--) {
if (tx == game_move[j].x && ty == game_move[j].y) {
unique = false;
break;
}
}
} while (! unique);
game_move[i].x = tx;
game_move[i].y = ty;
}
// Sort moves from left to right
qsort(game_move, NUMBER_MOVES, sizeof(move_rec_t), cmp_game_move);
quit_selected = false;
}
void get_move (void)
@ -831,3 +894,34 @@ void next_player (void)
// @@@ To be written
quit_selected = true;
}
/*-----------------------------------------------------------------------
Function: cmp_game_move - Compare two game_move elements
Arguments: a, b - Elements to compare
Returns: int - Comparison of a and b
This function compares two game_move elements (of type move_rec_t) and
returns -1 if a < b, 0 if a == b and 1 if a > b.
*/
int cmp_game_move (const void *a, const void *b)
{
const move_rec_t *aa = (const move_rec_t *) a;
const move_rec_t *bb = (const move_rec_t *) b;
if (aa->x < bb->x) {
return -1;
} else if (aa->x > bb->x) {
return 1;
} else {
if (aa->y < bb->y) {
return -1;
} else if (aa->y > bb->y) {
return 1;
} else {
return 0;
}
}
}

View File

@ -39,17 +39,17 @@
* Game function declarations *
************************************************************************/
void init_game (void);
void end_game (void);
extern void init_game (void);
extern void end_game (void);
bool load_game (int num);
bool save_game (int num);
extern bool load_game (int num);
extern bool save_game (int num);
void select_moves (void);
void get_move (void);
void process_move (void);
void exchange_stock (void);
void next_player (void);
extern void select_moves (void);
extern void get_move (void);
extern void process_move (void);
extern void exchange_stock (void);
extern void next_player (void);
#endif /* included_GAME_H */