blob: 62b469264258136ccd746c75aa416c960a72e895 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "pieces.h"
void print_board(int board[BOARD_SIZE][BOARD_SIZE]) {
for (int rank = BOARD_SIZE - 1; rank >= 0; rank--) {
printf("%i|", rank + 1);
for (int file = 0; file < BOARD_SIZE; file++) {
int piece = board[rank][file];
printf("%s ", pieces[piece]);
}
printf("\n");
}
printf(" a b c d e f g h\n");
}
void parse_FEN(char* FEN, int board[BOARD_SIZE][BOARD_SIZE]) {
// TODO: parse everything, not position only
int rank = 7;
int file = 0;
size_t lenght = strlen(FEN);
for (int k = 0; k < lenght; k++) {
if (FEN[k] > '0' && FEN[k] <= '8') {
int last_file = file + FEN[k] - '0';
for (file; file < last_file; file++) {
board[rank][file] = EMPTY;
}
} else {
switch (FEN[k]) {
case 'r':
board[rank][file] = BLACK | ROOK;
break;
case 'R':
board[rank][file] = ROOK;
break;
case 'n':
board[rank][file] = BLACK | KNIGHT;
break;
case 'N':
board[rank][file] = KNIGHT;
break;
case 'b':
board[rank][file] = BLACK | BISHOP;
break;
case 'B':
board[rank][file] = BISHOP;
break;
case 'q':
board[rank][file] = BLACK | QUEEN;
break;
case 'Q':
board[rank][file] = QUEEN;
break;
case 'k':
board[rank][file] = BLACK | KING;
break;
case 'K':
board[rank][file] = KING;
break;
case 'p':
board[rank][file] = BLACK | PAWN;
break;
case 'P':
board[rank][file] = PAWN;
break;
case '/':
rank--;
file = -1; // so that it becomes 0
break;
case ' ':
return;
default:
board[rank][file] = KNIGHT;
}
file++;
}
}
}
int main() {
int board[BOARD_SIZE][BOARD_SIZE];
parse_FEN(DEFAULT_FEN, board);
print_board(board);
return 0;
}
|