Some optimizations.

This commit is contained in:
bʰedoh₂ swé 2025-05-25 16:59:44 +05:00
parent b569aa3984
commit e1e670abdf

18
boot.c
View File

@ -8,7 +8,7 @@ typedef enum : char {
NOT = 'O'
} Cell;
Cell* board = ((Cell*)0x7F00);
Cell* const board = (Cell*)0x7F00;
HEDLEY_ALWAYS_INLINE char readchar(void);
HEDLEY_ALWAYS_INLINE bool checkwin(Cell c);
@ -24,9 +24,8 @@ void _start(void) {
"start:\n"
);
Cell cur = CROSS;
for (char i = 0; i < 3; i++)
for (char j = 0; j < 3; j++)
board[i * 3 + j] = NONE;
for (unsigned char i = 0; i < 9; i++)
board[i] = NONE;
while (1) {
printboard();
char k = readchar();
@ -45,10 +44,7 @@ void _start(void) {
board[c] = cur;
if (checkwin(cur))
break;
if (cur == CROSS)
cur = NOT;
else
cur = CROSS;
cur ^= (CROSS ^ NOT);
}
putchar(cur);
printboard();
@ -83,10 +79,8 @@ void printboard(void) {
HEDLEY_ALWAYS_INLINE
bool checkwin(Cell c) {
for (size_t i = 0; i <= 2; i++)
if ((board[i] == c) && (board[i + 3] == c) && (board[i + 6] == c))
return true;
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[i] == c) && (board[i + 3] == c) && (board[i + 6] == c)) &&
((board[3*i] == c) && (board[3*i + 1] == c) && (board[3*i + 2] == c)))
return true;
if (board[0] == c && board[4] == c && board[8] == c)
return true;