Skip to content
Snippets Groups Projects
Commit 444b056a authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Working project

parent e6fd7b64
No related branches found
No related tags found
No related merge requests found
......@@ -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
#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]);
}
......@@ -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
#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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment