Started to make the piece move
This commit is contained in:
13
src/board.c
13
src/board.c
@@ -4,3 +4,16 @@ PieceColors piece_getColor(const Piece piece) { return Black & piece; }
|
|||||||
Piece piece_getColorlessPiece(const Piece piece) {
|
Piece piece_getColorlessPiece(const Piece piece) {
|
||||||
return piece_getColor(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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
#ifndef __BOARD_HEADER
|
#ifndef __BOARD_HEADER
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
typedef int Piece;
|
typedef int Piece;
|
||||||
typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
|
typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
|
||||||
typedef enum { White = 0, Black = 8 } PieceColors;
|
typedef enum { White = 0, Black = 8 } PieceColors;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Piece piece;
|
|
||||||
uint row;
|
uint row;
|
||||||
uint column;
|
uint column;
|
||||||
|
} Coordinates;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Piece piece;
|
||||||
|
Coordinates coords;
|
||||||
} Square;
|
} Square;
|
||||||
|
|
||||||
typedef Square Board[64];
|
typedef Square Board[64];
|
||||||
@@ -15,4 +20,6 @@ typedef Square Board[64];
|
|||||||
PieceColors piece_getColor(const Piece piece);
|
PieceColors piece_getColor(const Piece piece);
|
||||||
Piece piece_getColorlessPiece(const Piece piece);
|
Piece piece_getColorlessPiece(const Piece piece);
|
||||||
|
|
||||||
|
bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
|
||||||
|
|
||||||
#endif // !__BOARD_HEADER
|
#endif // !__BOARD_HEADER
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef __COMMON_HEADER
|
#ifndef __COMMON_HEADER
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
|
|
||||||
char chrToLower(char chr);
|
char chrToLower(char chr);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "fen.h"
|
#include "fen.h"
|
||||||
|
|
||||||
// Starter fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
|
// Starter fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
|
||||||
void fen_toBoard(const FEN fen, Board board) {
|
void fen_toBoard(const FEN fen, Board board) {
|
||||||
uint i = 0;
|
uint i = 0;
|
||||||
@@ -43,7 +44,8 @@ void fen_toBoard(const FEN fen, Board board) {
|
|||||||
}
|
}
|
||||||
PieceColors color = is_upper_case ? White : Black;
|
PieceColors color = is_upper_case ? White : Black;
|
||||||
Piece piece = p ^ color;
|
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;
|
board[board_i++] = square;
|
||||||
} else if (fen_char > '0' && fen_char < '9') {
|
} else if (fen_char > '0' && fen_char < '9') {
|
||||||
// if the char is a number bet. 1 and 8
|
// 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';
|
int offset = fen_char - '0';
|
||||||
if (offset < 9) {
|
if (offset < 9) {
|
||||||
for (int j = 0; j < offset; j++) {
|
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;
|
board[board_i++] = square;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2,8 +2,11 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Board board;
|
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);
|
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);
|
fen_fromBoard(board, fen);
|
||||||
printf("%s", fen);
|
printf("%s", fen);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user