Skip to content

Commit

Permalink
Implement game termination
Browse files Browse the repository at this point in the history
Also, disable overflow check due to bug in rustc:
rust-lang/rust#78744
  • Loading branch information
elkozmon committed Mar 29, 2021
1 parent d8ec2f9 commit 0400d82
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 140 deletions.
3 changes: 3 additions & 0 deletions dot_chess/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["Lubos Kozmon <contact@elkozmon.com>"]
edition = "2018"

[profile.release]
overflow-checks = false

[dependencies]
ink_primitives = { version = "3.0.0-rc3", default-features = false }
ink_metadata = { version = "3.0.0-rc3", default-features = false, features = ["derive"], optional = true }
Expand Down
98 changes: 52 additions & 46 deletions dot_chess/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ pub use square::Square;
)]
pub struct Flags(u16);

impl core::convert::Into<u16> for Flags {
fn into(self) -> u16 {
self.0
}
}

impl Flags {
const WHITES_TURN_INDEX: usize = 12;

Expand Down Expand Up @@ -285,6 +291,51 @@ impl Board {
pieces
}

pub fn get_king_square(&self, side: Side) -> Square {
let pieces = match side {
Side::White => self.white,
Side::Black => self.black,
};

(pieces & self.kings).pop_square()
}

// TODO test
pub fn is_attacked(&self, square: Square, by_side: Side) -> bool {
let bitboard = BitBoard::square(square);
let attack_pieces = match by_side {
Side::White => self.white,
Side::Black => self.black,
};

let pawns = attack_pieces & self.pawns;
if (bitboard.black_pawn_any_attacks_mask() & pawns).not_empty() {
return true;
}

let knights = attack_pieces & self.knights;
if (BitBoard::knight_attacks_mask(square) & knights).not_empty() {
return true;
}

let kings = attack_pieces & self.kings;
if (BitBoard::king_attacks_mask(square) & kings).not_empty() {
return true;
}

let bishops_queens = attack_pieces & (self.bishops | self.queens);
if (self.bishop_attacks(square) & bishops_queens).not_empty() {
return true;
}

let rooks_queens = attack_pieces & (self.rooks | self.queens);
if (self.rook_attacks(square) & rooks_queens).not_empty() {
return true;
}

false
}

pub fn get_side_turn(&self) -> Side {
match self.get_flags().get_whites_turn() {
true => Side::White,
Expand Down Expand Up @@ -367,51 +418,6 @@ impl Board {
self.rook_attacks(square) | self.bishop_attacks(square)
}

fn get_king_square(&self, side: Side) -> Square {
let pieces = match side {
Side::White => self.white,
Side::Black => self.black,
};

(pieces & self.kings).pop_square()
}

// TODO test
fn is_attacked(&self, square: Square, by_side: Side) -> bool {
let bitboard = BitBoard::square(square);
let attack_pieces = match by_side {
Side::White => self.white,
Side::Black => self.black,
};

let pawns = attack_pieces & self.pawns;
if (bitboard.black_pawn_any_attacks_mask() & pawns).not_empty() {
return true;
}

let knights = attack_pieces & self.knights;
if (BitBoard::knight_attacks_mask(square) & knights).not_empty() {
return true;
}

let kings = attack_pieces & self.kings;
if (BitBoard::king_attacks_mask(square) & kings).not_empty() {
return true;
}

let bishops_queens = attack_pieces & (self.bishops | self.queens);
if (self.bishop_attacks(square) & bishops_queens).not_empty() {
return true;
}

let rooks_queens = attack_pieces & (self.rooks | self.queens);
if (self.rook_attacks(square) & rooks_queens).not_empty() {
return true;
}

false
}

// TODO test
fn try_make_pseudo_legal_move(&self, ply: Ply) -> Result<(Self, Vec<Event>), Error> {
// Assert sides turn
Expand Down Expand Up @@ -852,7 +858,7 @@ mod tests {
.into_iter()
.collect();

let board = Board::new(pieces, Flags::default());
let board = Board::new(pieces, Flags::default(), 0);
let square = Square::new(File::H, Rank::_8);

assert_eq!(
Expand Down
Loading

0 comments on commit 0400d82

Please sign in to comment.