;
This commit is contained in:
+20
-19
@@ -1,27 +1,28 @@
|
|||||||
#include <string.h>
|
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
const char p1 = 'x', p2 = 'o';
|
const char p1 = 'x', p2 = 'o';
|
||||||
char temp = 'x';
|
char temp = 'x';
|
||||||
char buf[9] = { " " };
|
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 } };
|
|
||||||
|
|
||||||
int
|
int check_line(const char id) {
|
||||||
check_line(const char id)
|
unsigned int player_mask = 0;
|
||||||
{
|
for (int i = 0; i < 9; i++) {
|
||||||
for (int i = 0; i < sizeof(wins) / sizeof(wins[0]); i++)
|
if (buf[i] == id) {
|
||||||
{
|
player_mask |= (1 << i); // This "turns on" the i-th bit
|
||||||
if ((buf[wins[i][0]] == id && buf[wins[i][1]] == id &&
|
}
|
||||||
buf[wins[i][2]] == id))
|
}
|
||||||
{
|
static const int win_patterns[8] = {
|
||||||
return 1;
|
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
|
||||||
return 0;
|
};
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
if ((player_mask & win_patterns[i]) == win_patterns[i]) {
|
||||||
|
return 1; // Win found!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -95,7 +96,7 @@ end:
|
|||||||
|
|
||||||
char dummy;
|
char dummy;
|
||||||
read(STDIN_FILENO, &dummy, 1);
|
read(STDIN_FILENO, &dummy, 1);
|
||||||
write(1, "\033[?25h", 5);
|
write(1, "\033[?25h", 6);
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user