73 lines
1.4 KiB
C
73 lines
1.4 KiB
C
#include "game_state.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
char *word_bank[] = {
|
|
"hypothesis",
|
|
"science",
|
|
"usurp",
|
|
"conditional",
|
|
"passionate",
|
|
"orion",
|
|
NULL // sentinel value marking end
|
|
};
|
|
|
|
struct game_state {
|
|
char **curr_el;
|
|
unsigned short curr_letter_index;
|
|
unsigned short wins;
|
|
};
|
|
|
|
static struct game_state state;
|
|
|
|
void init_game_state() {
|
|
state.curr_el = word_bank;
|
|
state.curr_letter_index = 0;
|
|
state.wins = 0;
|
|
}
|
|
|
|
// Gets the word at the current element. Will always return a valid non-null pointer to first char of the word
|
|
char *get_curr_word() {
|
|
assert(state.curr_el != NULL);
|
|
assert((*state.curr_el) != NULL);
|
|
return *state.curr_el;
|
|
}
|
|
|
|
char get_curr_letter() {
|
|
char *word = get_curr_word();
|
|
return word[state.curr_letter_index];
|
|
}
|
|
|
|
int get_curr_letter_index() {
|
|
return state.curr_letter_index;
|
|
}
|
|
|
|
int get_wins() {
|
|
return state.wins;
|
|
}
|
|
|
|
// goes to the next word in the word bank, wrapping when necessary
|
|
static void go_next_word() {
|
|
if (*(state.curr_el + 1) == NULL) {
|
|
state.curr_el = word_bank;
|
|
} else {
|
|
state.curr_el++;
|
|
}
|
|
|
|
state.wins++;
|
|
state.curr_letter_index = 0;
|
|
}
|
|
|
|
// handle either going to the next letter or next word if at the end
|
|
void next() {
|
|
size_t word_len = strlen(get_curr_word());
|
|
if (state.curr_letter_index >= word_len - 1) { // at last character
|
|
go_next_word();
|
|
} else {
|
|
state.curr_letter_index++;
|
|
}
|
|
}
|