Skip to content

Commit

Permalink
chore: a bunch more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Jan 16, 2025
1 parent 7ded42b commit 270309d
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 219 deletions.
4 changes: 2 additions & 2 deletions games/src/games/ataxx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::{cmp, fmt, num::ParseIntError, str::FromStr, sync::LazyLock};
use crate::interface::{
game_details,
parse::{self, PiecePlacementParseError},
BitBoardType, Hash, MoveStore, MoveType, PositionType, RepresentableType,
SetType, TypeParseError,
Hash, MoveStore, MoveType, PositionType, RepresentableType, SetType,
TypeParseError,
};

use strum::IntoEnumIterator;
Expand Down
2 changes: 1 addition & 1 deletion games/src/games/chess/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ops, sync::LazyLock};

use crate::interface::{BitBoardType, RepresentableType, SetType, SquareType};
use crate::interface::{RepresentableType, SetType};

use strum::IntoEnumIterator;

Expand Down
17 changes: 11 additions & 6 deletions games/src/games/chess/movegen.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::str::FromStr;

use super::{
castling, moves, BitBoard, ColoredPiece, Direction, Move, MoveFlag, Piece,
Position, Rank, Square,
};
use crate::interface::{
BitBoardType, Color, ColoredPieceType, MoveStore, PositionType, SetType,
SquareType,
Color, ColoredPieceType, MoveStore, PositionType, SetType,
};

pub struct MoveGenerationInfo<'a> {
Expand Down Expand Up @@ -59,8 +60,8 @@ impl MoveGenerationInfo<'_> {
movelist: &mut ML,
) {
let last_rank = match self.position.side_to_move() {
Color::<Position>::White => BitBoard::rank(Rank::Eighth),
Color::<Position>::Black => BitBoard::rank(Rank::First),
Color::<Position>::White => BitBoard::rank(Rank::last()),
Color::<Position>::Black => BitBoard::rank(Rank::first()),
};

let targets = targets & self.checkmask & self.territory;
Expand Down Expand Up @@ -224,8 +225,12 @@ impl MoveGenerationInfo<'_> {
let uw = up + Direction::West;

let third_rank = match self.position.side_to_move() {
Color::<Position>::White => BitBoard::rank(Rank::Third),
Color::<Position>::Black => BitBoard::rank(Rank::Sixth),
Color::<Position>::White => {
BitBoard::rank(Rank::from_str("3").unwrap())
}
Color::<Position>::Black => {
BitBoard::rank(Rank::from_str("6").unwrap())
}
};

let pawns = self.position.piece_bb(Piece::Pawn) & self.friends;
Expand Down
22 changes: 11 additions & 11 deletions games/src/games/chess/moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
use std::sync::LazyLock;

use super::{BitBoard, Color, Direction, Square};
use crate::interface::{BitBoardType, RepresentableType, SetType, SquareType};
use crate::interface::{RepresentableType, SetType};

use strum::IntoEnumIterator;

pub fn pawn_attacks(square: Square, color: Color) -> BitBoard {
PAWN_ATTACKS_TABLE[color as usize][square as usize]
PAWN_ATTACKS_TABLE[color as usize][square.into()]
}

pub fn knight(square: Square) -> BitBoard {
KNIGHT_MOVES_TABLE[square as usize]
KNIGHT_MOVES_TABLE[square.into()]
}

pub fn bishop(square: Square, blockers: BitBoard) -> BitBoard {
Expand All @@ -45,18 +45,18 @@ pub fn queen(square: Square, blockers: BitBoard) -> BitBoard {
}

pub fn king(square: Square) -> BitBoard {
KING_MOVES_TABLE[square as usize]
KING_MOVES_TABLE[square.into()]
}

pub(crate) fn hyperbola(
square: Square,
blockers: BitBoard,
mask: BitBoard,
) -> BitBoard {
let mask = mask.0;
let square = BitBoard::from(square).0;
let mask = mask.into();
let square = BitBoard::from(square).into();
let rev_sq = square.reverse_bits();
let blockers = blockers.0;
let blockers = blockers.into();

let mut ray = blockers & mask;
let mut rev = ray.reverse_bits();
Expand All @@ -65,7 +65,7 @@ pub(crate) fn hyperbola(
ray ^= rev.reverse_bits();
ray &= mask;

BitBoard(ray)
BitBoard::from(ray)
}

static KING_MOVES_TABLE: LazyLock<[BitBoard; Square::N]> =
Expand All @@ -75,7 +75,7 @@ static KING_MOVES_TABLE: LazyLock<[BitBoard; Square::N]> =
let square_bb = BitBoard::from(square);
let line = square_bb | square_bb.east() | square_bb.west();
let cell = line | line.north() | line.south();
king_moves[square as usize] = cell ^ square_bb;
king_moves[square.into()] = cell ^ square_bb;
}

king_moves
Expand All @@ -99,7 +99,7 @@ static KNIGHT_MOVES_TABLE: LazyLock<[BitBoard; Square::N]> =
let north = north.east() | north.west();
let south = south.east() | south.west();

knight_moves[square as usize] = north | south | east | west;
knight_moves[square.into()] = north | south | east | west;
}

knight_moves
Expand All @@ -113,7 +113,7 @@ static PAWN_ATTACKS_TABLE: LazyLock<[[BitBoard; Square::N]; Color::N]> =
for square in Square::iter() {
let square_bb_up =
BitBoard::from(square).shift(Direction::up(color));
pawn_attacks[color as usize][square as usize] =
pawn_attacks[color as usize][square.into()] =
square_bb_up.east() | square_bb_up.west();
}
}
Expand Down
30 changes: 16 additions & 14 deletions games/src/games/chess/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl FromStr for Position {
castling: Default::default(),
};

