Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc comments & tests. Some refactoring #28

Merged
merged 6 commits into from
Oct 13, 2023

Conversation

na2hiro
Copy link
Contributor

@na2hiro na2hiro commented Sep 14, 2023

I'm interested in using (and potentially extending) this library and read the codebase to understand how it works. Along the way,

  • I added comments for what was not clear to me as well as adding doc comments for structs and methods which look important. (1st commit)
  • I added test cases for some uncovered code. (1st commit)
  • I refactored and optimized (hopefully) some run time code (second commit).

Please be patient if I do stuff wrong: I'm not very fluent in Rust. And thanks for the great library!

let bb = to_bb(vec![SQ_6H, SQ_8F]);
assert_eq!(
bb | to_bb(vec![SQ_6G, SQ_7F]),
bb.sliding_positives(&[
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to make sure where the positives are

#[test]
fn evasion_moves() {
// TODO: add more cases
// Behind RY
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cover the line 57, described as

龍が斜め位置から王手している場合のみ、他の駒の裏に逃がれることができる可能性がある

src/position.rs Outdated
@@ -346,17 +348,24 @@ impl From<shogi_core::PartialPosition> for PartialPosition {

#[derive(Debug, Clone)]
struct State {
/// Zobrist hashes for (board ^ side, hand)
/// TODO: It seems it's always used by XOR-ing two keys. Can we only store a single XOR-ed key?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a question for you and probably should be revised before merging

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'm not using it specifically, I think that it would be easier to calculate the judgment when searching for positions with a "superiority relationship" if it is possible to independently obtain the Hash of the board only, not including the hand pieces, for example, when searching for Tsume-Shogi.

詰将棋を解くアルゴリズムにおける優越関係の効率的な利用について

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: grammar

Suggested change
/// TODO: It seems it's always used by XOR-ing two keys. Can we only store a single XOR-ed key?
/// TODO: It seems whenever we use this field its components (two keys) are always XOR-ed. Can we only store a single XOR-ed key?

@@ -43,50 +43,49 @@ impl Position {
/// Generate moves to evade check, optimized using AttackInfo.
fn generate_evasions(&self, av: &mut ArrayVec<Move, MAX_LEGAL_MOVES>) {
let c = self.side_to_move();
if let Some(king) = self.king_position(c) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwrapped king_position() as this method should be called during evading

let mut checkers_attacks = Bitboard::empty();
let mut checkers_count = 0;
for ch in self.checkers() {
if let Some(p) = self.piece_at(ch) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unwrapped piece_at(ch) as checkers() should only return positions where pieces exist

self.generate_for_ki(av, &target_move);
self.generate_for_um(av, &target_move);
self.generate_for_ry(av, &target_move);
if !target_drop.is_empty() {
Copy link
Contributor Author

@na2hiro na2hiro Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing generate_drop is somewhat costly/redundant in case target_drop is empty, which is fairly often like in case attacker is not 飛び駒.

}
let pk = piece.piece_kind();
let pk = if promote {
pk.promote().unwrap_or(pk)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a few refactor with unwrap_or to improve readability. Hoping if this is not harmful for performance.

return !(BETWEEN_TABLE[sq.array_index()][from.array_index()].contains(to)
|| BETWEEN_TABLE[sq.array_index()][to.array_index()].contains(from));
}
let c = self.inner.side.flip();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When only flip side is used for multiple times, I rewrote to flipping it once than doing it in every occurrence.

@@ -393,14 +381,14 @@ impl AttackInfo {
let ka = ATTACK_TABLE.ka.attack(sq, &occ);
let hi = ATTACK_TABLE.hi.attack(sq, &occ);
let ki = ATTACK_TABLE.ki.attack(sq, opp);
let ou = ATTACK_TABLE.ou.attack(sq, opp);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspired by calculate_checkers(), omit one table lookup for OU.

@sugyan sugyan self-requested a review October 11, 2023 14:44
Copy link
Owner

@sugyan sugyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding doc comments, tests, and refactoring! It helps a lot.
I have submitted a change request to na2hiro#1 regarding fmt. Please check it out.
I wrote the answer about Hash in the comment, is this OK?

src/position.rs Outdated Show resolved Hide resolved
src/position.rs Outdated Show resolved Hide resolved
@na2hiro
Copy link
Contributor Author

na2hiro commented Oct 13, 2023

Thank you for adding doc comments, tests, and refactoring! It helps a lot. I have submitted a change request to na2hiro#1 regarding fmt. Please check it out. I wrote the answer about Hash in the comment, is this OK?

Thanks for checking this! That looks good and I merged it to the PR.

That makes sense, I removed that TODO comment from code base. Also applied the suggested grammar fixes. Could you check this now? Thanks!

@na2hiro na2hiro requested a review from sugyan October 13, 2023 21:44
@sugyan sugyan merged commit 51c2378 into sugyan:main Oct 13, 2023
@na2hiro na2hiro deleted the add-doc-comments-and-tests branch October 14, 2023 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants