converting to bit instead of varibles
This commit is contained in:
+137
-46
@@ -2,40 +2,100 @@
|
||||
#include <unistd.h>
|
||||
|
||||
const char p1 = 'x', p2 = 'o';
|
||||
char temp = 'x';
|
||||
char buf[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
|
||||
char temp = 0;
|
||||
// char buf[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
|
||||
|
||||
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
|
||||
unsigned int
|
||||
encoder(unsigned int mask, int length, int point)
|
||||
{
|
||||
int shift_amount = point * 2;
|
||||
if (length > 3)
|
||||
{
|
||||
length = 3;
|
||||
}
|
||||
unsigned int clear_mask = ~(0x03 << shift_amount);
|
||||
mask &= clear_mask;
|
||||
mask |= length << shift_amount;
|
||||
return mask;
|
||||
}
|
||||
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;
|
||||
|
||||
unsigned int
|
||||
decoder(unsigned int buf, int point)
|
||||
{
|
||||
return (buf >> (point * 2)) & 0x03;
|
||||
}
|
||||
|
||||
// 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;
|
||||
// }
|
||||
|
||||
void
|
||||
draw_board()
|
||||
draw_board(unsigned int buf)
|
||||
{
|
||||
char divider[] = " - + - + -\n";
|
||||
write(1, "\033[H\033[J", sizeof("\033[H\033[J") - 1);
|
||||
for (int i = 0; i < 9; i += 3)
|
||||
{
|
||||
char row[] = " 0 | 0 | 0\n";
|
||||
row[1] = buf[i];
|
||||
row[5] = buf[i + 1];
|
||||
row[9] = buf[i + 2];
|
||||
if (decoder(buf, i) == 1)
|
||||
{
|
||||
row[1] = 'x';
|
||||
}
|
||||
else if (decoder(buf, i) == 2)
|
||||
{
|
||||
row[1] = 'o';
|
||||
}
|
||||
else
|
||||
{
|
||||
row[1] = ' ';
|
||||
}
|
||||
if (decoder(buf, i + 1) == 1)
|
||||
{
|
||||
row[5] = 'x';
|
||||
}
|
||||
else if (decoder(buf, i + 1) == 2)
|
||||
{
|
||||
row[5] = 'o';
|
||||
}
|
||||
else
|
||||
{
|
||||
row[5] = ' ';
|
||||
}
|
||||
if (decoder(buf, i + 2) == 1)
|
||||
{
|
||||
row[9] = 'x';
|
||||
}
|
||||
else if (decoder(buf, i + 2) == 2)
|
||||
{
|
||||
row[9] = 'o';
|
||||
}
|
||||
else
|
||||
{
|
||||
row[9] = ' ';
|
||||
}
|
||||
write(1, row, sizeof(row) - 1);
|
||||
if (i < 6) write(1, divider, sizeof(divider) - 1);
|
||||
}
|
||||
@@ -44,9 +104,48 @@ draw_board()
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
void
|
||||
print_bits_int(unsigned int n)
|
||||
{
|
||||
for (int i = 31; i >= 0; i--)
|
||||
{
|
||||
if (i == 7 || i == 15 || i == 23) write(1, " ", 1);
|
||||
char bit = (n & (1 << i)) ? '1' : '0';
|
||||
write(1, &bit, 1);
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
unsigned player_mask = 0;
|
||||
player_mask = encoder(player_mask, 1, 0);
|
||||
draw_board(player_mask);
|
||||
// player_mask = encoder(player_mask, 1, 1);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 2, 3);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 2, 4);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 1, 5);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 2, 6);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 1, 7);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 1, 8);
|
||||
// print_bits_int(player_mask);
|
||||
//
|
||||
//
|
||||
// print_bits_int(decoder(player_mask, 0));
|
||||
// draw_board(player_mask);
|
||||
// player_mask = decoder(player_mask, 7);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 1, 13);
|
||||
// print_bits_int(player_mask);
|
||||
// player_mask = encoder(player_mask, 2, 14);
|
||||
// print_bits_int(player_mask);
|
||||
static struct termios oldt, newt;
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
newt = oldt;
|
||||
@@ -55,35 +154,27 @@ main()
|
||||
write(1, "\033[?25l", sizeof("\033[?25l") - 1);
|
||||
while (1)
|
||||
{
|
||||
if (check_line(p1))
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (check_line(p2))
|
||||
{
|
||||
write(1, "P2 wins!\n", sizeof("P2 wins!\n") - 1);
|
||||
break;
|
||||
}
|
||||
int filled = 0;
|
||||
unsigned char filled = 0;
|
||||
while (filled < 9)
|
||||
{
|
||||
draw_board();
|
||||
char ch;
|
||||
draw_board(player_mask);
|
||||
unsigned char ch;
|
||||
read(0, &ch, 1);
|
||||
if (ch == 'q') break;
|
||||
int idx = ch - '1';
|
||||
if (ch >= '1' && ch <= '9' && buf[idx] == ' ')
|
||||
unsigned char idx = ch - '1';
|
||||
if (ch >= '1' && ch <= '9' && decoder(player_mask, idx) != (1 | 2))
|
||||
{
|
||||
buf[idx] = temp;
|
||||
if (check_line(temp))
|
||||
{
|
||||
draw_board();
|
||||
write(1, &temp, 1);
|
||||
write(1, " wins!\n",
|
||||
sizeof(" wins!\n") - 1);
|
||||
goto end;
|
||||
}
|
||||
temp = (temp == p1) ? p2 : p1;
|
||||
// buf[idx] = temp;
|
||||
player_mask = encoder(player_mask, 1, idx);
|
||||
// if (check_line(temp))
|
||||
// {
|
||||
// draw_board(player_mask);
|
||||
// write(1, &temp, 1);
|
||||
// write(1, " wins!\n",
|
||||
// sizeof(" wins!\n") - 1);
|
||||
// goto end;
|
||||
// }
|
||||
// temp = (temp == 1) ? 2 : 1;
|
||||
filled++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user