Refactor using Clippy nursery and unwrap_used
This commit is contained in:
@@ -13,6 +13,6 @@ rand = { version = "0.8.5", features = ["small_rng"] }
|
|||||||
|
|
||||||
[lints.clippy]
|
[lints.clippy]
|
||||||
enum_glob_use = "deny"
|
enum_glob_use = "deny"
|
||||||
#pedantic = "deny"
|
# pedantic = "deny"
|
||||||
#nursery = "deny"
|
nursery = "deny"
|
||||||
#unwrap_used = "deny"
|
unwrap_used = "deny"
|
||||||
@@ -36,7 +36,7 @@ static mut KING_ATTACKS: [Bitboard; 64] = [0; 64];
|
|||||||
static mut BISHOP_ATTACKS: [[Bitboard; 512]; 64] = [[0; 512]; 64];
|
static mut BISHOP_ATTACKS: [[Bitboard; 512]; 64] = [[0; 512]; 64];
|
||||||
static mut ROOK_ATTACKS: [[Bitboard; 4096]; 64] = [[0; 4096]; 64];
|
static mut ROOK_ATTACKS: [[Bitboard; 4096]; 64] = [[0; 4096]; 64];
|
||||||
|
|
||||||
fn pawn_attacks(bitboard: Bitboard, color: Color) -> Bitboard {
|
const fn pawn_attacks(bitboard: Bitboard, color: Color) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
|
|
||||||
match color {
|
match color {
|
||||||
@@ -53,7 +53,7 @@ fn pawn_attacks(bitboard: Bitboard, color: Color) -> Bitboard {
|
|||||||
attacks
|
attacks
|
||||||
}
|
}
|
||||||
|
|
||||||
fn knight_attacks(bitboard: Bitboard) -> Bitboard {
|
const fn knight_attacks(bitboard: Bitboard) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
|
|
||||||
attacks |= (bitboard & NOT_AB_FILE) << (6);
|
attacks |= (bitboard & NOT_AB_FILE) << (6);
|
||||||
@@ -68,7 +68,7 @@ fn knight_attacks(bitboard: Bitboard) -> Bitboard {
|
|||||||
attacks
|
attacks
|
||||||
}
|
}
|
||||||
|
|
||||||
fn king_attacks(bitboard: Bitboard) -> Bitboard {
|
const fn king_attacks(bitboard: Bitboard) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
|
|
||||||
attacks |= (bitboard & NOT_H_FILE) << (1);
|
attacks |= (bitboard & NOT_H_FILE) << (1);
|
||||||
@@ -85,12 +85,16 @@ fn king_attacks(bitboard: Bitboard) -> Bitboard {
|
|||||||
|
|
||||||
pub fn mask_bishop_attacks(bitboard: Bitboard) -> Bitboard {
|
pub fn mask_bishop_attacks(bitboard: Bitboard) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
let target_rank = (bitboard == 0)
|
let target_rank = if bitboard == 0 {
|
||||||
.then(|| 0)
|
0
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
|
} else {
|
||||||
let target_file = (bitboard == 0)
|
bitboard.trailing_zeros() / 8
|
||||||
.then(|| 0)
|
};
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
|
let target_file = if bitboard == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
bitboard.trailing_zeros() % 8
|
||||||
|
};
|
||||||
|
|
||||||
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
||||||
for (rank, file) in (rank..=6).zip(file..=6) {
|
for (rank, file) in (rank..=6).zip(file..=6) {
|
||||||
@@ -117,12 +121,16 @@ pub fn mask_bishop_attacks(bitboard: Bitboard) -> Bitboard {
|
|||||||
|
|
||||||
pub fn mask_rook_attacks(bitboard: Bitboard) -> Bitboard {
|
pub fn mask_rook_attacks(bitboard: Bitboard) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
let target_rank = (bitboard == 0)
|
let target_rank = if bitboard == 0 {
|
||||||
.then(|| 0)
|
0
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
|
} else {
|
||||||
let target_file = (bitboard == 0)
|
bitboard.trailing_zeros() / 8
|
||||||
.then(|| 0)
|
};
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
|
let target_file = if bitboard == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
bitboard.trailing_zeros() % 8
|
||||||
|
};
|
||||||
|
|
||||||
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
||||||
for rank in rank..=6 {
|
for rank in rank..=6 {
|
||||||
@@ -145,12 +153,16 @@ pub fn mask_rook_attacks(bitboard: Bitboard) -> Bitboard {
|
|||||||
|
|
||||||
pub fn bishop_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboard {
|
pub fn bishop_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
let target_rank = (bitboard == 0)
|
let target_rank = if bitboard == 0 {
|
||||||
.then(|| 0)
|
0
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
|
} else {
|
||||||
let target_file = (bitboard == 0)
|
bitboard.trailing_zeros() / 8
|
||||||
.then(|| 0)
|
};
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
|
let target_file = if bitboard == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
bitboard.trailing_zeros() % 8
|
||||||
|
};
|
||||||
|
|
||||||
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
||||||
for (rank, file) in (rank..=7).zip(file..=7) {
|
for (rank, file) in (rank..=7).zip(file..=7) {
|
||||||
@@ -193,12 +205,16 @@ pub fn bishop_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitbo
|
|||||||
|
|
||||||
pub fn rook_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboard {
|
pub fn rook_attacks_on_the_fly(bitboard: Bitboard, blocker: Bitboard) -> Bitboard {
|
||||||
let mut attacks = 0_u64;
|
let mut attacks = 0_u64;
|
||||||
let target_rank = (bitboard == 0)
|
let target_rank = if bitboard == 0 {
|
||||||
.then(|| 0)
|
0
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() / 8);
|
} else {
|
||||||
let target_file = (bitboard == 0)
|
bitboard.trailing_zeros() / 8
|
||||||
.then(|| 0)
|
};
|
||||||
.unwrap_or_else(|| bitboard.trailing_zeros() % 8);
|
let target_file = if bitboard == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
bitboard.trailing_zeros() % 8
|
||||||
|
};
|
||||||
|
|
||||||
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
let (mut rank, mut file) = (target_rank + 1, target_file + 1);
|
||||||
for rank in rank..=7 {
|
for rank in rank..=7 {
|
||||||
|
|||||||
20
src/board.rs
20
src/board.rs
@@ -1,15 +1,15 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use u64 as Bitboard;
|
use u64 as Bitboard;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
white_pieces: [Piece; 6],
|
white_pieces: [Piece; 6],
|
||||||
black_pieces: [Piece; 6],
|
black_pieces: [Piece; 6],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn new() -> Board {
|
pub const fn new() -> Self {
|
||||||
Board {
|
Self {
|
||||||
white_pieces: [
|
white_pieces: [
|
||||||
Piece::new(0xff00, Kind::Pawn, Color::White),
|
Piece::new(0xff00, Kind::Pawn, Color::White),
|
||||||
Piece::new(0x81, Kind::Rook, Color::White),
|
Piece::new(0x81, Kind::Rook, Color::White),
|
||||||
@@ -29,8 +29,8 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn empty_board() -> Board {
|
pub const fn empty_board() -> Self {
|
||||||
Board {
|
Self {
|
||||||
white_pieces: [
|
white_pieces: [
|
||||||
Piece::new(0x0, Kind::Pawn, Color::White),
|
Piece::new(0x0, Kind::Pawn, Color::White),
|
||||||
Piece::new(0x0, Kind::Rook, Color::White),
|
Piece::new(0x0, Kind::Rook, Color::White),
|
||||||
@@ -85,7 +85,7 @@ impl fmt::Display for Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Piece {
|
pub struct Piece {
|
||||||
pub bitboard: Bitboard,
|
pub bitboard: Bitboard,
|
||||||
pub kind: Kind,
|
pub kind: Kind,
|
||||||
@@ -93,8 +93,8 @@ pub struct Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Piece {
|
impl Piece {
|
||||||
pub fn new(bitboard: Bitboard, kind: Kind, color: Color) -> Piece {
|
pub const fn new(bitboard: Bitboard, kind: Kind, color: Color) -> Self {
|
||||||
Piece {
|
Self {
|
||||||
bitboard,
|
bitboard,
|
||||||
kind,
|
kind,
|
||||||
color,
|
color,
|
||||||
@@ -116,7 +116,7 @@ impl fmt::Display for Piece {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Kind {
|
pub enum Kind {
|
||||||
Pawn,
|
Pawn,
|
||||||
Rook,
|
Rook,
|
||||||
@@ -126,7 +126,7 @@ pub enum Kind {
|
|||||||
King,
|
King,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Color {
|
pub enum Color {
|
||||||
White,
|
White,
|
||||||
Black,
|
Black,
|
||||||
|
|||||||
@@ -123,9 +123,9 @@ fn en_passant_target_square(square: &str) -> Result<u8, FenError> {
|
|||||||
|
|
||||||
match sqr.next() {
|
match sqr.next() {
|
||||||
Some(file) if files.contains_key(&file) => match sqr.next() {
|
Some(file) if files.contains_key(&file) => match sqr.next() {
|
||||||
Some(rank) if ranks.contains_key(&rank) => {
|
Some(rank) if ranks.contains_key(&rank) => Ok(ranks.get(&rank).expect("Invalid rank")
|
||||||
Ok(ranks.get(&rank).unwrap() * 8 + files.get(&file).unwrap())
|
* 8
|
||||||
}
|
+ files.get(&file).expect("Invalid file")),
|
||||||
Some(_) | None => Err(FenError::from(
|
Some(_) | None => Err(FenError::from(
|
||||||
"Not a valid rank (3 or 6) for an en passant target square",
|
"Not a valid rank (3 or 6) for an en passant target square",
|
||||||
)),
|
)),
|
||||||
|
|||||||
22
src/game.rs
22
src/game.rs
@@ -4,25 +4,25 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use String as FenError;
|
use String as FenError;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
pub board: Board,
|
pub board: Board,
|
||||||
pub state: State,
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn new() -> Game {
|
pub const fn new() -> Self {
|
||||||
Game {
|
Self {
|
||||||
board: Board::new(),
|
board: Board::new(),
|
||||||
state: State::new(),
|
state: State::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_fen(fen: &str) -> Result<Game, FenError> {
|
pub fn new_from_fen(fen: &str) -> Result<Self, FenError> {
|
||||||
from_fen(fen)
|
from_fen(fen)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&self) {
|
pub const fn run(&self) {
|
||||||
Board::new();
|
Board::new();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ impl Default for Game {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
side_to_move: Color,
|
side_to_move: Color,
|
||||||
castling_ability: u8,
|
castling_ability: u8,
|
||||||
@@ -43,8 +43,8 @@ pub struct State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub fn new() -> State {
|
pub const fn new() -> Self {
|
||||||
State {
|
Self {
|
||||||
side_to_move: Color::White,
|
side_to_move: Color::White,
|
||||||
castling_ability: 0b1111,
|
castling_ability: 0b1111,
|
||||||
en_passant_target_square: 0,
|
en_passant_target_square: 0,
|
||||||
@@ -53,14 +53,14 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_state(
|
pub const fn load_state(
|
||||||
side_to_move: Color,
|
side_to_move: Color,
|
||||||
castling_ability: u8,
|
castling_ability: u8,
|
||||||
en_passant_target_square: u8,
|
en_passant_target_square: u8,
|
||||||
halfmove_clock: u8,
|
halfmove_clock: u8,
|
||||||
fullmove_counter: u8,
|
fullmove_counter: u8,
|
||||||
) -> State {
|
) -> Self {
|
||||||
State {
|
Self {
|
||||||
side_to_move,
|
side_to_move,
|
||||||
castling_ability,
|
castling_ability,
|
||||||
en_passant_target_square,
|
en_passant_target_square,
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ pub mod board;
|
|||||||
pub mod fen;
|
pub mod fen;
|
||||||
pub mod game;
|
pub mod game;
|
||||||
pub mod magic;
|
pub mod magic;
|
||||||
pub mod r#move;
|
|
||||||
|
|
||||||
use game::Game;
|
use game::Game;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user