Skip to content

Commit

Permalink
Merge pull request #1 from /pull/28
Browse files Browse the repository at this point in the history
Apply `cargo fmt`
  • Loading branch information
na2hiro authored Oct 13, 2023
2 parents 92a53fc + 2ed61bc commit 1a96943
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
31 changes: 12 additions & 19 deletions src/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod tests {
assert_eq!(
bb | to_bb(vec![SQ_6G, SQ_7F]),
bb.sliding_positives(&[
to_bb(vec![SQ_6G, SQ_6H,SQ_6I]),
to_bb(vec![SQ_6G, SQ_6H, SQ_6I]),
to_bb(vec![SQ_7F, SQ_8F, SQ_9F]),
])
);
Expand Down Expand Up @@ -206,32 +206,25 @@ mod tests {

#[test]
fn vacant_files() {
assert_eq!(
!Bitboard::empty(),
Bitboard::empty().vacant_files(),
);
let all_files = to_bb(vec![SQ_1A, SQ_2B, SQ_3C, SQ_4D, SQ_5E, SQ_6F, SQ_7G, SQ_8H, SQ_9I]).vacant_files();
assert_eq!(
Bitboard::empty(),
all_files
);
assert_eq!(!Bitboard::empty(), Bitboard::empty().vacant_files());
let all_files = to_bb(vec![
SQ_1A, SQ_2B, SQ_3C, SQ_4D, SQ_5E, SQ_6F, SQ_7G, SQ_8H, SQ_9I,
])
.vacant_files();
assert_eq!(Bitboard::empty(), all_files);

let odd_files = to_bb(vec![SQ_1A, SQ_3A, SQ_5A, SQ_7A, SQ_9A]).vacant_files();
let odd_files2 = to_bb(vec![SQ_1I, SQ_3I, SQ_5I, SQ_7I, SQ_9I]).vacant_files();
assert_eq!(odd_files, odd_files2);

let even_files = to_bb(vec![SQ_2A, SQ_4A, SQ_6A, SQ_8A]).vacant_files();
assert_eq!(
Bitboard::empty(),
odd_files & even_files,
);
assert_eq!(
!Bitboard::empty(),
odd_files | even_files,
);
assert_eq!(Bitboard::empty(), odd_files & even_files);
assert_eq!(!Bitboard::empty(), odd_files | even_files);
}

fn to_bb(squares: Vec<Square>) -> Bitboard {
squares.iter().fold(Bitboard::empty(), |acc, e| (acc | Bitboard::single(*e)))
squares
.iter()
.fold(Bitboard::empty(), |acc, e| (acc | Bitboard::single(*e)))
}
}
23 changes: 9 additions & 14 deletions src/movegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ impl Position {
for ch in self.checkers() {
let pk = self.piece_at(ch).unwrap().piece_kind();
// 龍が斜め位置から王手している場合のみ、他の駒の裏に逃がれることができる可能性がある
if pk == PieceKind::ProRook
&& ch.file() != king.file()
&& ch.rank() != king.rank()
{
if pk == PieceKind::ProRook && ch.file() != king.file() && ch.rank() != king.rank() {
checkers_attacks |= ATTACK_TABLE.hi.attack(ch, &self.occupied_bitboard());
} else {
checkers_attacks |= ATTACK_TABLE.pseudo_attack(pk, ch, c.flip());
}
checkers_count += 1;
}
for to in ATTACK_TABLE.ou.attack(king, c) & !self.player_bitboard(c) & !checkers_attacks
{
for to in ATTACK_TABLE.ou.attack(king, c) & !self.player_bitboard(c) & !checkers_attacks {
av.push(Move::Normal {
from: king,
to,
Expand Down Expand Up @@ -332,8 +328,8 @@ impl Position {
// 玉が相手の攻撃範囲内に動いてしまう指し手は除外
if self.piece_at(from) == Some(king)
&& !self
.attackers_to(c.flip(), m.to(), &self.occupied_bitboard())
.is_empty()
.attackers_to(c.flip(), m.to(), &self.occupied_bitboard())
.is_empty()
{
return false;
}
Expand Down Expand Up @@ -409,7 +405,6 @@ impl Position {

#[cfg(test)]
mod tests {
use shogi_core::consts::square::SQ_2I;
use super::*;
use shogi_core::PartialPosition;
use shogi_usi_parser::FromUsi;
Expand Down Expand Up @@ -438,7 +433,7 @@ mod tests {
PartialPosition::from_usi(
"sfen lnsgkg1nl/1r5s1/pppppp1pp/6p2/9/2P6/PP1PPPPPP/7R1/LNSGKGSNL b Bb 1",
)
.expect("failed to parse"),
.expect("failed to parse"),
);
assert_eq!(
43,
Expand Down Expand Up @@ -493,7 +488,7 @@ mod tests {
);
let moves = pos.legal_moves();
assert_eq!(1, moves.len());
assert_eq!(SQ_2I, moves[0].to());
assert_eq!(Square::SQ_2I, moves[0].to());
}

#[test]
Expand All @@ -515,7 +510,7 @@ mod tests {
PartialPosition::from_usi(
"sfen lnsgkgsnl/1r5s1/pppppppp1/9/8L/9/PPPPPPPP1/1B5S1/LNSGKGSN1 w Pp 1",
)
.expect("failed to parse"),
.expect("failed to parse"),
);
let drop_moves = pos
.legal_moves()
Expand Down Expand Up @@ -548,7 +543,7 @@ mod tests {
PartialPosition::from_usi(
"sfen lnsgkgsn1/1r5s1/pppppppp1/9/8l/9/PPPPPPPP1/1B5S1/LNSGKGSN1 b Ppl 1",
)
.expect("failed to parse"),
.expect("failed to parse"),
);
let drop_moves = pos
.legal_moves()
Expand Down Expand Up @@ -672,7 +667,7 @@ mod tests {
PartialPosition::from_usi(
"sfen 6B2/7np/8k/7P1/7G1/9/9/9/9 b P2rb3g4s3n4l15p 1",
)
.expect("failed to parse"),
.expect("failed to parse"),
),
Square::SQ_1D,
true,
Expand Down

0 comments on commit 1a96943

Please sign in to comment.