Started to make the piece move

This commit is contained in:
cdricms
2023-12-21 00:48:10 +01:00
parent 11781e37ff
commit 619d03ab53
5 changed files with 31 additions and 4 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -1,6 +1,7 @@
#ifndef __COMMON_HEADER
#include <stdbool.h>
#include <stdio.h>
typedef unsigned int uint;
char chrToLower(char chr);

View File

@@ -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 {

View File

@@ -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;