-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.c
99 lines (85 loc) · 3.09 KB
/
io.c
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
92
93
94
95
96
97
98
99
#include "include/definitions.h"
#include "include/validate.h"
char *printSquareString(const int square) {
static char squareString[3];
int file = filesBoard[square];
int rank = ranksBoard[square];
sprintf(squareString,"%c%c", ('a' + file), ('1' + rank));
return squareString;
}
char *printMove(const int move) {
static char moveString[6];
int fileFrom = filesBoard[FROM(move)];
int rankForm = ranksBoard[FROM(move)];
int fileTo = filesBoard[TOSQ(move)];
int rankTo = ranksBoard[TOSQ(move)];
int promoted = PROMOTED(move);
if(promoted) {
char pieceChar = 'q';
if(isKn(promoted)) {
pieceChar = 'n';
}else if(isRQ(promoted) && !isBQ(promoted)) {
pieceChar = 'r';
}else if(!isRQ(promoted) && isBQ(promoted)) {
pieceChar = 'b';
}
sprintf(moveString, "%c%c%c%c%c", ('a' + fileFrom), ('1' + rankForm), ('a' + fileTo), ('1' + rankTo), pieceChar);
}else {
sprintf(moveString, "%c%c%c%c", ('a' + fileFrom), ('1' + rankForm), ('a' + fileTo), ('1' + rankTo));
}
return moveString;
}
int parseMove(char *ptrChar, board *position) {
if(ptrChar[1] > '8' || ptrChar[1] < '1') {
return NOMOVE;
}
if(ptrChar[3] > '8' || ptrChar[3] < '1') {
return NOMOVE;
}
if(ptrChar[0] > 'h' || ptrChar[0] < 'a') {
return NOMOVE;
}
if(ptrChar[2] > 'h' || ptrChar[2] < 'a') {
return NOMOVE;
}
int from = FILE_RANK_TO_SQUARE(ptrChar[0] - 'a', ptrChar[1] - '1');
int to = FILE_RANK_TO_SQUARE(ptrChar[2] - 'a', ptrChar[3] - '1');
ASSERT(squareOnBoard(from) && squareOnBoard(to));
movelist newList[1];
generateAllMoves(position, newList);
int moveNumber = 0;
int move = NOMOVE;
int promotedPiece = EMPTY_SQR;
for(moveNumber = 0; moveNumber < newList -> numberOfMoves; moveNumber++) {
move = newList -> moves[moveNumber].move;
if(FROM(move) == from && TOSQ(move) == to) {
promotedPiece = PROMOTED(move);
if(promotedPiece != EMPTY_SQR) {
if(isRQ(promotedPiece) && !isBQ(promotedPiece) && ptrChar[4] == 'r') {
return move;
}else if(!isRQ(promotedPiece) && isBQ(promotedPiece) && ptrChar[4] == 'b') {
return move;
}else if(isRQ(promotedPiece) && isBQ(promotedPiece) && ptrChar[4] == 'q') {
return move;
}else if(isKn(promotedPiece) && ptrChar[4] == 'n') {
return move;
}
continue;
}
return move;
}
}
return NOMOVE;
}
void printMoveList(const movelist *list) {
int index = 0;
int score = 0;
int move = 0;
printf("MoveList: %d\n", list -> numberOfMoves);
for(index = 0; index < list -> numberOfMoves; index++) {
move = list -> moves[index].move;
score = list -> moves[index].score;
printf("Move: %d > %s (score: %d)\n", index + 1, printMove(move), score);
}
printf("Move List Total %d Moves: \n\n", list -> numberOfMoves);
}