Skip to content

Commit

Permalink
fix frame end handling
Browse files Browse the repository at this point in the history
Detect winners/losers during breaks to clarify what's going to happen.

End frame on last black foul.

Automatically respot black on equal score at end of frame.

Log frame stats at end of frame, not at beginning of new frame.
  • Loading branch information
jnikula committed Dec 5, 2022
1 parent b00cee7 commit 28235da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 5 additions & 2 deletions docs/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ Press the snooker ball buttons to pot (numbers **1-7** on the keyboard).
the number of balls left on the `Edit Page`_.

* If, after all balls have been potted, two (or all) players have equal points,
increase the number of balls left on the `Edit Page`_ to add black as the
respotted black.
the game continues with a re-spotted black.

Press the foul ball buttons to enter fouls (**f** key immediately followed by
numbers **4-7** on the keyboard).
Expand All @@ -104,6 +103,10 @@ numbers **4-7** on the keyboard).
* If the previous player committed a foul, you can request them to play again
using the **PLAY AGAIN** button.

* If there's a foul with just the last black remaining, the frame ends. If two
(or all) players have equal points, the game continues with a re-spotted
black.

Press the **END TURN** button to end turn (space bar on the beyboard). Fouls end
turn automatically.

Expand Down
16 changes: 12 additions & 4 deletions src/lib/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ class State {
this.cur_pid = player.pid;
this.cur_pos = player.pos;
}

// end frame or respot black?
if (this._is_frame_over())
this._end_frame();
else if (this.num_colors() === 0)
this._num_balls++;
}

_pot_points(points: number): void {
Expand All @@ -345,9 +351,10 @@ class State {
let p: Player = this.current_player();
p.pot_points(points);

// game over
if (this.num_points() === 0)
this._end_turn();
else
this._detect_win_lose();
}

_can_pot_red(): boolean {
Expand Down Expand Up @@ -423,6 +430,10 @@ class State {

player.log_foul(value);

// foul on last black, drop ball count to zero to cause end frame
if (this.num_colors() === 1)
this._num_balls--;

this._end_turn();

this.foul = true;
Expand Down Expand Up @@ -454,9 +465,6 @@ class State {
}

new_frame(): void {
// FIXME: this should happen earlier, when frame is over
this._end_frame();

// game
this.cur_perm++;
if (this.cur_perm >= permutations.length)
Expand Down

0 comments on commit 28235da

Please sign in to comment.