Modules and Makefile

This commit is contained in:
cdricms
2023-12-20 23:32:19 +01:00
parent 6cba000233
commit 11781e37ff
9 changed files with 90 additions and 44 deletions

6
src/board.c Normal file
View File

@@ -0,0 +1,6 @@
#include "board.h"
PieceColors piece_getColor(const Piece piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Piece piece) {
return piece_getColor(piece) ^ piece;
}

18
src/board.h Normal file
View File

@@ -0,0 +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;
} Square;
typedef Square Board[64];
PieceColors piece_getColor(const Piece piece);
Piece piece_getColorlessPiece(const Piece piece);
#endif // !__BOARD_HEADER

13
src/common.c Normal file
View File

@@ -0,0 +1,13 @@
#include "common.h"
char chrToLower(char chr) {
if (chr >= 'A' && chr <= 'Z')
return chr + 32;
return chr;
}
char chrToUpper(char chr) {
if (chr >= 'a' && chr <= 'z')
return chr - 32;
return chr;
}

9
src/common.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef __COMMON_HEADER
#include <stdbool.h>
#include <stdio.h>
typedef unsigned int uint;
char chrToLower(char chr);
char chrToUpper(char chr);
#endif // !__COMMON_HEADER

118
src/fen.c Normal file
View File

@@ -0,0 +1,118 @@
#include "fen.h"
// Starter fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
void fen_toBoard(const FEN fen, Board board) {
uint i = 0;
char fen_char;
uint row = 8;
uint column = 0;
uint board_i;
while ((fen_char = fen[i++]) != 0) {
if (fen_char == '/') {
row--;
column = 0;
continue;
}
// Upper case or lower case
bool is_upper_case;
if ((is_upper_case = fen_char > 'A' && fen_char < 'Z') ||
(fen_char > 'a' && fen_char < 'z')) {
char lowered_char = chrToLower(fen_char);
Pieces p;
switch (lowered_char) {
case 'p':
p = Pawn;
break;
case 'n':
p = Knight;
break;
case 'b':
p = Bishop;
break;
case 'r':
p = Rook;
break;
case 'q':
p = Queen;
break;
case 'k':
p = King;
break;
default:
printf("[ERROR] %c is not an authorized character\n", fen_char);
return;
}
PieceColors color = is_upper_case ? White : Black;
Piece piece = p ^ color;
Square square = {.piece = piece, .row = row, .column = ++column};
board[board_i++] = square;
} else if (fen_char > '0' && fen_char < '9') {
// if the char is a number bet. 1 and 8
// we increment the i by its real value (1..=8)
int offset = fen_char - '0';
if (offset < 9) {
for (int j = 0; j < offset; j++) {
Square square = {.piece = -1, .row = row, .column = ++column};
board[board_i++] = square;
}
} else {
printf("[ERROR]: On character %d of FEN, %c is out of range.", i,
fen_char);
return;
}
}
}
}
void fen_fromBoard(const Board board, FEN fen) {
// Reset fen
uint fen_i = 0;
while (fen[fen_i] != 0)
fen[fen_i++] = 0;
fen_i = 0;
uint board_i = 0;
while (board_i < 64) {
if (board_i != 0 && board_i % 8 == 0)
fen[fen_i++] = '/';
Square square = board[board_i++];
if (square.piece == -1) {
uint num = 1;
while (board_i % 8 != 0 && (square = board[board_i]).piece == -1) {
board_i++;
num++;
}
fen[fen_i++] = num + '0';
continue;
}
char fen_char;
switch (piece_getColorlessPiece(square.piece)) {
case Pawn:
fen_char = 'p';
break;
case Knight:
fen_char = 'n';
break;
case Bishop:
fen_char = 'b';
break;
case Rook:
fen_char = 'r';
break;
case Queen:
fen_char = 'q';
break;
case King:
fen_char = 'k';
break;
default:
printf("[ERROR]");
return;
}
if (piece_getColor(square.piece) == White)
fen_char = chrToUpper(fen_char);
fen[fen_i++] = fen_char;
}
}

9
src/fen.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef __FEN_HEADER
#include "board.h"
#include "common.h"
typedef char FEN[75];
void fen_fromBoard(const Board board, FEN fen);
void fen_toBoard(const FEN fen, Board board);
#endif // !__FEN_HEADER

10
src/main.c Normal file
View File

@@ -0,0 +1,10 @@
#include "fen.h"
int main() {
Board board;
FEN fen = "rnbqkbnr/ppppp1pp/5p2/8/8/8/PPPPPPPP/RNBQKBNR";
fen_toBoard(fen, board);
fen_fromBoard(board, fen);
printf("%s", fen);
return 0;
}