add makefile, and mods for wordlist installation

This commit is contained in:
Gregory Gauthier 2026-01-26 11:51:27 +00:00
parent 4b59258364
commit 30b7116fbd
3 changed files with 103 additions and 12 deletions

69
Makefile Normal file
View File

@ -0,0 +1,69 @@
# Makefile for Cordle - C90 Wordle Game
# Compiler and flags
CC = gcc
CFLAGS = -std=c90 -pedantic -Wpedantic -Wall -Wextra
LDFLAGS = -lncurses
# Directories
SRC_DIR = src
INCLUDE_DIR = include
BUILD_DIR = build
INSTALL_DIR = /usr/local
# Source files
SRCS = $(SRC_DIR)/main.c $(SRC_DIR)/game.c $(SRC_DIR)/ui.c $(SRC_DIR)/words.c
OBJS = $(SRCS:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
# Target executable
TARGET = $(BUILD_DIR)/cordle
# Default target
all: $(TARGET) wordlists
# Create build directory
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# Compile source files to object files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -I$(INCLUDE_DIR) -c $< -o $@
# Link object files to create executable
$(TARGET): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $(TARGET)
# Copy wordlists to build directory
wordlists: $(TARGET)
cp -R wordlists $(BUILD_DIR)/
# Clean build artifacts
clean:
rm -rf $(BUILD_DIR)
# Install the game
install: all
install -d $(INSTALL_DIR)/bin
install -m 755 $(TARGET) $(INSTALL_DIR)/bin/cordle
install -d $(INSTALL_DIR)/share/cordle
cp -R wordlists $(INSTALL_DIR)/share/cordle/
# Uninstall the game
uninstall:
rm -f $(INSTALL_DIR)/bin/cordle
rm -rf $(INSTALL_DIR)/share/cordle
# Rebuild everything
rebuild: clean all
# Help target
help:
@echo "Cordle Makefile targets:"
@echo " all - Build the game (default)"
@echo " clean - Remove build artifacts"
@echo " rebuild - Clean and rebuild"
@echo " install - Install to $(INSTALL_DIR)"
@echo " uninstall - Remove installed files"
@echo " help - Show this help message"
.PHONY: all clean install uninstall rebuild wordlists help

View File

@ -88,7 +88,7 @@ void draw_keyboard(WINDOW *win, GameState *game, int y) {
x = (width - strlen(row) * 2 + 1) / 2; x = (width - strlen(row) * 2 + 1) / 2;
y_pos = y + row_idx; y_pos = y + row_idx;
/* Add indentation for keyboard layout */ /* Add indentation for the keyboard layout */
if (row_idx == 1) { if (row_idx == 1) {
x += 1; x += 1;
} else if (row_idx == 2) { } else if (row_idx == 2) {

View File

@ -9,6 +9,35 @@
#include <time.h> #include <time.h>
#include <ctype.h> #include <ctype.h>
/* Try to open a wordlist file, using multiple fallback locations */
static FILE* open_wordlist(const char *filename) {
FILE *file;
char filepath[512];
const char *home;
/* Try 1: wordlists/ relative to the current directory (for development) */
sprintf(filepath, "wordlists/%s", filename);
file = fopen(filepath, "r");
if (file) return file;
/* Try 2: /usr/local/share/cordle/wordlists/ (system installation) */
sprintf(filepath, "/usr/local/share/cordle/wordlists/%s", filename);
file = fopen(filepath, "r");
if (file) return file;
/* Try 3: ${HOME}/.local/share/cordle/wordlists/ (user installation) */
home = getenv("HOME");
if (home) {
sprintf(filepath, "%s/.local/share/cordle/wordlists/%s", home, filename);
file = fopen(filepath, "r");
if (file) return file;
}
/* Try 4: filename as-is (custom path via --wordlist) */
file = fopen(filename, "r");
return file;
}
/* Convert string to uppercase */ /* Convert string to uppercase */
void to_upper(char *str) { void to_upper(char *str) {
int i; int i;
@ -17,25 +46,18 @@ void to_upper(char *str) {
} }
} }
/* Load words from file */ /* Load words from the file */
int load_words(GameState *game, const char *filename) { int load_words(GameState *game, const char *filename) {
FILE *file; FILE *file;
char filepath[MAX_FILENAME];
char line[32]; char line[32];
char word[WORD_LENGTH + 1]; char word[WORD_LENGTH + 1];
int len; int len;
/* Construct file path */ /* Try to open the file */
sprintf(filepath, "wordlists/%s", filename); file = open_wordlist(filename);
file = fopen(filepath, "r");
if (!file) {
/* Try without wordlists/ prefix */
file = fopen(filename, "r");
if (!file) { if (!file) {
return 0; return 0;
} }
}
game->word_count = 0; game->word_count = 0;
while (fgets(line, sizeof(line), file) && game->word_count < MAX_WORDS) { while (fgets(line, sizeof(line), file) && game->word_count < MAX_WORDS) {