Compare commits
25 Commits
v117
...
Engine/che
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e64a7ca16 | ||
|
|
6e0efc76f3 | ||
|
|
1a9d7ad460 | ||
|
|
9209f1c4e0 | ||
|
|
3ebfc61ac7 | ||
|
|
a8970053f9 | ||
|
|
fa0a93172d | ||
|
|
bb74a486d8 | ||
|
|
d1932762f9 | ||
|
|
9181b6c4ca | ||
|
|
b703d8b9a1 | ||
|
|
acaa7d078a | ||
|
|
30448b5f3d | ||
|
|
ff38c5c475 | ||
|
|
42daeb9971 | ||
|
|
c8b6bc35d5 | ||
|
|
5f2a4e1721 | ||
|
|
5c42b6e52a | ||
|
|
f7364e7939 | ||
|
|
b4a1f0820f | ||
|
|
6d9d5c49ae | ||
|
|
1ca44b472b | ||
|
|
79aabeac8c | ||
|
|
5eb85dab89 | ||
|
|
291b7e71a2 |
@@ -64,7 +64,7 @@ impl Board {
|
|||||||
|
|
||||||
for (i, c) in coming_up.chars().enumerate() {
|
for (i, c) in coming_up.chars().enumerate() {
|
||||||
if pieces.contains(&c) {
|
if pieces.contains(&c) {
|
||||||
// board.place_piece(row*8 + col, c);
|
board.place_piece(row*8 + col, c);
|
||||||
col += 1;
|
col += 1;
|
||||||
}
|
}
|
||||||
else if ('1'..='8').contains(&c) {
|
else if ('1'..='8').contains(&c) {
|
||||||
@@ -179,4 +179,21 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn place_piece(&mut self, sq: i32, piece: char) {
|
||||||
|
match piece {
|
||||||
|
'p' => {self.bitboards[6] |= 1 << sq}
|
||||||
|
'n' => {self.bitboards[7] |= 1 << sq}
|
||||||
|
'b' => {self.bitboards[8] |= 1 << sq}
|
||||||
|
'r' => {self.bitboards[9] |= 1 << sq}
|
||||||
|
'q' => {self.bitboards[10] |= 1 << sq}
|
||||||
|
'k' => {self.bitboards[11] |= 1 << sq}
|
||||||
|
'P' => {self.bitboards[0] |= 1 << sq}
|
||||||
|
'N' => {self.bitboards[1] |= 1 << sq}
|
||||||
|
'B' => {self.bitboards[2] |= 1 << sq}
|
||||||
|
'R' => {self.bitboards[3] |= 1 << sq}
|
||||||
|
'Q' => {self.bitboards[4] |= 1 << sq}
|
||||||
|
'K' => {self.bitboards[5] |= 1 << sq}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,56 @@
|
|||||||
use super::board::Board;
|
use super::board::Board;
|
||||||
use super::attackmaps::RAY_TABLE;
|
use super::attackmaps::RAY_TABLE;
|
||||||
|
use super::checkinfo::CheckInfo;
|
||||||
|
use super::attacks::get_raycast_from_square_in_direction;
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
|
||||||
|
pub fn check_test(&self) -> CheckInfo {
|
||||||
|
let mut check_info: CheckInfo = CheckInfo::new();
|
||||||
|
let offset: usize = 6 * self.side_to_move as usize;
|
||||||
|
let king: u64 = self.bitboards[5 + offset];
|
||||||
|
let king_sq = king.trailing_zeros() as usize;
|
||||||
|
let occupancy = self.occupancy[2];
|
||||||
|
|
||||||
|
// queen-rook checks (+)
|
||||||
|
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[9 - offset];
|
||||||
|
|
||||||
|
for dir in [0, 2, 4, 6] {
|
||||||
|
let threat_mask: u64 = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
|
||||||
|
if threat_mask & attacker_mask != 0 {
|
||||||
|
check_info.add_checker(threat_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// queen-bishop checks (x)
|
||||||
|
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[8 - offset];
|
||||||
|
|
||||||
|
for dir in [1, 3, 5, 7] {
|
||||||
|
let threat_mask = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
|
||||||
|
if threat_mask & attacker_mask != 0 {
|
||||||
|
check_info.add_checker(threat_mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// knight checks (L)
|
||||||
|
let attacker_mask = self.bitboards[7 - offset];
|
||||||
|
let threat_mask = self.get_pseudo_knight_moves(king_sq as u32);
|
||||||
|
let checker = threat_mask & attacker_mask;
|
||||||
|
if checker != 0 {
|
||||||
|
check_info.add_checker(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pawn checks (v)
|
||||||
|
let attacker_mask = self.bitboards[6 - offset];
|
||||||
|
let threat_mask = self.get_pseudo_pawn_captures(king_sq as u32);
|
||||||
|
let checker = threat_mask & attacker_mask;
|
||||||
|
if checker != 0 {
|
||||||
|
check_info.add_checker(checker);
|
||||||
|
}
|
||||||
|
|
||||||
|
return check_info;
|
||||||
|
}
|
||||||
|
|
||||||
pub(in super) fn calc_pinned_squares(&mut self) {
|
pub(in super) fn calc_pinned_squares(&mut self) {
|
||||||
self.pinned_squares = [4; 64];
|
self.pinned_squares = [4; 64];
|
||||||
self.pin_mask = 0u64;
|
self.pin_mask = 0u64;
|
||||||
@@ -46,3 +94,34 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// <----- TESTS ----->
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_test_test() {
|
||||||
|
|
||||||
|
let fens = [
|
||||||
|
"rnb1k1nr/pppppppp/4q3/8/1b6/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", // no check
|
||||||
|
"rnb1k1nr/pppppppp/4q3/8/1b1P4/8/PPP1PPPP/RNBQKBNR w KQkq d3 0 1", // single check
|
||||||
|
"rnb1k1nr/ppp1p2p/3pq1p1/8/1b1P1P2/8/PPP2PPP/RNBQKBNR w KQkq - 0 1" // double check
|
||||||
|
];
|
||||||
|
let expected_results = [
|
||||||
|
CheckInfo { check_count: 0, move_mask: 0xFFFF_FFFF_FFFF_FFFF },
|
||||||
|
CheckInfo { check_count: 1, move_mask: 0x0000_0000_0204_0800 },
|
||||||
|
CheckInfo { check_count: 2, move_mask: 0x0000_0000_0000_0000 }
|
||||||
|
];
|
||||||
|
|
||||||
|
for test_nr in 0..3 {
|
||||||
|
let fen = fens[test_nr];
|
||||||
|
let board = Board::build(fen);
|
||||||
|
let check_test_actual = board.check_test();
|
||||||
|
assert_eq!(check_test_actual.check_count, expected_results[test_nr].check_count);
|
||||||
|
assert_eq!(check_test_actual.move_mask, expected_results[test_nr].move_mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
32
engine/src/boardsquare.rs
Normal file
32
engine/src/boardsquare.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
pub struct BoardSquare {
|
||||||
|
pub x: usize,
|
||||||
|
pub y: usize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BoardSquare {
|
||||||
|
|
||||||
|
pub fn new() -> Self {
|
||||||
|
return Self{
|
||||||
|
x: 0,
|
||||||
|
y: 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_coord(x: usize, y: usize) -> Self {
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
if x > 7 {
|
||||||
|
println!("Warning: x coordinate of square is bigger than 7, it might not be on the board!");
|
||||||
|
}
|
||||||
|
if y > 7 {
|
||||||
|
println!("Warning: y coordinate of square is bigger than 7, it might not be on the board!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Self {
|
||||||
|
x: x,
|
||||||
|
y: y
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
71
engine/src/chessmove.rs
Normal file
71
engine/src/chessmove.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
use crate::piecetype;
|
||||||
|
|
||||||
|
use super::boardsquare::BoardSquare;
|
||||||
|
use super::piecetype::PieceType;
|
||||||
|
use super::movetype::MoveType;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct ChessMove {
|
||||||
|
pub move_type: MoveType,
|
||||||
|
pub piece_type: PieceType,
|
||||||
|
pub from_square: BoardSquare,
|
||||||
|
pub to_square: BoardSquare,
|
||||||
|
pub rook_from: BoardSquare,
|
||||||
|
pub rook_to: BoardSquare,
|
||||||
|
pub promotion_piece: Option<PieceType>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChessMove {
|
||||||
|
|
||||||
|
pub fn quiet(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
promotion_piece: Option<PieceType>
|
||||||
|
) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: MoveType::Quiet,
|
||||||
|
piece_type: piece_type,
|
||||||
|
from_square: from_square,
|
||||||
|
to_square: to_square,
|
||||||
|
rook_from: BoardSquare::new(),
|
||||||
|
rook_to: BoardSquare::new(),
|
||||||
|
promotion_piece: promotion_piece
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn capture(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
promotion_piece: Option<PieceType>
|
||||||
|
) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: MoveType::Capture,
|
||||||
|
piece_type: piece_type,
|
||||||
|
from_square: from_square,
|
||||||
|
to_square: to_square,
|
||||||
|
rook_from: BoardSquare::new(),
|
||||||
|
rook_to: BoardSquare::new(),
|
||||||
|
promotion_piece: promotion_piece
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn castle(
|
||||||
|
piece_type: PieceType,
|
||||||
|
from_square: BoardSquare,
|
||||||
|
to_square: BoardSquare,
|
||||||
|
rook_from: BoardSquare,
|
||||||
|
rook_to: BoardSquare
|
||||||
|
) -> Self {
|
||||||
|
return Self {
|
||||||
|
move_type: MoveType::Quiet,
|
||||||
|
piece_type: piece_type,
|
||||||
|
from_square: from_square,
|
||||||
|
to_square: to_square,
|
||||||
|
rook_from: rook_from,
|
||||||
|
rook_to: rook_to,
|
||||||
|
promotion_piece: None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6
engine/src/gameend.rs
Normal file
6
engine/src/gameend.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
pub enum GameEnd {
|
||||||
|
WhiteWon(String),
|
||||||
|
BlackWon(String),
|
||||||
|
Draw(String)
|
||||||
|
}
|
||||||
24
engine/src/lib.rs
Normal file
24
engine/src/lib.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
mod bitboard;
|
||||||
|
pub mod chessmove;
|
||||||
|
pub mod piecetype;
|
||||||
|
pub mod boardsquare;
|
||||||
|
pub mod movetype;
|
||||||
|
pub mod gameend;
|
||||||
|
|
||||||
|
use chessmove::ChessMove;
|
||||||
|
use gameend::GameEnd;
|
||||||
|
|
||||||
|
pub fn get_available_moves(fen: &str) -> Vec<ChessMove> {
|
||||||
|
println!("get_available_moves answered");
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_game_over(fen: &str) -> Option<GameEnd> {
|
||||||
|
println!("is_game_over answered");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_board_after_move(fen: &str, chess_move: &ChessMove) -> String {
|
||||||
|
println!("get_board_after_move answered");
|
||||||
|
return String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
||||||
|
}
|
||||||
7
engine/src/movetype.rs
Normal file
7
engine/src/movetype.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
pub enum MoveType {
|
||||||
|
Quiet,
|
||||||
|
Capture,
|
||||||
|
Castle,
|
||||||
|
EnPassant
|
||||||
|
}
|
||||||
15
engine/src/piecetype.rs
Normal file
15
engine/src/piecetype.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
pub enum PieceType {
|
||||||
|
WhitePawn,
|
||||||
|
WhiteKnight,
|
||||||
|
WhiteBishop,
|
||||||
|
WhiteRook,
|
||||||
|
WhiteQueen,
|
||||||
|
WhiteKing,
|
||||||
|
BlackPawn,
|
||||||
|
BlackKnight,
|
||||||
|
BlackBishop,
|
||||||
|
BlackRook,
|
||||||
|
BlackQueen,
|
||||||
|
BlackKing
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user