|
|
|
@ -17,6 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <stddef.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <string.h> |
|
|
|
@ -28,42 +29,43 @@ Position notation_tile_to_position(TileNotation notation) {
|
|
|
|
|
Position pos; |
|
|
|
|
switch (notation.file) { |
|
|
|
|
case 'a': { |
|
|
|
|
pos.column = 1;
|
|
|
|
|
pos.column = 0;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'b': { |
|
|
|
|
pos.column = 2;
|
|
|
|
|
pos.column = 1;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'c': { |
|
|
|
|
pos.column = 3;
|
|
|
|
|
pos.column = 2;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'd': { |
|
|
|
|
pos.column = 4;
|
|
|
|
|
pos.column = 3;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'e': { |
|
|
|
|
pos.column = 5;
|
|
|
|
|
pos.column = 4;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'f': { |
|
|
|
|
pos.column = 6;
|
|
|
|
|
pos.column = 5;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'g': { |
|
|
|
|
pos.column = 7;
|
|
|
|
|
pos.column = 6;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case 'h': { |
|
|
|
|
pos.column = 8;
|
|
|
|
|
pos.column = 7;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
default: { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
pos.row = notation.rank; |
|
|
|
|
// pos.row = notation.rank - 1;
|
|
|
|
|
pos.row = (8 - (notation.rank)); |
|
|
|
|
|
|
|
|
|
return pos; |
|
|
|
|
} |
|
|
|
@ -101,7 +103,7 @@ TileNotation notation_tile_from_string(const char* tile_string) {
|
|
|
|
|
|
|
|
|
|
TileNotation tile; |
|
|
|
|
tile.file = tile_string[0]; |
|
|
|
|
tile.rank = (unsigned int) tile_string[1]; |
|
|
|
|
tile.rank = (unsigned int) (tile_string[1] - '0'); // Convert to proper int
|
|
|
|
|
|
|
|
|
|
return tile; |
|
|
|
|
} |
|
|
|
@ -239,6 +241,28 @@ int notation_is_piece_char(char character) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Returns a piece deduced from character, returns NONE piece if couldn't deduce a piece type. |
|
|
|
|
Big characters are for WHITE pieces, small for BLACK pieces. |
|
|
|
|
*/ |
|
|
|
|
Piece notation_piece_from_char(char character) { |
|
|
|
|
if (character == 'K') return piece_new(WHITE, KING); |
|
|
|
|
else if (character == 'k') return piece_new(BLACK, KING); |
|
|
|
|
else if (character == 'R') return piece_new(WHITE, ROOK); |
|
|
|
|
else if (character == 'r') return piece_new(BLACK, ROOK); |
|
|
|
|
else if (character == 'B') return piece_new(WHITE, BISHOP); |
|
|
|
|
else if (character == 'b') return piece_new(BLACK, BISHOP); |
|
|
|
|
else if (character == 'N') return piece_new(WHITE, KNIGHT); |
|
|
|
|
else if (character == 'n') return piece_new(BLACK, KNIGHT); |
|
|
|
|
else if (character == 'P') return piece_new(WHITE, PAWN); |
|
|
|
|
else if (character == 'p') return piece_new(BLACK, PAWN); |
|
|
|
|
else if (character == 'Q') return piece_new(WHITE, QUEEN); |
|
|
|
|
else if (character == 'q') return piece_new(BLACK, QUEEN); |
|
|
|
|
|
|
|
|
|
return piece_new(WHITE, NONE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* notation_move_to_string(MoveNotation move) { |
|
|
|
|
char* move_notation = (char*) malloc(sizeof(char) * 10); |
|
|
|
|
if (!move_notation) { |
|
|
|
@ -302,8 +326,20 @@ char* notation_move_to_string(MoveNotation move) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Accepts a move string which consists of an origin square, piece name |
|
|
|
|
(upper case for WHITE, lower case for BLACK) and a square to move to
|
|
|
|
|
in algebraic notation. |
|
|
|
|
|
|
|
|
|
Returns a notated move with origin, target and piece values set. Move type is to be |
|
|
|
|
determined by the engine. |
|
|
|
|
|
|
|
|
|
Returns NULL if technical errors were encountered, move_string does not contain enough information |
|
|
|
|
*/ |
|
|
|
|
MoveNotation* notation_move_from_string(const char* move_string) { |
|
|
|
|
if (!move_string) { |
|
|
|
|
unsigned int len = strlen(move_string); |
|
|
|
|
|
|
|
|
|
if (!move_string || len < 5) { |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -312,11 +348,25 @@ MoveNotation* notation_move_from_string(const char* move_string) {
|
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// printf();
|
|
|
|
|
if (notation_is_piece_char(move_string[0])) { |
|
|
|
|
|
|
|
|
|
// Origin tile
|
|
|
|
|
TileNotation origin = notation_tile_from_string(move_string); |
|
|
|
|
|
|
|
|
|
// Piece
|
|
|
|
|
Piece piece = notation_piece_from_char(move_string[2]); |
|
|
|
|
if (piece.type == NONE) { |
|
|
|
|
// No piece information given
|
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Target tile
|
|
|
|
|
TileNotation target = notation_tile_from_string(move_string + 3); |
|
|
|
|
|
|
|
|
|
move->origin = origin; |
|
|
|
|
move->piece = piece; |
|
|
|
|
move->target = target; |
|
|
|
|
move->move_type = 0; |
|
|
|
|
|
|
|
|
|
return move; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|