minor syntactic cleanup
This commit is contained in:
parent
8f3e6ef638
commit
7679dd9c6c
@ -8,7 +8,7 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
/* Initialize game state */
|
/* Initialize game state */
|
||||||
void init_game(GameState* game) {
|
void init_game(GameState *game) {
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
game->word_count = 0;
|
game->word_count = 0;
|
||||||
@ -34,7 +34,7 @@ void init_game(GameState* game) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check guess against target word */
|
/* Check guess against target word */
|
||||||
void check_guess(GameState* game, const char* guess, int* colors) {
|
void check_guess(GameState *game, const char *guess, int *colors) {
|
||||||
char target_copy[WORD_LENGTH + 1];
|
char target_copy[WORD_LENGTH + 1];
|
||||||
char guess_copy[WORD_LENGTH + 1];
|
char guess_copy[WORD_LENGTH + 1];
|
||||||
int i, j;
|
int i, j;
|
||||||
@ -86,7 +86,7 @@ void check_guess(GameState* game, const char* guess, int* colors) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Process a guess */
|
/* Process a guess */
|
||||||
int make_guess(GameState* game, const char* guess) {
|
int make_guess(GameState *game, const char *guess) {
|
||||||
char upper_guess[WORD_LENGTH + 1];
|
char upper_guess[WORD_LENGTH + 1];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
|||||||
21
src/main.c
21
src/main.c
@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
/* main.c - Entry point and main game loop */
|
/* main.c - Entry point and main game loop */
|
||||||
/* Compile with: gcc -std=c90 -o cordle src/*.c -Iinclude -lncurses */
|
/* Compile with: gcc -std=c90 -o cordle src\/\*.c -Iinclude -lncurses */
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -15,7 +15,7 @@
|
|||||||
#include "../include/ui.h"
|
#include "../include/ui.h"
|
||||||
|
|
||||||
/* Parse command line arguments */
|
/* Parse command line arguments */
|
||||||
void parse_arguments(int argc, char* argv[], char* filename, char* difficulty) {
|
void parse_arguments(int argc, char *argv[], char *filename, char *difficulty) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Default values */
|
/* Default values */
|
||||||
@ -53,8 +53,8 @@ void parse_arguments(int argc, char* argv[], char* filename, char* difficulty) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Main game loop */
|
/* Main game loop */
|
||||||
int main_game_loop(int argc, char* argv[]) {
|
int main_game_loop(int argc, char *argv[]) {
|
||||||
WINDOW* stdscr;
|
WINDOW *stdscr;
|
||||||
GameState game;
|
GameState game;
|
||||||
char filename[MAX_FILENAME];
|
char filename[MAX_FILENAME];
|
||||||
char difficulty[32];
|
char difficulty[32];
|
||||||
@ -70,7 +70,7 @@ int main_game_loop(int argc, char* argv[]) {
|
|||||||
|
|
||||||
/* Initialize ncurses */
|
/* Initialize ncurses */
|
||||||
stdscr = initscr();
|
stdscr = initscr();
|
||||||
curs_set(0); /* Hide cursor */
|
curs_set(0); /* Hide cursor */
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
/* Initialize colors */
|
/* Initialize colors */
|
||||||
@ -133,16 +133,16 @@ int main_game_loop(int argc, char* argv[]) {
|
|||||||
|
|
||||||
if (game.won) {
|
if (game.won) {
|
||||||
sprintf(win_message, "Congratulations! You won in %d guesses!",
|
sprintf(win_message, "Congratulations! You won in %d guesses!",
|
||||||
game.guess_count);
|
game.guess_count);
|
||||||
draw_message(stdscr, win_message, y_pos, COLOR_CORRECT);
|
draw_message(stdscr, win_message, y_pos, COLOR_CORRECT);
|
||||||
} else {
|
} else {
|
||||||
sprintf(win_message, "Game Over! The word was: %s",
|
sprintf(win_message, "Game Over! The word was: %s",
|
||||||
game.target_word);
|
game.target_word);
|
||||||
draw_message(stdscr, win_message, y_pos, COLOR_ABSENT);
|
draw_message(stdscr, win_message, y_pos, COLOR_ABSENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_message(stdscr, "Press 'N' for new game or 'Q' to quit",
|
draw_message(stdscr, "Press 'N' for new game or 'Q' to quit",
|
||||||
y_pos + 1, COLOR_DEFAULT);
|
y_pos + 1, COLOR_DEFAULT);
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
key = getch();
|
key = getch();
|
||||||
@ -165,7 +165,8 @@ int main_game_loop(int argc, char* argv[]) {
|
|||||||
key = getch();
|
key = getch();
|
||||||
|
|
||||||
/* Handle input */
|
/* Handle input */
|
||||||
if (key == 27) { /* ESC */
|
if (key == 27) {
|
||||||
|
/* ESC */
|
||||||
break;
|
break;
|
||||||
} else if (key == '\n' || key == '\r' || key == KEY_ENTER) {
|
} else if (key == '\n' || key == '\r' || key == KEY_ENTER) {
|
||||||
if (game.current_guess_length == WORD_LENGTH) {
|
if (game.current_guess_length == WORD_LENGTH) {
|
||||||
@ -206,7 +207,7 @@ int main_game_loop(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Main entry point */
|
/* Main entry point */
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Handle help */
|
/* Handle help */
|
||||||
|
|||||||
30
src/ui.c
30
src/ui.c
@ -7,14 +7,14 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/* Keyboard layout */
|
/* Keyboard layout */
|
||||||
static const char* keyboard_rows[3] = {
|
static const char *keyboard_rows[3] = {
|
||||||
"QWERTYUIOP",
|
"QWERTYUIOP",
|
||||||
"ASDFGHJKL",
|
"ASDFGHJKL",
|
||||||
"ZXCVBNM"
|
"ZXCVBNM"
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Draw game title */
|
/* Draw game title */
|
||||||
void draw_title(WINDOW* win, int y, const char* difficulty) {
|
void draw_title(WINDOW *win, int y, const char *difficulty) {
|
||||||
char title[256];
|
char title[256];
|
||||||
int height, width, x;
|
int height, width, x;
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ void draw_title(WINDOW* win, int y, const char* difficulty) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Draw the game board */
|
/* Draw the game board */
|
||||||
void draw_board(WINDOW* win, GameState* game, int y) {
|
void draw_board(WINDOW *win, GameState *game, int y) {
|
||||||
int height, width, board_x;
|
int height, width, board_x;
|
||||||
int row, col, x_pos, y_pos;
|
int row, col, x_pos, y_pos;
|
||||||
char cell[4];
|
char cell[4];
|
||||||
@ -41,7 +41,7 @@ void draw_board(WINDOW* win, GameState* game, int y) {
|
|||||||
y_pos = y + row * 2;
|
y_pos = y + row * 2;
|
||||||
|
|
||||||
if (row < game->guess_count) {
|
if (row < game->guess_count) {
|
||||||
/* Draw completed guess */
|
/* Draw a completed guess */
|
||||||
for (col = 0; col < WORD_LENGTH; col++) {
|
for (col = 0; col < WORD_LENGTH; col++) {
|
||||||
x_pos = board_x + col * 4;
|
x_pos = board_x + col * 4;
|
||||||
sprintf(cell, "[%c]", game->guesses[row][col]);
|
sprintf(cell, "[%c]", game->guesses[row][col]);
|
||||||
@ -75,10 +75,10 @@ void draw_board(WINDOW* win, GameState* game, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Draw visual keyboard */
|
/* Draw visual keyboard */
|
||||||
void draw_keyboard(WINDOW* win, GameState* game, int y) {
|
void draw_keyboard(WINDOW *win, GameState *game, int y) {
|
||||||
int height, width;
|
int height, width;
|
||||||
int row_idx, x, y_pos, i;
|
int row_idx, x, y_pos, i;
|
||||||
const char* row;
|
const char *row;
|
||||||
char letter;
|
char letter;
|
||||||
int status, color;
|
int status, color;
|
||||||
|
|
||||||
@ -98,10 +98,14 @@ void draw_keyboard(WINDOW* win, GameState* game, int y) {
|
|||||||
status = game->letter_status[letter - 'A'];
|
status = game->letter_status[letter - 'A'];
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case STATUS_CORRECT: color = COLOR_CORRECT; break;
|
case STATUS_CORRECT: color = COLOR_CORRECT;
|
||||||
case STATUS_PRESENT: color = COLOR_PRESENT; break;
|
break;
|
||||||
case STATUS_ABSENT: color = COLOR_ABSENT; break;
|
case STATUS_PRESENT: color = COLOR_PRESENT;
|
||||||
default: color = COLOR_UNUSED; break;
|
break;
|
||||||
|
case STATUS_ABSENT: color = COLOR_ABSENT;
|
||||||
|
break;
|
||||||
|
default: color = COLOR_UNUSED;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (color == COLOR_UNUSED) {
|
if (color == COLOR_UNUSED) {
|
||||||
@ -119,7 +123,7 @@ void draw_keyboard(WINDOW* win, GameState* game, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Draw centered message */
|
/* Draw centered message */
|
||||||
void draw_message(WINDOW* win, const char* message, int y, int color_pair) {
|
void draw_message(WINDOW *win, const char *message, int y, int color_pair) {
|
||||||
int height, width, x;
|
int height, width, x;
|
||||||
|
|
||||||
getmaxyx(win, height, width);
|
getmaxyx(win, height, width);
|
||||||
@ -131,8 +135,8 @@ void draw_message(WINDOW* win, const char* message, int y, int color_pair) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Draw game instructions */
|
/* Draw game instructions */
|
||||||
void draw_instructions(WINDOW* win, int y) {
|
void draw_instructions(WINDOW *win, int y) {
|
||||||
const char* instructions[] = {
|
const char *instructions[] = {
|
||||||
"Guess the 5-letter word in 6 tries!",
|
"Guess the 5-letter word in 6 tries!",
|
||||||
"",
|
"",
|
||||||
"Colors: GREEN=Correct, YELLOW=Wrong position, RED=Not in word",
|
"Colors: GREEN=Correct, YELLOW=Wrong position, RED=Not in word",
|
||||||
|
|||||||
12
src/words.c
12
src/words.c
@ -10,7 +10,7 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
/* Convert string to uppercase */
|
/* Convert string to uppercase */
|
||||||
void to_upper(char* str) {
|
void to_upper(char *str) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; str[i]; i++) {
|
for (i = 0; str[i]; i++) {
|
||||||
str[i] = toupper(str[i]);
|
str[i] = toupper(str[i]);
|
||||||
@ -18,8 +18,8 @@ void to_upper(char* str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Load words from file */
|
/* Load words from 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 filepath[MAX_FILENAME];
|
||||||
char line[32];
|
char line[32];
|
||||||
char word[WORD_LENGTH + 1];
|
char word[WORD_LENGTH + 1];
|
||||||
@ -41,7 +41,7 @@ int load_words(GameState* game, const char* filename) {
|
|||||||
while (fgets(line, sizeof(line), file) && game->word_count < MAX_WORDS) {
|
while (fgets(line, sizeof(line), file) && game->word_count < MAX_WORDS) {
|
||||||
/* Remove newline and whitespace */
|
/* Remove newline and whitespace */
|
||||||
len = strlen(line);
|
len = strlen(line);
|
||||||
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r' || line[len-1] == ' ')) {
|
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r' || line[len - 1] == ' ')) {
|
||||||
line[--len] = '\0';
|
line[--len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,14 +60,14 @@ int load_words(GameState* game, const char* filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Select random target word */
|
/* Select random target word */
|
||||||
srand((unsigned int)time(NULL));
|
srand((unsigned int) time(NULL));
|
||||||
strcpy(game->target_word, game->words[rand() % game->word_count]);
|
strcpy(game->target_word, game->words[rand() % game->word_count]);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if word exists in word list */
|
/* Check if word exists in word list */
|
||||||
int is_valid_word(GameState* game, const char* word) {
|
int is_valid_word(GameState *game, const char *word) {
|
||||||
int i;
|
int i;
|
||||||
char upper_word[WORD_LENGTH + 1];
|
char upper_word[WORD_LENGTH + 1];
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user