Make the code ever so slightly less stupid.

This commit is contained in:
bʰedoh₂ swé 2025-05-01 20:11:16 +05:00
parent 3a02a0ee8a
commit 60d8f9bc31

20
boot.c
View File

@ -2,14 +2,14 @@
#include <stdbool.h> #include <stdbool.h>
#include "hedley.h" #include "hedley.h"
#define BOARD ((Cell*)0x7F00)
typedef enum : char { typedef enum : char {
NONE = ' ', NONE = ' ',
CROSS = 'X', CROSS = 'X',
NOT = 'O' NOT = 'O'
} Cell; } Cell;
Cell* board = ((Cell*)0x7F00);
HEDLEY_ALWAYS_INLINE char readchar(void); HEDLEY_ALWAYS_INLINE char readchar(void);
HEDLEY_ALWAYS_INLINE bool checkwin(Cell c); HEDLEY_ALWAYS_INLINE bool checkwin(Cell c);
HEDLEY_NEVER_INLINE void putchar(char); HEDLEY_NEVER_INLINE void putchar(char);
@ -26,7 +26,7 @@ void _start(void) {
Cell cur = CROSS; Cell cur = CROSS;
for (char i = 0; i < 3; i++) for (char i = 0; i < 3; i++)
for (char j = 0; j < 3; j++) for (char j = 0; j < 3; j++)
BOARD[i * 3 + j] = NONE; board[i * 3 + j] = NONE;
while (1) { while (1) {
printboard(); printboard();
char k = readchar(); char k = readchar();
@ -40,9 +40,9 @@ void _start(void) {
else else
o = (0x2C - 8); o = (0x2C - 8);
size_t c = numpadkeys[k - o]; size_t c = numpadkeys[k - o];
if (BOARD[c] != NONE) if (board[c] != NONE)
continue; continue;
BOARD[c] = cur; board[c] = cur;
if (checkwin(cur)) if (checkwin(cur))
break; break;
if (cur == CROSS) if (cur == CROSS)
@ -73,7 +73,7 @@ void printboard(void) {
); );
for (char i = 0; i < 3; i++) { for (char i = 0; i < 3; i++) {
for (char j = 0; j < 3; j++) { for (char j = 0; j < 3; j++) {
putchar(BOARD[i * 3 + j]); putchar(board[i * 3 + j]);
} }
putchar('\n'); putchar('\n');
putchar('\r'); putchar('\r');
@ -83,14 +83,14 @@ void printboard(void) {
HEDLEY_ALWAYS_INLINE HEDLEY_ALWAYS_INLINE
bool checkwin(Cell c) { bool checkwin(Cell c) {
for (size_t i = 0; i <= 2; i++) for (size_t i = 0; i <= 2; i++)
if ((BOARD[i] == c) && (BOARD[i + 3] == c) && (BOARD[i + 6] == c)) if ((board[i] == c) && (board[i + 3] == c) && (board[i + 6] == c))
return true; return true;
for (size_t i = 0; i <= 2; i++) for (size_t i = 0; i <= 2; i++)
if ((BOARD[3*i] == c) && (BOARD[3*i + 1] == c) && (BOARD[3*i + 2] == c)) if ((board[3*i] == c) && (board[3*i + 1] == c) && (board[3*i + 2] == c))
return true; return true;
if (BOARD[0] == c && BOARD[4] == c && BOARD[8] == c) if (board[0] == c && board[4] == c && board[8] == c)
return true; return true;
if (BOARD[2] == c && BOARD[4] == c && BOARD[6] == c) if (board[2] == c && board[4] == c && board[6] == c)
return true; return true;
return false; return false;
} }