Skip to content

Commit

Permalink
Add is_point
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiefl committed Jan 3, 2025
1 parent 67ff6bf commit 0e89b7f
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions pooltool/ruleset/three_cushion.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,45 @@ def _other(cue: str, event: Event) -> str:
raise Exception()


def is_turn_over(shot: System, constraints: ShotConstraints) -> bool:
assert constraints.cueable is not None
cue = constraints.cueable[0]
def is_point(shot: System) -> bool:
cue_id = shot.cue.cue_ball_id

Check warning on line 28 in pooltool/ruleset/three_cushion.py

View check run for this annotation

Codecov / codecov/patch

pooltool/ruleset/three_cushion.py#L28

Added line #L28 was not covered by tests

# Find when the second ball is first hit by the cue-ball

ball_hits = filter_events(
# Get collisions of the cue ball with the object balls.
cb_ob_collisions = filter_events(

Check warning on line 31 in pooltool/ruleset/three_cushion.py

View check run for this annotation

Codecov / codecov/patch

pooltool/ruleset/three_cushion.py#L31

Added line #L31 was not covered by tests
shot.events,
by_type(EventType.BALL_BALL),
by_ball(cue),
by_ball(cue_id),
)

hits = set()
for event in ball_hits:
hits.add(_other(cue, event))
if len(hits) == 2:
hit_ob_ids = set()
for event in cb_ob_collisions:
hit_ob_ids.add(_other(cue_id, event))
if len(hit_ob_ids) == 2:

Check warning on line 40 in pooltool/ruleset/three_cushion.py

View check run for this annotation

Codecov / codecov/patch

pooltool/ruleset/three_cushion.py#L37-L40

Added lines #L37 - L40 were not covered by tests
# This is the first (and perhaps only) instance of the cue ball hitting the
# second object ball.
second_ob_collision = event

Check warning on line 43 in pooltool/ruleset/three_cushion.py

View check run for this annotation

Codecov / codecov/patch

pooltool/ruleset/three_cushion.py#L43

Added line #L43 was not covered by tests
break
else:
# Both object balls were not contacted by the cue ball. No point.
return True

# Now calculate all cue-ball cushion hits before that event
# Both balls have been hit by the object ball. But were at least 3 cushions
# contacted before the second object ball was first hit? If yes, point, otherwise
# no.

cushion_hits = filter_events(
shot.events,
by_type(EventType.BALL_LINEAR_CUSHION),
by_ball(cue),
by_time(event.time, after=False),
by_ball(cue_id),
by_time(second_ob_collision.time, after=False),
)

return len(cushion_hits) < 3
return len(cushion_hits) >= 3

Check warning on line 60 in pooltool/ruleset/three_cushion.py

View check run for this annotation

Codecov / codecov/patch

pooltool/ruleset/three_cushion.py#L60

Added line #L60 was not covered by tests


def is_turn_over(shot: System, constraints: ShotConstraints) -> bool:
assert constraints.cueable is not None
return not is_point(shot)

Check warning on line 65 in pooltool/ruleset/three_cushion.py

View check run for this annotation

Codecov / codecov/patch

pooltool/ruleset/three_cushion.py#L64-L65

Added lines #L64 - L65 were not covered by tests


def is_game_over(
Expand Down

0 comments on commit 0e89b7f

Please sign in to comment.