diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..42fc3d2 --- /dev/null +++ b/Makefile @@ -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 diff --git a/src/ui.c b/src/ui.c index 43332c1..9c95808 100644 --- a/src/ui.c +++ b/src/ui.c @@ -88,7 +88,7 @@ void draw_keyboard(WINDOW *win, GameState *game, int y) { x = (width - strlen(row) * 2 + 1) / 2; y_pos = y + row_idx; - /* Add indentation for keyboard layout */ + /* Add indentation for the keyboard layout */ if (row_idx == 1) { x += 1; } else if (row_idx == 2) { diff --git a/src/words.c b/src/words.c index 45faef7..c264ada 100644 --- a/src/words.c +++ b/src/words.c @@ -9,6 +9,35 @@ #include #include +/* 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 */ void to_upper(char *str) { int i; @@ -17,24 +46,17 @@ void to_upper(char *str) { } } -/* Load words from file */ +/* Load words from the file */ int load_words(GameState *game, const char *filename) { FILE *file; - char filepath[MAX_FILENAME]; char line[32]; char word[WORD_LENGTH + 1]; int len; - /* Construct file path */ - sprintf(filepath, "wordlists/%s", filename); - - file = fopen(filepath, "r"); + /* Try to open the file */ + file = open_wordlist(filename); if (!file) { - /* Try without wordlists/ prefix */ - file = fopen(filename, "r"); - if (!file) { - return 0; - } + return 0; } game->word_count = 0;