Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Created January 9, 2025 11:24
Show Gist options
  • Save alirezaarzehgar/8ecaff2800013da667e6f88e261d9761 to your computer and use it in GitHub Desktop.
Save alirezaarzehgar/8ecaff2800013da667e6f88e261d9761 to your computer and use it in GitHub Desktop.
dooz game
#include <asm-generic/errno.h>
#include <stdio.h>
#include <stdbool.h>
enum {
E = 0,
P1 = 'X',
P2 = 'O'
};
bool running = true;
int pmap[3][3] = {};
void print_board()
{
for (int i = 0; i < 3; i++) {
printf("\n#############\n# ");
for (int j = 0; j < 3; j++) {
char c = pmap[i][j];
if (c == E)
c = (i*3+j) + '1';
printf("%c # ", c);
}
}
printf("\n#############\n");
}
void check4win(int player)
{
int sum;
for (int i = 0; i < 3; i++) {
sum = 0;
for (int j = 0; j < 3; j++)
sum += pmap[i][j];
if (sum == 3*player)
goto win;
sum = 0;
for (int j = 0; j < 3; j++)
sum += pmap[j][i];
if (sum == 3*player)
goto win;
}
if (pmap[0][0]+pmap[1][1]+pmap[2][2] == 3*player ||
pmap[0][2]+pmap[1][1]+pmap[2][0] == 3*player)
goto win;
return;
win:
printf("%c is winner!\n", player);
running = false;
}
void pick_box(int n, int player)
{
int boxcnt = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (n-1 == (i*3+j))
pmap[i][j] = player;
}
}
}
int main()
{
int player = P1;
while (1) {
int n;
print_board();
check4win(P1);
check4win(P2);
if (!running)
break;
printf("Enter box number: ");
scanf("%d", &n);
pick_box(n, player);
if (player == P1)
player = P2;
else
player = P1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment