Hi-lo: Additional code tweaks

This commit is contained in:
Chris 2024-09-01 19:30:31 +01:00 committed by Matheus Afonso Martins Moreira
parent 97ad12d939
commit 9e1e692511
2 changed files with 33 additions and 28 deletions

View File

@ -36,15 +36,16 @@
#define GAME_BOARD_SIZE 6 #define GAME_BOARD_SIZE 6
#define MAX_BOARDS 40 #define MAX_BOARDS 40
#define GUESSES_PER_SCREEN 5 #define GUESSES_PER_SCREEN 5
#define WIN_SCORE MAX_BOARDS * GUESSES_PER_SCREEN #define WIN_SCORE (MAX_BOARDS * GUESSES_PER_SCREEN)
#define STATUS_DISPLAY_START 0 #define STATUS_DISPLAY_START 0
#define BOARD_SCORE_DISPLAY_START 2 #define BOARD_SCORE_DISPLAY_START 2
#define BOARD_DISPLAY_START 4 #define BOARD_DISPLAY_START 4
#define BOARD_DISPLAY_END 9 #define BOARD_DISPLAY_END 9
#define MIN_CARD_VALUE 2 #define MIN_CARD_VALUE 2
#define MAX_CARD_VALUE 14 #define MAX_CARD_VALUE 14
#define DUPLICATES_OF_CARD 4 #define CARD_RANK_COUNT (MAX_CARD_VALUE - MIN_CARD_VALUE + 1)
#define DECK_COUNT (DUPLICATES_OF_CARD * (MAX_CARD_VALUE - MIN_CARD_VALUE + 1)) #define CARD_SUIT_COUNT 4
#define DECK_SIZE (CARD_SUIT_COUNT * CARD_RANK_COUNT)
#define FLIP_BOARD_DIRECTION false #define FLIP_BOARD_DIRECTION false
typedef struct card_t { typedef struct card_t {
@ -75,8 +76,8 @@ static card_t game_board[GAME_BOARD_SIZE] = {0};
static uint8_t guess_position = 0; static uint8_t guess_position = 0;
static uint8_t score = 0; static uint8_t score = 0;
static uint8_t completed_board_count = 0; static uint8_t completed_board_count = 0;
static uint8_t _deck[DECK_COUNT]; static uint8_t deck[DECK_SIZE] = {0};
static uint8_t _curr_card; static uint8_t current_card = 0;
static uint8_t generate_random_number(uint8_t num_values) { static uint8_t generate_random_number(uint8_t num_values) {
// Emulator: use rand. Hardware: use arc4random. // Emulator: use rand. Hardware: use arc4random.
@ -87,35 +88,37 @@ static uint8_t generate_random_number(uint8_t num_values) {
#endif #endif
} }
static void stack_deck(uint8_t *array) { static void stack_deck(void) {
const uint8_t unique_cards = MAX_CARD_VALUE - MIN_CARD_VALUE + 1; for (size_t i = 0; i < CARD_RANK_COUNT; i++) {
for (uint8_t i = 0; i < unique_cards; i++) for (size_t j = 0; j < CARD_SUIT_COUNT; j++)
{ deck[(i * CARD_SUIT_COUNT) + j] = MIN_CARD_VALUE + i;
for (uint8_t j = 0; j < DUPLICATES_OF_CARD; j++)
array[(i * DUPLICATES_OF_CARD) + j] = MIN_CARD_VALUE + i;
} }
} }
static void shuffle_deck(uint8_t *array, uint8_t n) { static void shuffle_deck(void) {
// Randomize shuffle with Fisher Yates // Randomize shuffle with Fisher Yates
uint8_t i, j, tmp; size_t i;
for (i = n - 1; i > 0; i--) { size_t j;
uint8_t tmp;
for (i = DECK_SIZE - 1; i > 0; i--) {
j = generate_random_number(0xFF) % (i + 1); j = generate_random_number(0xFF) % (i + 1);
tmp = array[j]; tmp = deck[j];
array[j] = array[i]; deck[j] = deck[i];
array[i] = tmp; deck[i] = tmp;
} }
} }
static void reset_deck(void) { static void reset_deck(void) {
_curr_card = 0; current_card = 0;
stack_deck(_deck); stack_deck();
shuffle_deck(_deck, DECK_COUNT); shuffle_deck();
} }
static uint8_t get_next_card(void) { static uint8_t get_next_card(void) {
if (_curr_card >= DECK_COUNT) reset_deck(); if (current_card >= DECK_SIZE)
return _deck[_curr_card++]; reset_deck();
return deck[current_card++];
} }
static void reset_board(bool first_round) { static void reset_board(bool first_round) {
@ -196,7 +199,7 @@ static void render_board_position(size_t board_position) {
} }
} }
static void render_board() { static void render_board(void) {
for (size_t i = 0; i < GAME_BOARD_SIZE; ++i) { for (size_t i = 0; i < GAME_BOARD_SIZE; ++i) {
render_board_position(i); render_board_position(i);
} }
@ -218,7 +221,7 @@ static void render_final_score(void) {
watch_display_string(buf, BOARD_DISPLAY_START); watch_display_string(buf, BOARD_DISPLAY_START);
} }
static guess_t get_answer() { static guess_t get_answer(void) {
if (guess_position < 1 || guess_position > GAME_BOARD_SIZE) if (guess_position < 1 || guess_position > GAME_BOARD_SIZE)
return HL_GUESS_EQUAL; // Maybe add an error state, shouldn't ever hit this. return HL_GUESS_EQUAL; // Maybe add an error state, shouldn't ever hit this.

View File

@ -65,6 +65,8 @@
* The face tries to remain true to the spirit of using "cards"; to cope with the display limitations I've arrived at * The face tries to remain true to the spirit of using "cards"; to cope with the display limitations I've arrived at
* the following mapping of card values to screen display, but am open to better suggestions: * the following mapping of card values to screen display, but am open to better suggestions:
* *
* Thanks to voloved for adding deck shuffling and drawing!
*
* | Cards | | * | Cards | |
* |---------|--------------------------| * |---------|--------------------------|
* | Value |2|3|4|5|6|7|8|9|10|J|Q|K|A| * | Value |2|3|4|5|6|7|8|9|10|J|Q|K|A|