This commit is contained in:
Andrew Haynes
2026-02-27 09:18:29 -05:00
parent 5e06151e84
commit a27162ca2d
+17 -16
View File
@@ -1,24 +1,25 @@
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
const char p1 = 'x', p2 = 'o';
char temp = 'x';
char buf[9] = { " " };
static const unsigned char wins[8][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
{ 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 },
{ 0, 4, 8 }, { 2, 4, 6 } };
char buf[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
int
check_line(const char id)
{
for (int i = 0; i < sizeof(wins) / sizeof(wins[0]); i++)
{
if ((buf[wins[i][0]] == id && buf[wins[i][1]] == id &&
buf[wins[i][2]] == id))
{
return 1;
int check_line(const char id) {
unsigned int player_mask = 0;
for (int i = 0; i < 9; i++) {
if (buf[i] == id) {
player_mask |= (1 << i); // This "turns on" the i-th bit
}
}
static const int win_patterns[8] = {
7, 56, 448, // Rows: 0-1-2, 3-4-5, 6-7-8
73, 146, 292, // Cols: 0-3-6, 1-4-7, 2-5-8
273, 84 // Diagonals: 0-4-8, 2-4-6
};
for (int i = 0; i < 8; i++) {
if ((player_mask & win_patterns[i]) == win_patterns[i]) {
return 1; // Win found!
}
}
return 0;
@@ -95,7 +96,7 @@ end:
char dummy;
read(STDIN_FILENO, &dummy, 1);
write(1, "\033[?25h", 5);
write(1, "\033[?25h", 6);
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return 0;
}