diff --git a/src/board.c b/src/board.c index 9850daf..864435f 100644 --- a/src/board.c +++ b/src/board.c @@ -4,3 +4,16 @@ PieceColors piece_getColor(const Piece piece) { return Black & piece; } Piece piece_getColorlessPiece(const Piece piece) { return piece_getColor(piece) ^ piece; } + +uint board_getIndex(const Coordinates coords) { + return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8); +} + +bool board_movePiece(Board board, const Coordinates from, + const Coordinates to) { + uint from_index = board_getIndex(from); + uint to_index = board_getIndex(to); + board[to_index].piece = board[from_index].piece; + board[from_index].piece = -1; + return true; +} diff --git a/src/board.h b/src/board.h index 0c22a98..ef4c607 100644 --- a/src/board.h +++ b/src/board.h @@ -1,13 +1,18 @@ #ifndef __BOARD_HEADER #include "common.h" + typedef int Piece; typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces; typedef enum { White = 0, Black = 8 } PieceColors; typedef struct { - Piece piece; uint row; uint column; +} Coordinates; + +typedef struct { + Piece piece; + Coordinates coords; } Square; typedef Square Board[64]; @@ -15,4 +20,6 @@ typedef Square Board[64]; PieceColors piece_getColor(const Piece piece); Piece piece_getColorlessPiece(const Piece piece); +bool board_movePiece(Board board, const Coordinates from, const Coordinates to); + #endif // !__BOARD_HEADER diff --git a/src/common.h b/src/common.h index ee70eac..5969acf 100644 --- a/src/common.h +++ b/src/common.h @@ -1,6 +1,7 @@ #ifndef __COMMON_HEADER #include #include + typedef unsigned int uint; char chrToLower(char chr); diff --git a/src/fen.c b/src/fen.c index 9fc4218..73f5454 100644 --- a/src/fen.c +++ b/src/fen.c @@ -1,4 +1,5 @@ #include "fen.h" + // Starter fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR void fen_toBoard(const FEN fen, Board board) { uint i = 0; @@ -43,7 +44,8 @@ void fen_toBoard(const FEN fen, Board board) { } PieceColors color = is_upper_case ? White : Black; Piece piece = p ^ color; - Square square = {.piece = piece, .row = row, .column = ++column}; + Coordinates coords = {.row = row, .column = ++column}; + Square square = {.piece = piece, .coords = coords}; board[board_i++] = square; } else if (fen_char > '0' && fen_char < '9') { // if the char is a number bet. 1 and 8 @@ -51,7 +53,8 @@ void fen_toBoard(const FEN fen, Board board) { int offset = fen_char - '0'; if (offset < 9) { for (int j = 0; j < offset; j++) { - Square square = {.piece = -1, .row = row, .column = ++column}; + Coordinates coords = {.row = row, .column = ++column}; + Square square = {.piece = -1, .coords = coords}; board[board_i++] = square; } } else { diff --git a/src/main.c b/src/main.c index 5137f03..c4aaeb0 100644 --- a/src/main.c +++ b/src/main.c @@ -2,8 +2,11 @@ int main() { Board board; - FEN fen = "rnbqkbnr/ppppp1pp/5p2/8/8/8/PPPPPPPP/RNBQKBNR"; + FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; fen_toBoard(fen, board); + Coordinates from = {.row = 8, .column = 3}; + Coordinates to = {.row = 6, .column = 3}; + board_movePiece(board, from, to); fen_fromBoard(board, fen); printf("%s", fen); return 0;