Skip to content

Commit

Permalink
Reuse ArrayShuffle to obivate InitializeMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
noncombatant committed May 14, 2023
1 parent b3dd631 commit 53e767c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
4 changes: 2 additions & 2 deletions non_kitten_items.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define ArrayCount(a) (sizeof((a)) / sizeof((a)[0]))

static const char* Messages[] = {
// Do not change these:
static char* Messages[] = {
// Do not change these; they are placeholders for Robot and Kitten.
"",
"",

Expand Down
27 changes: 8 additions & 19 deletions robotfindskitten.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static struct GameState {
size_t item_count;
Item items[ArrayCount(Messages)];
size_t message_count;
const char** messages;
char** messages;
size_t icon_count;
char** icons;
} GameState;
Expand All @@ -130,23 +130,6 @@ static bool StringsEqual(const char* a, const char* b) {
return strcmp(a, b) == 0;
}

static void InitializeMessages(void) {
GameState.messages = Messages;
GameState.message_count = ArrayCount(Messages);
assert(GameState.message_count > Bogus);
for (size_t i = Bogus; i < (GameState.message_count - 1); ++i) {
const size_t j = i + ((size_t)random() % (GameState.message_count - i));
if (i != j) {
const char* temp = GameState.messages[i];
GameState.messages[i] = GameState.messages[j];
GameState.messages[j] = temp;
}
}
assert(StringsEqual("", GameState.messages[Robot]));
assert(StringsEqual("", GameState.messages[Kitten]));
}

// TODO: Use this on Messages, too.
static void ArrayShuffle(char** array, size_t count) {
for (size_t i = 0; i < count - 1; ++i) {
const size_t j = i + ((size_t)random() % (count - i));
Expand Down Expand Up @@ -209,7 +192,13 @@ static noreturn void Finish(int signal) {
}

static void InitializeGame(size_t item_count) {
InitializeMessages();
GameState.messages = Messages;
GameState.message_count = ArrayCount(Messages);
// Shuffle only the items after the Robot and Kitten placeholders:
ArrayShuffle(&GameState.messages[Bogus], GameState.message_count - Bogus);
assert(StringsEqual("", GameState.messages[Robot]));
assert(StringsEqual("", GameState.messages[Kitten]));

GameState.item_count = Bogus + item_count;

GameState.icons = Icons;
Expand Down

0 comments on commit 53e767c

Please sign in to comment.