Skip to content

Encapsulate Bag; double-ended drawing#9

Merged
andy-k merged 3 commits intomainfrom
bag-encapsulation
Mar 25, 2026
Merged

Encapsulate Bag; double-ended drawing#9
andy-k merged 3 commits intomainfrom
bag-encapsulation

Conversation

@andy-k
Copy link
Owner

@andy-k andy-k commented Mar 23, 2026

Summary

  • Commit 1: Encapsulate Bag internals — make field private, add semantic methods (return_tiles, return_tile, set_from_iter, as_slice, len, is_empty, remove_tile)
  • Commit 2: Double-ended bag drawing — even players draw from back, odd from front. Bag uses front cursor (fc) with tiles[0..fc] as dead space. Optimized put_back with special cases for m=1 (1 random call), m=2 (2 random calls, swap doubles as coin flip), and general m>=3 (interleave with early break). Order-preserving remove_tile.
  • Commit 3: put_back uses self.tiles as scratch buffer instead of fixed [0u8; 16], removing the m<=16 assumption. When fc >= 2m, scratch lives in dead space (zero allocation). Otherwise, scratch is appended to the vec's back.

Why

Prepares infrastructure for game pairs (playing each seed twice with different initial players).

Notes

  • main_endgame.rs unchanged — still uses reset_and_draw_tiles (back-only)
  • Callers that want double-ended drawing use reset_and_draw_tiles_double_ended
  • No unsafe except in the general (m>=3) put_back loop, with safety proof in comments

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

andy-k and others added 2 commits March 24, 2026 08:19
Make Bag(Vec<u8>) field private. Add methods: return_tiles, return_tile,
set_from_iter, as_slice, len, is_empty, swap_remove_tile. Replace all
external bag.0 accesses across 8 files. No behavior change.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Rewrite Bag as a struct with a front cursor (fc) so tiles can be
drawn from either end without allocation. Add went_first field to
GameState and reset_and_draw_tiles_double_ended for the new drawing
path. Existing reset_and_draw_tiles uses replenish_back only (no
behavior change for main_endgame).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@andy-k andy-k force-pushed the bag-encapsulation branch from f615534 to 5d5f903 Compare March 24, 2026 02:43
@andy-k andy-k changed the title Refactor Bag for double-ended drawing support Encapsulate Bag; double-ended drawing Mar 24, 2026
Removes the m<=16 assumption. When fc >= 2m, scratch lives in dead
space with zero allocation. When fc < 2m, scratch is appended to
the vec's back (within existing capacity in practice).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@andy-k andy-k force-pushed the bag-encapsulation branch from 1ff3245 to 433dcb0 Compare March 25, 2026 13:38
@andy-k andy-k merged commit 6d7586d into main Mar 25, 2026
6 checks passed
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.

1 participant