interface::parse::piece_placement(&mut position, pos)?;
// TODO: interface::parse::piece_placement(&mut position, pos)?;

let kings = position.piece_bb(Piece::King);
let white_king = (kings & position.color_bb(Color::White)).next();
Expand Down Expand Up @@ -291,19 +291,21 @@ impl fmt::Display for Position {
let board = self;
let mut string_rep = String::from(" ");

for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}

// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
/*
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}
// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
*/

// Append the file markers.
string_rep += "a b c d e f g\n";
Expand Down
6 changes: 3 additions & 3 deletions games/src/games/chess/zobrist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// limitations under the License.

use super::{castling, ColoredPiece, File, Square};
use crate::interface::{Hash, RepresentableType, SquareType};
use crate::interface::{Hash, RepresentableType};

#[rustfmt::skip]
const PIECE_SQUARE_KEYS: [[u64; Square::N]; ColoredPiece::N] = [
Expand Down Expand Up @@ -43,12 +43,12 @@ const EN_PASSANT_KEYS: [u64; File::N] = [

#[inline(always)]
pub fn piece_square_key(piece: ColoredPiece, square: Square) -> Hash {
Hash::new(PIECE_SQUARE_KEYS[piece as usize][square as usize])
Hash::new(PIECE_SQUARE_KEYS[piece as usize][square.into()])
}

#[inline(always)]
pub fn en_passant_key(ep_square: Square) -> Hash {
Hash::new(EN_PASSANT_KEYS[ep_square.file() as usize])
Hash::new(EN_PASSANT_KEYS[ep_square.file().into()])
}

#[inline(always)]
Expand Down
40 changes: 21 additions & 19 deletions games/src/games/isolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::{fmt, num::ParseIntError, str::FromStr};

use crate::interface::{
parse::{self, PiecePlacementParseError},
BitBoardType, ColoredPieceType, Hash, MoveStore, MoveType, PositionType,
RepresentableType, SetType, SquareType, TypeParseError,
ColoredPieceType, Hash, MoveStore, MoveType, PositionType,
RepresentableType, SetType, TypeParseError,
};

use strum::IntoEnumIterator;
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Position {
tiles: BitBoard,
stm: Color,
) -> Hash {
let a = pawns[0] as u64 * Square::N as u64 + pawns[1] as u64;
let a = pawns[0].into() * Square::N as u64 + pawns[1].into();
let b = tiles.into();

// Currently, an 2^-63-almost delta universal hash function, based on
Expand Down Expand Up @@ -279,7 +279,7 @@ impl FromStr for Position {
ply_count: 0,
};

parse::piece_placement(&mut position, pos)?;
// TODO: parse::piece_placement(&mut position, pos)?;

position.side_to_move = Color::from_str(stm)?;
position.ply_count = parse::ply_count(fmc, position.side_to_move)?;
Expand All @@ -301,19 +301,21 @@ impl fmt::Display for Position {
let board = self;
let mut string_rep = String::from(" ");

for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}

// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
/*
for rank in Rank::iter().rev() {
for file in File::iter() {
let square = Square::new(file, rank);
let square_str = match board.at(square) {
Some(piece) => format!("{} ", piece),
None => ". ".to_string(),
};
string_rep += &square_str;
}
// Append the rank marker.
string_rep += &format!(" {} \n ", rank);
}
*/

// Append the file markers.
string_rep += "a b c d e f g h\n";
Expand Down Expand Up @@ -372,8 +374,8 @@ impl Move {
#[rustfmt::skip]
pub fn new(pawn: Square, tile: Square) -> Move {
Move(
(pawn as u16) << Move::PAWN_OFFSET |
(tile as u16) << Move::TILE_OFFSET
(pawn.into()) << Move::PAWN_OFFSET |
(tile.into()) << Move::TILE_OFFSET
)
}

Expand Down
Loading

0 comments on commit 270309d

Please sign in to comment.