Le support aux utilisateurs et les demandes de création de comptes externes doivent être faites depuis les issues du projet GitLab HEFR (https://gitlab.forge.hefr.ch/forge-hefr/gitlab-hefr)

Commit eccb377b authored by Arton Hoxha's avatar Arton Hoxha
Browse files

refactor magic numbers (wip) and added more difficulty with timer

parent 1260e573
Pipeline #15477 failed with stages
in 15 seconds
......@@ -7,6 +7,12 @@
#include "string.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define WORD_LENGTH 9
#define ERROR 1
#define NO_ERROR 0
#define TIME_FACTOR 5
#define MAX_NB_ERRORS 6
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
......@@ -19,9 +25,9 @@ void game_init(struct game* game)
game->events_msq = msgq_create(32, true);
game->counter = 0;
game->errors = 0;
game->inMenu = false;
game->inSelection = false;
game->inMainScreen = true;
game->inSelection = false;
game->inMenu = false;
game->inGame = false;
game->inEndMenu = false;
game->hasGameEnded = false;
......@@ -31,7 +37,7 @@ void game_init(struct game* game)
game->selected_char = 'a';
game->selected_word = " ";
game->selected_word_length = 0;
for (unsigned i = 0; i < 9; i++) {
for (unsigned i = 0; i < WORD_LENGTH; i++) {
game->are_chars_in_word[i] = 0;
}
msgq_post(game->render_msq, (void*)RENDER_MAIN_MENU);
......@@ -40,7 +46,7 @@ void game_init(struct game* game)
void thread_game(void* param)
{
game* game = (struct game*)param;
while (1) {
while (true) {
handle_events((int)msgq_fetch(game->events_msq), game);
}
}
......@@ -48,7 +54,7 @@ void thread_game(void* param)
static int str_length(char* str)
{
int len = 0;
for (int i = 0; i < 8; i++) {
for (int i = 0; i < WORD_LENGTH-1; i++) {
if (str[i] == ' ' || str[i] == '\0') {
break;
}
......@@ -59,7 +65,7 @@ static int str_length(char* str)
static void check_word(game* game)
{
int detected_error = 1;
int detected_error = ERROR;
for (int i = 0; i < game->selected_word_length; i++) {
if (game->selected_char == game->selected_word[i]) {
......@@ -67,8 +73,8 @@ static void check_word(game* game)
game->correct_letters++;
game->are_chars_in_word[i] = 1;
msgq_post(game->render_msq, (void*)RENDER_WORD_CHAR);
detected_error = 0;
game->counter = (game->selected_word_length + 1) * 5;
detected_error = NO_ERROR;
game->counter = (game->selected_word_length - game->correct_letters + 1) * TIME_FACTOR;
}
}
}
......@@ -77,7 +83,7 @@ static void check_word(game* game)
msgq_post(game->events_msq, (void*)GAME_STOP);
} else if (detected_error) {
game->errors = game->errors + 1;
if (game->errors == 6) {
if (game->errors == MAX_NB_ERRORS) {
msgq_post(game->events_msq, (void*)GAME_STOP);
} else {
msgq_post(game->render_msq, (void*)RENDER_NEXT_PART);
......@@ -139,9 +145,7 @@ void handle_events(game_events event, game* game)
} else if (event == GAME_WHEEL_CLICK) {
if (game->inSelection) {
printf("index: %d", index);
if (index != 8) {
printf("render char %c\n", game->selected_char);
if (index != WORD_LENGTH -1) {
game->selected_word[index++] = game->selected_char;
msgq_post(game->render_msq, (void*)RENDER_CHAR);
msgq_post(game->render_msq, (void*)RENDER_NEXT_CHAR);
......@@ -150,10 +154,9 @@ void handle_events(game_events event, game* game)
game->inMenu = true;
game->selected_word[index++] = game->selected_char;
game->selected_word_length = strlen(game->selected_word);
game->counter = (strlen(game->selected_word) + 1) * 5;
game->counter = (strlen(game->selected_word) + 1) * TIME_FACTOR;
game->selected_char = 'a';
game->selected_char_index = 0;
printf("word: %s\n", game->selected_word);
msgq_post(game->menu_msq, (void*)MENU_RENDER);
}
} else if (game->inMainScreen) {
......@@ -171,7 +174,7 @@ void handle_events(game_events event, game* game)
} else if (event == GAME_RESET_SELECTION) {
// https://stackoverflow.com/questions/53943743/how-to-reset-a-string-in-c
game->selected_word = memset(game->selected_word, 0, 9);
game->selected_word = memset(game->selected_word, 0, WORD_LENGTH); //set all 0
index = 0;
game->hasGameEnded = false;
game->inEndMenu = false;
......@@ -185,7 +188,7 @@ void handle_events(game_events event, game* game)
game->selected_char = 'a';
game->selected_word_length = 0;
game->correct_letters = 0;
for (int i = 0; i < 9; i++) {
for (int i = 0; i < WORD_LENGTH; i++) {
game->are_chars_in_word[i] = 0;
}
msgq_post(game->render_msq, (void*)RENDER_RESET);
......@@ -205,7 +208,7 @@ void handle_events(game_events event, game* game)
game->selected_word_length = str_length(game->selected_word);
game->selected_char = 'a';
game->selected_char_index = 0;
game->counter = (str_length(game->selected_word) + 1) * 5;
game->counter = (str_length(game->selected_word) + 1) * TIME_FACTOR;
msgq_post(game->menu_msq, (void*)MENU_RENDER);
}
}
......
......@@ -33,10 +33,7 @@
typedef struct game {
msgqid_t menu_msq;
msgqid_t render_msq;
msgqid_t char_msq;
msgqid_t events_msq;
int counter;
uint8_t errors;
bool inMenu;
bool inMainScreen;
bool inSelection;
......@@ -44,9 +41,11 @@ typedef struct game {
bool inEndMenu;
bool hasGameEnded;
bool hasWon;
uint8_t selected_char_index;
char selected_char;
char* selected_word;
uint8_t errors;
uint8_t selected_char_index;
int counter;
int selected_word_length;
int correct_letters;
int are_chars_in_word[];
......@@ -94,6 +93,5 @@ typedef enum {
} game_render;
void handle_events(game_events event, game* game);
void handle_render(game_render render);
#endif
......@@ -26,7 +26,7 @@
#define S1_PIN 15
#define S2_PIN 16
#define S3_PIN 17
#define PERIOD 100
#define BLINK_PERIOD 100
#define GAME_PERIOD 10
enum dmtimer_timers timer_blink = DMTIMER_2;
......@@ -46,7 +46,7 @@ static void s1_handler(void* param)
}
}
// increments/decrements counter
static void wheel_handler(enum wheel_direction dir, void* param)
{
struct game* game = (struct game*)param;
......@@ -56,12 +56,15 @@ static void wheel_handler(enum wheel_direction dir, void* param)
msgq_post(game->events_msq, (void*)GAME_WHEEL_LEFT);
}
}
static void wheel_click(void* param)
{
struct game* game = (struct game*)param;
msgq_post(game->events_msq, (void*)GAME_WHEEL_CLICK);
}
void refresh_display(void* param)
{
game* game = (struct game*)param;
......@@ -98,10 +101,8 @@ void refresh_counter(void* param)
void init()
{
counter_init();
// wheel_init();
counter_init(); //init wheel and seg7 too
lcd_init();
// seg7_init();
intc_init();
interrupt_init();
exception_init();
......@@ -123,7 +124,7 @@ int main()
dmtimer_reset(timer_blink);
dmtimer_on_event(timer_blink, refresh_display, &game);
dmtimer_set_period(timer_blink, PERIOD);
dmtimer_set_period(timer_blink, BLINK_PERIOD);
dmtimer_start(timer_blink);
gpio_on_event(SW_GPIO, S1_PIN, GPIO_FALLING, s1_handler, &game);
......
......@@ -10,6 +10,10 @@
#define BTN_GPIO AM335X_GPIO0
#define BTN_PIN 2
#define END_OR_RESTART 0
#define PLAY_OR_PLAY_AGAIN 1
#define CHOICE_X_POS 65
#define OFFSET_Y 30
// Menu entries
char* choices[2] = {
......@@ -23,7 +27,9 @@ char* players_str[2] = {
[1] = "Player 2 wins",
};
char* reset = "Confirm ?";
char* confirm = "Confirm ?";
char* replay = "Replay ?";
// Current id of the menu item selected
unsigned current_choice = 1;
......@@ -31,24 +37,29 @@ unsigned current_choice = 1;
static void menu_display(game* game)
{
if (game->inMenu) {
lcd_draw_rect(0, 50, 0, 95, BLACK);
lcd_display_string(reset, 0, 0, false);
lcd_draw_rect(
0,
50,
0,
95,
BLACK); // clear lcd display due to lag with lcd_clear() method
lcd_display_string(confirm, 0, 0, false);
lcd_display_string(game->selected_word, 45, 0, false);
lcd_change_text_color(RED);
lcd_display_string(choices[0], 65, 35, false);
lcd_display_string(choices[0], CHOICE_X_POS, CHOICE_X_POS - OFFSET_Y, false);
lcd_change_text_color(WHITE);
lcd_display_string(choices[1], 65, 65, false);
lcd_display_string(choices[1], CHOICE_X_POS, CHOICE_X_POS, false);
} else if (game->inEndMenu) {
if (game->hasWon) {
lcd_display_string(players_str[1], 0, 0, false);
} else {
lcd_display_string(players_str[0], 0, 0, false);
}
lcd_display_string("Replay ?", 48, 20, false);
lcd_display_string(replay, 48, 20, false);
lcd_change_text_color(RED);
lcd_display_string(choices[0], 65, 35, false);
lcd_display_string(choices[0], CHOICE_X_POS, CHOICE_X_POS - OFFSET_Y, false);
lcd_change_text_color(WHITE);
lcd_display_string(choices[1], 65, 65, false);
lcd_display_string(choices[1], CHOICE_X_POS, CHOICE_X_POS, false);
}
}
......@@ -56,31 +67,19 @@ static void change_option(game* game)
{
printf("choice: %d\n", current_choice);
if (current_choice == 1) {
if (game->inMenu) {
lcd_change_text_color(RED);
lcd_display_string(choices[0], 65, 35, false);
lcd_change_text_color(WHITE);
lcd_display_string(choices[1], 65, 65, false);
} else if (game->inEndMenu) {
// Render menu end
if (game->inMenu || game->inEndMenu) {
lcd_change_text_color(RED);
lcd_display_string(choices[0], 65, 35, false);
lcd_display_string(choices[0], CHOICE_X_POS, CHOICE_X_POS - OFFSET_Y, false);
lcd_change_text_color(WHITE);
lcd_display_string(choices[1], 65, 65, false);
lcd_display_string(choices[1], CHOICE_X_POS, CHOICE_X_POS, false);
}
} else {
if (game->inMenu) {
lcd_change_text_color(RED);
lcd_display_string(choices[1], 65, 65, false);
lcd_change_text_color(WHITE);
lcd_display_string(choices[0], 65, 35, false);
} else if (game->inEndMenu) {
// Render menu end
if (game->inMenu || game->inEndMenu) {
lcd_change_text_color(RED);
lcd_display_string(choices[1], 65, 65, false);
lcd_display_string(choices[1], CHOICE_X_POS, CHOICE_X_POS, false);
lcd_change_text_color(WHITE);
lcd_display_string(choices[0], 65, 35, false);
lcd_display_string(choices[0], CHOICE_X_POS, CHOICE_X_POS - OFFSET_Y, false);
}
}
}
......@@ -90,16 +89,16 @@ static void menu_wheel_handler(game_menu menu_event, game* game)
if (menu_event == MENU_RENDER) {
menu_display(game);
} else if (menu_event == MENU_RESET) {
current_choice = 1;
current_choice = PLAY_OR_PLAY_AGAIN;
} else if (menu_event == MENU_LEFT) {
current_choice = 1;
current_choice = PLAY_OR_PLAY_AGAIN;
change_option(game);
} else if (menu_event == MENU_RIGHT) {
current_choice = 0;
current_choice = END_OR_RESTART;
change_option(game);
} else if (menu_event == MENU_CLICK) {
if (current_choice == 0) {
if (current_choice == END_OR_RESTART) {
if (game->inMenu) { // reset
game->inMenu = false;
msgq_post(game->events_msq, (void*)GAME_RESET_SELECTION);
......@@ -107,7 +106,7 @@ static void menu_wheel_handler(game_menu menu_event, game* game)
msgq_post(game->render_msq, (void*)RENDER_END);
}
} else if (current_choice == 1) {
} else if (current_choice == PLAY_OR_PLAY_AGAIN) {
if (game->inMenu) { // play
msgq_post(game->events_msq, (void*)GAME_START);
game->inMenu = false;
......@@ -117,7 +116,7 @@ static void menu_wheel_handler(game_menu menu_event, game* game)
msgq_post(game->events_msq, (void*)GAME_RESET_SELECTION);
}
}
current_choice = 1;
current_choice = PLAY_OR_PLAY_AGAIN;
}
}
......
......@@ -16,6 +16,9 @@
#define WHITE 0xFFFF
#define BLACK 0x0000
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define WORD_LENGTH 9
#define TITLE_Y 20
#define TICK_OFFSET 10
char* str = "Select word:";
char* player1_str = "Player 1";
......@@ -34,29 +37,23 @@ static void render_selection()
lcd_draw_rect(80, 95, 0, 95, BLACK);
lcd_display_string(player1_str, 0, 20, false);
lcd_display_string(str, 12, 0, false);
lcd_draw_rect(48, 50, 4, 12, WHITE);
lcd_draw_rect(48, 50, 14, 22, WHITE);
lcd_draw_rect(48, 50, 24, 32, WHITE);
lcd_draw_rect(48, 50, 34, 42, WHITE);
lcd_draw_rect(48, 50, 44, 52, WHITE);
lcd_draw_rect(48, 50, 54, 62, WHITE);
lcd_draw_rect(48, 50, 64, 72, WHITE);
lcd_draw_rect(48, 50, 74, 82, WHITE);
lcd_draw_rect(48, 50, 84, 92, WHITE);
for (int i = 0; i < WORD_LENGTH; i++) {
lcd_draw_rect(48, 50, 4 + 10 * i, 12 + 10 * i, WHITE);
}
}
static void render_game(game* game)
{
lcd_change_text_color(WHITE);
lcd_draw_rect(0, 80, 0, 95, BLACK);
lcd_display_string("Hangman", 0, 20, false);
lcd_draw_rect(42, 44, 4, 12, WHITE);
lcd_draw_rect(0, 80, 0, 95, BLACK); // clear screen
lcd_display_string("Hangman", 0, TITLE_Y, false);
lcd_draw_rect(42, 44, 4, 12, WHITE); // tick selection
int x = 4, y = 12;
lcd_draw_rect(80, 82, x, y, WHITE);
for (int i = 0; i < game->selected_word_length - 1; i++) {
x += 10;
y += 10;
x += TICK_OFFSET;
y += TICK_OFFSET;
lcd_draw_rect(80, 82, x, y, WHITE);
}
lcd_draw_image(&hangman[0], 20, 20);
......@@ -65,37 +62,7 @@ static void render_game(game* game)
static void render_word_char(game* game, int index)
{
switch (index) {
case 0:
lcd_display_char(game->selected_word[0], 80, 4, false);
break;
case 1:
lcd_display_char(game->selected_word[1], 80, 14, false);
break;
case 2:
lcd_display_char(game->selected_word[2], 80, 24, false);
break;
case 3:
lcd_display_char(game->selected_word[3], 80, 34, false);
break;
case 4:
lcd_display_char(game->selected_word[4], 80, 44, false);
break;
case 5:
lcd_display_char(game->selected_word[5], 80, 54, false);
break;
case 6:
lcd_display_char(game->selected_word[6], 80, 64, false);
break;
case 7:
lcd_display_char(game->selected_word[7], 80, 74, false);
break;
case 8:
lcd_display_char(game->selected_word[8], 80, 84, false);
break;
default:
break;
}
lcd_display_char(game->selected_word[index], 80, 4 + index * 10, false);
}
void thread_render(void* param)
......@@ -126,7 +93,7 @@ void thread_render(void* param)
char_y = 4;
render_selection();
} else if (msg == RENDER_WORD_CHAR) {
for (int i = 0; i < 9; i++) {
for (int i = 0; i < WORD_LENGTH; i++) {
if (game->are_chars_in_word[i]) {
render_word_char(game, i);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment