diff --git a/conf.h b/conf.h index 36f148ac13a0202fe5a94ead3c289499976234a6..09303701e0ea6837f3adb66fe8f490b646b58f91 100644 --- a/conf.h +++ b/conf.h @@ -4,9 +4,10 @@ // Game setup #define TRIES_COUNT 6 #define WORD_LENGHT 5 +#define VERSUS_TURN_COUNT 2 // Enums typedef enum Gamemode { SOLO, VERSUS, TOOL_ASSISTED } Gamemode; typedef enum Pattern { WRONG, MISPLACED, CORRECT, NAP } Pattern; // NAP => Not A Pattern (Only used when computing all possible patterns) -#endif \ No newline at end of file +#endif diff --git a/ui/ui.c b/ui/ui.c index 2f0ab684f960f064b832042304e7a092b22450e6..a4e8fd31ab91937a979e6f2807d7513555923c04 100644 --- a/ui/ui.c +++ b/ui/ui.c @@ -1,4 +1,5 @@ #include "ui.h" +#include <curses.h> #define MENU_MAX 4 #define TITLE_Y_OFFSET 2 @@ -282,7 +283,7 @@ void update_tool(int count, char words[count][WORD_LENGHT + 1], double *entropie wrefresh(tool); } -void show_gameboard(int tries_count, int word_length, char **tries, Pattern **patterns, int score) { +void show_gameboard(int tries_count, int word_length, char **tries, Pattern **patterns, int* scores, Gamemode mode, bool p2_turn, int current_turn) { for (int i = 0; i < tries_count; i++) { // Draw current try letters for (int j = 0; j < word_length; j++) { @@ -304,8 +305,33 @@ void show_gameboard(int tries_count, int word_length, char **tries, Pattern **pa } } - move(GAMEBOARD_Y_OFFSET - 2 - menu_offset_modifer() + 5, TERM_MID_COLS - word_length - 3); - printw("Score: %d", score); + if (mode == VERSUS) { + move(GAMEBOARD_Y_OFFSET, TERM_MID_COLS - word_length); + printw("Turn: %d/%d", current_turn, VERSUS_TURN_COUNT); + + if (!p2_turn) + attron(COLOR_PAIR(GREEN_PAIR)); + move(GAMEBOARD_Y_OFFSET - 2 - menu_offset_modifer() + 5, TERM_MID_COLS - word_length - 10); + printw("Score P1: %d", scores[0]); + + attroff(COLOR_PAIR(GREEN_PAIR)); + + if (p2_turn) + attron(COLOR_PAIR(GREEN_PAIR)); + move(GAMEBOARD_Y_OFFSET - 2 - menu_offset_modifer() + 5, TERM_MID_COLS - word_length + 8); + printw("Score P2: %d", scores[1]); + + attroff(COLOR_PAIR(GREEN_PAIR)); + } + else { + move(GAMEBOARD_Y_OFFSET - 2 - menu_offset_modifer() + 5, TERM_MID_COLS - word_length); + printw("Score: %d", scores[0]); + } refresh(); -} \ No newline at end of file +} + +void show_winner(bool p2_turn, int* scores) { + move(GAMEBOARD_Y_OFFSET + 15, TERM_MID_COLS - 13); + printw("Winner: %s with %d points !", p2_turn ? "P2" : "P1", p2_turn ? scores[1] : scores[0]); +} diff --git a/ui/ui.h b/ui/ui.h index 9bf570213f7a97a014dab13a624c751e1d7c1edf..3a73bf124a0b1b9123be269416adbbdfe6be7364 100644 --- a/ui/ui.h +++ b/ui/ui.h @@ -118,8 +118,19 @@ void update_tool(int count, char words[count][WORD_LENGHT + 1], double *entropie * @param word_length * @param tries * @param patterns - * @param score + * @param scores + * @param mode + * @param p2_turn + * @param current_turn */ -void show_gameboard(int tries_count, int word_length, char **tries, Pattern **patterns, int score); +void show_gameboard(int tries_count, int word_length, char **tries, Pattern **patterns, int* scores, Gamemode mode, bool p2_turn, int current_turn); -#endif \ No newline at end of file +/** + * @brief Show the winner + * + * @param p2_turn + * @param scores + */ +void show_winner(bool p2_turn, int* scores); + +#endif diff --git a/wordle/wordle.c b/wordle/wordle.c index f209bd546aed43c35c8a493648b287a8fdef6986..c1d82dfbc7b1842f88a77b9cf5b8ac2e01e10965 100644 --- a/wordle/wordle.c +++ b/wordle/wordle.c @@ -1,4 +1,6 @@ #include "wordle.h" +#include <curses.h> +#include <stdlib.h> //========================== // PRIVATE @@ -6,6 +8,9 @@ Gamemode mode; bool _game_finished = false; int score = 0; +int *scores; +bool p2_turn = false; +int turn = 0; char **tries; char *chosen_word; Pattern **patterns; @@ -30,8 +35,8 @@ void render_game() { draw_title(COMPUTER_TITLE_ID); break; } - - show_gameboard(TRIES_COUNT, WORD_LENGHT, tries, patterns, score); + + show_gameboard(TRIES_COUNT, WORD_LENGHT, tries, patterns, scores, mode, p2_turn, turn); move(LINES - 2, COLS - 10); } @@ -108,9 +113,11 @@ void initialize_game() { current_try_id = 0; current_try_letter_id = 0; score = 0; + scores = calloc(2, sizeof(int)); // Tries and answer setup set_answer(get_random_word()); + mvprintw(30, 30, "%s", chosen_word); tries = calloc(TRIES_COUNT, sizeof(char *)); patterns = malloc(sizeof(Pattern *) * TRIES_COUNT); @@ -127,6 +134,8 @@ void destroy_game() { if (mode == TOOL_ASSISTED) destroy_tool(); + free(scores); + for (int i = 0; i < TRIES_COUNT; i++) { free(tries[i]); free(patterns[i]); @@ -142,8 +151,25 @@ void restart_game() { init_tool(); } - score += (TRIES_COUNT - current_try_id) * 10; + // Versus mode scores or solo score + if (mode == VERSUS) { + if (turn >= VERSUS_TURN_COUNT) { + show_winner(p2_turn, scores); + getch(); + terminate_game(); + return; + } + + scores[p2_turn] += (TRIES_COUNT - current_try_id) * 10; + p2_turn = !p2_turn; + + if (!p2_turn) + turn++; + } else + scores[0] += (TRIES_COUNT - current_try_id) * 10; + set_answer(get_random_word()); + mvprintw(30, 30, "%s", chosen_word); current_try_id = 0; current_try_letter_id = 0;