fix(3p): resolve North tile from hand in handle_kita to prevent hand corruption - #180
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a sanma (3P) game-state corruption bug where handle_kita could default to tile ID 0 when Action.tile is None, failing to remove the North tile from the hand and cascading into incorrect legal_actions() behavior (Issue #179).
Changes:
- Update
GameState3P::handle_kitato resolve the actual North tile from the player’s hand when the incoming Kita action omitstile. - Add regression/unit tests covering Kita with
tile=Noneand ensuring follow-uplegal_actions()include expected actions (e.g., riichi/ankan/tsumo) and exclude spurious Kita when no North remains.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
riichienv-core/src/state_3p/sanma.rs |
Fixes Kita tile resolution to prevent silent hand corruption when Action.tile is missing. |
riichienv-core/src/tests.rs |
Adds sanma regression tests reproducing the tile=None Kita scenario and validating downstream legality/action masking. |
Comments suppressed due to low confidence (1)
riichienv-core/src/state_3p/sanma.rs:33
handle_kitastill has a fallback path that can resolve to a non-North tile (including0) if no North is found in the hand, and the function then proceeds to push that tile intokita_tileseven if it was never removed from the hand. That can reintroduce hand corruption for invalid inputs/call sites. Consider failing fast here (e.g., return/trigger an error) when no North tile exists in the hand, and/or assert that the resolved tile is North and was actually removed.
let tile = match act.tile {
Some(t) if t / 4 == 30 => t,
_ => self.players[p_idx]
.hand
.iter()
.find(|&&t| t / 4 == 30)
.copied()
.unwrap_or_else(|| {
act.tile
.unwrap_or_else(|| act.consume_tiles.first().copied().unwrap_or(0))
}),
};
// Remove North tile from hand
if let Some(idx) = self.players[p_idx].hand.iter().position(|&t| t == tile) {
self.players[p_idx].hand.remove(idx);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #179.
handle_kitafell back to tile ID 0 (1m) when the incomingActionhadtile = None, silently failing to remove the North tile from the player's hand. This corrupted subsequent game state: the hand retained an extra tile, causinglegal_actions()to omit valid actions (reach, ankan, hora) and include spurious Kita entries.Root cause
When
act.tileisNoneandconsume_tilesis empty (both true for a generic Kita action),tileresolved to 0 (1m). The subsequenthand.remove()found no matching tile, so nothing was removed. The rinshan draw then added an extra tile, leaving the hand one tile too large for the rest of the round.Fix
handle_kitanow validatesact.tileand, when it is missing or not a North tile, looks up the actual North tile from the player's hand: