Skip to content

Instantly share code, notes, and snippets.

@PanJarda
Last active December 19, 2017 11:45
Show Gist options
  • Save PanJarda/403407ca21e89393533549817b0bd10f to your computer and use it in GitHub Desktop.
Save PanJarda/403407ca21e89393533549817b0bd10f to your computer and use it in GitHub Desktop.
Turkish draughts in c
#include <stdio.h>
#include <stdlib.h>
#define BSIZE 8
#define ERRSCAN 1
#define ERRMEM 2
typedef struct vec {
char x;
char y;
} vec;
typedef struct move {
vec a;
vec b;
} move;
vec relm = {0, 0};
vec takingpc = {0, 0};
move mv = {{0, 0}, {0, 0}};
char movingpc = 0;
int player = -1;
char board[BSIZE][BSIZE] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, -2, 0, 0, 1, 0},
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
};
char pieces[5] = {'k', 'm', '.', 'M', 'K'};
char piecemvs[5][15][15] = {
{
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0,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
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
},
{
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1}
}
};
vec *vecalloc() {
return (vec *) malloc(sizeof(vec));
}
move *movalloc() {
return (move *) malloc(sizeof(move));
}
int scanmv() {
char x0, y0, x1, y1;
if (scanf(" %c%c %c%c", &x0, &y0, &x1, &y1) != 4)
return 1;
mv.a.x = x0 - 'a';
mv.a.y = y0 - '1';
mv.b.x = x1 - 'a';
mv.b.y = y1 - '1';
return 0;
}
void printbrd() {
for (int i = BSIZE; i > 0;) {
printf("%d", i--);
for (int j = 0; j < BSIZE; j++)
printf(" %c ", pieces[board[i][j] + 2]);
putchar('\n');
}
printf(" a b c d e f g h\n");
}
int isvalidm() {
movingpc = board[mv.a.y][mv.a.x];
if ((movingpc > 0 && player < 0) || (movingpc < 0 && player > 0))
return 0;
/* print chosen piece */
//printf("moving piece: %c\n", pieces[movingpc + 2]);
relm.x = mv.b.x - mv.a.x;
relm.y = mv.b.y - mv.a.y;
/* print relative mv */
/* printf("[%d, %d]\n", relm.x, relm.y); */
/* check valid mv */
char mvtype = piecemvs[movingpc + 2][relm.y + 7][relm.x + 7];
if (mvtype == 0 || board[mv.b.y][mv.b.x])
return 0;
int sgnx = (relm.x > 0) - (relm.x < 0);
int sgny = (relm.y > 0) - (relm.y < 0);
int dx = sgnx;
int dy = sgny;
char pcbtwn;
char pctaken = 0;
while (dy != relm.y || dx != relm.x) {
pcbtwn = board[mv.a.y + dy][mv.a.x + dx];
if (pcbtwn) {
if ((pcbtwn > 0 && movingpc > 0) || (pcbtwn < 0 && movingpc < 0) || pctaken)
return 0;
takingpc.x = mv.a.x + dx;
takingpc.y = mv.a.y + dy;
pctaken++;
}
dx += sgnx;
dy += sgny;
}
if (mvtype == 2 && !pctaken)
return 0;
if (pctaken)
return 2;
else
return 1;
}
void applymv(int mvtype) {
board[mv.a.y][mv.a.x] = 0;
board[mv.b.y][mv.b.x] = movingpc;
if (mvtype == 2) {
/*printf("taken: %c\n", pieces[board[takingpc.y][takingpc.x] + 2]);*/
board[takingpc.y][takingpc.x] = 0;
}
if ((mv.b.y == 0 || mv.b.y == 7) && movingpc == player)
board[mv.b.y][mv.b.x] = player * 2;
}
int main() {
while(1) {
printbrd();
printf("Player%d> ", player);
if (scanmv())
exit(ERRSCAN);
int mvtype = isvalidm();
/* printf("mvtype: %d\n", mvtype); */
/* apply mv */
if (mvtype == 0) {
printf("Invalid move\n");
continue;
}
applymv(mvtype);
player = player * -1;
}
}
pieces:
4
0 M 6 3
-1 0
0 1
-1 0
-2 0
0 2
2 0
1 m 6 3
-1 0
0 1
-1 0
-2 0
0 2
2 0
board:
8 8
. . . . . . . .
M M M M M M M M
M M M M M M M M
. . . . . . . .
. . . . . . . .
m m m m m m m m
m m m m m m m m
. . . . . . . .
flags:
player_opening 0
optional_taking 1
optional_changing 0
draw 30
changes:
2
8 m k
0 M K
game:
take m a4 a6 M a5 take m a6 a8 M a7 change m k
move M a6 a7
move k a8 b8
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment