Skip to content

Commit

Permalink
Better names
Browse files Browse the repository at this point in the history
  • Loading branch information
noncombatant committed May 14, 2023
1 parent d818a79 commit b3dd631
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions robotfindskitten.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ static const int HeaderSize = 1;
static const int FrameThickness = 1;
static const unsigned int White = 7;

typedef struct ScreenObject {
typedef struct Item {
int x;
int y;
unsigned int color;
char* icon;
} ScreenObject;
} Item;

static struct GameState {
int lines;
int columns;
bool screen_has_color;
unsigned int border_color;
size_t item_count;
ScreenObject items[ArrayCount(Messages)];
Item items[ArrayCount(Messages)];
size_t message_count;
const char** messages;
size_t icon_count;
Expand Down Expand Up @@ -176,8 +176,8 @@ static char* RandomIcon(void) {
return r;
}

static bool ScreenObjectsEqual(const ScreenObject a, const ScreenObject b) {
return a.x == b.x && a.y == b.y;
static bool ItemsCoincide(const Item* a, const Item* b) {
return a->x == b->x && a->y == b->y;
}

typedef enum {
Expand Down Expand Up @@ -243,22 +243,20 @@ static void InitializeGame(size_t item_count) {
do {
GameState.items[Kitten].y = RandomY();
GameState.items[Kitten].x = RandomX();
} while (ScreenObjectsEqual(GameState.items[Robot], GameState.items[Kitten]));
} while (ItemsCoincide(&GameState.items[Robot], &GameState.items[Kitten]));

for (size_t i = Bogus; i < GameState.item_count; ++i) {
GameState.items[i].icon = RandomIcon();
while (true) {
GameState.items[i].y = RandomY();
GameState.items[i].x = RandomX();
if (ScreenObjectsEqual(GameState.items[Robot], GameState.items[i])) {
continue;
}
if (ScreenObjectsEqual(GameState.items[Kitten], GameState.items[i])) {
if (ItemsCoincide(&GameState.items[Robot], &GameState.items[i]) ||
ItemsCoincide(&GameState.items[Kitten], &GameState.items[i])) {
continue;
}
size_t j;
for (j = 0; j < i; ++j) {
if (ScreenObjectsEqual(GameState.items[j], GameState.items[i])) {
if (ItemsCoincide(&GameState.items[j], &GameState.items[i])) {
break;
}
}
Expand All @@ -283,20 +281,19 @@ static void InitializeGame(size_t item_count) {
}
}

static void Draw(const ScreenObject* o) {
static void ItemDraw(const Item* o) {
mvprintw(o->y, o->x, "%s", o->icon);
}

static void ShowMessage(const char* message) {
static void MessageDraw(const char* message) {
int y, x;
getyx(curscr, y, x);
if (GameState.screen_has_color) {
attrset(COLOR_PAIR(White));
}
move(0, 0);
clrtoeol();
move(0, 0);
printw("%.*s", GameState.columns, message);
mvprintw(0, 0, "%.*s", GameState.columns, message);
move(y, x);
refresh();
}
Expand Down Expand Up @@ -325,7 +322,7 @@ static void RedrawScreen(void) {
attroff(attributes);
}
for (size_t i = 0; i < GameState.item_count; ++i) {
Draw(&GameState.items[i]);
ItemDraw(&GameState.items[i]);
}
move(GameState.items[Robot].y, GameState.items[Robot].x);
if (GameState.screen_has_color) {
Expand Down Expand Up @@ -374,20 +371,20 @@ static void PlayAnimation(bool approach_from_right) {
clrtoeol();
const int animation_meet = (COLS / 2);

ScreenObject kitten;
Item kitten;
memcpy(&kitten, &GameState.items[Kitten], sizeof(kitten));

ScreenObject robot;
Item robot;
memcpy(&robot, &GameState.items[Robot], sizeof(robot));

GameState.items[Robot].y = GameState.items[Kitten].y = 0;
for (int i = 4; i > 0; --i) {
printf("\a");

GameState.items[Robot].icon = " ";
Draw(&GameState.items[Robot]);
ItemDraw(&GameState.items[Robot]);
GameState.items[Kitten].icon = " ";
Draw(&GameState.items[Kitten]);
ItemDraw(&GameState.items[Kitten]);

GameState.items[Robot].icon = "🤖";
GameState.items[Kitten].icon = "😺";
Expand All @@ -399,16 +396,16 @@ static void PlayAnimation(bool approach_from_right) {
GameState.items[Kitten].x = animation_meet + i;
}

Draw(&kitten);
Draw(&robot);
ItemDraw(&kitten);
ItemDraw(&robot);

Draw(&GameState.items[Robot]);
Draw(&GameState.items[Kitten]);
ItemDraw(&GameState.items[Robot]);
ItemDraw(&GameState.items[Kitten]);
move(GameState.items[Robot].y, GameState.items[Robot].x);
refresh();
sleep(1);
}
ShowMessage(WinMessage);
MessageDraw(WinMessage);
curs_set(0);
sleep(1);
}
Expand Down Expand Up @@ -502,7 +499,7 @@ static void MainLoop(void) {
HandleResize();
break;
default:
ShowMessage("Use direction keys or Q to quit.");
MessageDraw("Use direction keys or Q to quit.");
break;
}

Expand All @@ -518,12 +515,12 @@ static void MainLoop(void) {
case TouchTestResultNone:
// Robot moved.
// GameState.items[Robot].icon = " ";
// Draw(&GameState.items[Robot]);
// ItemDraw(&GameState.items[Robot]);
GameState.items[Robot].y = y;
GameState.items[Robot].x = x;
// GameState.items[Robot].icon = "🤖";
move(y, x);
Draw(&GameState.items[Robot]);
ItemDraw(&GameState.items[Robot]);
// move(y, x);
// refresh();
// Using RedrawScreen instead of refresh restores the icon the
Expand All @@ -537,7 +534,7 @@ static void MainLoop(void) {
PlayAnimation(approach_from_right);
Finish(EXIT_SUCCESS);
case TouchTestResultNonKitten:
ShowMessage(GameState.messages[item_number]);
MessageDraw(GameState.messages[item_number]);
break;
}
}
Expand Down

0 comments on commit b3dd631

Please sign in to comment.