Started working on generating the moves
This commit is contained in:
67
src/board.c
67
src/board.c
@@ -5,14 +5,75 @@ Piece piece_getColorlessPiece(const Piece piece) {
|
|||||||
return piece_getColor(piece) ^ piece;
|
return piece_getColor(piece) ^ piece;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint board_getIndex(const Coordinates coords) {
|
int board_getIndex(const Coordinates coords) {
|
||||||
|
if (coords.row < 1 || coords.row > 8) {
|
||||||
|
printf("[ERROR]: Row out of bounds.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (coords.column < 1 || coords.column > 8) {
|
||||||
|
printf("[ERROR]: Column out of bounds.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
|
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void piece_getMoves(const Board board, const Piece piece,
|
||||||
|
const Coordinates piece_coords) {
|
||||||
|
Pieces colorless_piece = piece_getColorlessPiece(piece);
|
||||||
|
PieceColors piece_color = piece_getColor(piece);
|
||||||
|
int board_index = board_getIndex(piece_coords);
|
||||||
|
if (board_index == -1)
|
||||||
|
return;
|
||||||
|
Coordinates moves[27];
|
||||||
|
uint moves_i = 0;
|
||||||
|
switch (colorless_piece) {
|
||||||
|
case Pawn: {
|
||||||
|
Coordinates new_coordinates = {.column = piece_coords.column,
|
||||||
|
.row = piece_color == Black
|
||||||
|
? piece_coords.row - 1
|
||||||
|
: piece_coords.row + 1};
|
||||||
|
moves[moves_i++] = new_coordinates;
|
||||||
|
|
||||||
|
bool is_starting_pos =
|
||||||
|
piece_color == Black ? piece_coords.row == 7 : piece_coords.row == 2;
|
||||||
|
if (is_starting_pos) {
|
||||||
|
Coordinates leap_coords = {.column = piece_coords.column,
|
||||||
|
.row = piece_color == Black
|
||||||
|
? piece_coords.row - 2
|
||||||
|
: piece_coords.row + 2};
|
||||||
|
moves[moves_i++] = leap_coords;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case Knight: {
|
||||||
|
struct temp {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
struct temp temps[8] = {{-1, 2}, {1, 2}, {2, 1}, {2, -1},
|
||||||
|
{1, -2}, {-1, -2}, {-2, -1}, {-2, 1}};
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y,
|
||||||
|
.column =
|
||||||
|
piece_coords.column + temps[i].x};
|
||||||
|
int index = board_getIndex(new_coordinates);
|
||||||
|
if (index != -1)
|
||||||
|
moves[moves_i++] = new_coordinates;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case Bishop: {
|
||||||
|
uint max = 13;
|
||||||
|
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool board_movePiece(Board board, const Coordinates from,
|
bool board_movePiece(Board board, const Coordinates from,
|
||||||
const Coordinates to) {
|
const Coordinates to) {
|
||||||
uint from_index = board_getIndex(from);
|
int from_index = board_getIndex(from);
|
||||||
uint to_index = board_getIndex(to);
|
int to_index = board_getIndex(to);
|
||||||
|
if (from_index == -1 || to_index == -1)
|
||||||
|
return false;
|
||||||
|
|
||||||
board[to_index].piece = board[from_index].piece;
|
board[to_index].piece = board[from_index].piece;
|
||||||
board[from_index].piece = -1;
|
board[from_index].piece = -1;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef __BOARD_HEADER
|
#ifndef __BOARD_HEADER
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
typedef int Piece;
|
typedef uint 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;
|
||||||
|
|
||||||
@@ -21,5 +21,6 @@ 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);
|
bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
|
||||||
|
int board_getIndex(const Coordinates coords);
|
||||||
|
|
||||||
#endif // !__BOARD_HEADER
|
#endif // !__BOARD_HEADER
|
||||||
|
|||||||
Reference in New Issue
Block a user