def hardcoded_dart(score): assert score in range(60, 159) if score in [72, 76, 84, 88, 92, 96] + list(range(98, 125)) + list( range(127, 159)): return Dart(20, 3) elif score in (69, 73, 89, 93, 95, 97, 126): return Dart(19, 3) elif score in (70, 86, 90, 94): return Dart(18, 3) elif score in (67, 75, 83, 87, 91): return Dart(17, 3) elif score in ( 64, 68, 80, ): return Dart(16, 3) elif score in (61, 77, 81, 85): return Dart(15, 3) elif score in (74, 78, 82): return Dart(14, 3) elif score in (63, 71, 79): return Dart(13, 3) elif score in (62, 66): return Dart(10, 3) elif score in (65, 125): return Dart(25, 1)
def below_40(score): assert score <= 40 if score % 2 == 0: return Dart(int(score / 2), 2) targets = [20, 16, 8, 18, 12, 4, 2, 1] for dart in targets: single = score - dart * 2 if single > 0: return Dart(single, 1) return None
def get_target(score, in_hand): assert score >= 2 if in_hand == 1: if score == 50: return Dart(25, 2) elif score in (182, 185, 188): return Dart(18, 3) elif score in (183, 186, 189): return Dart(19, 3) elif in_hand == 2: if score in range(61, 71): return Dart(score - 50, 3) elif score in (101, 104, 107, 110): return Dart(int((score - 50) / 3), 3) for score_range, algorithm in sequences.items(): if score_range[0] < score <= score_range[1]: return algorithm(score)
def dart_from_polar(distance: float, angle: float) -> Dart: sector = NUMBERS[int((angle + 9) % 360 / SECTOR_ANGLE)] for range_multipliers, dart in MULTIPLIERS.items(): if range_multipliers[0] <= distance / BOARD_SIZE < range_multipliers[1]: return dart(sector) return Dart(sector, 1)
def throw_dart(self, visit_score, in_hand): while True: dart_str = input('Insert your dart:\n') try: return Dart(dart_str) except: pass
def throw_dart(self, visit_score: int, in_hand: int) -> Dart: left = self.score - visit_score if in_hand == 1 and left <= 5 and left % 2 == 1 and self.score in ( 40, 32, 16, 12): target = Dart(20, 1) print('Trying to bust...') else: target = get_target(self.score - visit_score, in_hand) print(f'{self.score - visit_score} left. Wants {str(target)}...', end=' ') if not self.fast: sleep(2.0) result = simulate_throw(target, self.sigma) print(f'Got {str(result)}') return result
def below_40(score): assert score <= 40 if score % 2 == 0: return Dart(int(score / 2), 2) targets = [20, 16, 8, 18, 12, 4, 2, 1] for dart in targets: single = score - dart * 2 if single > 0: return Dart(single, 1) return None sequences = { (1, 40): lambda x: below_40(x), (40, 60): lambda x: Dart(x - 40, 1), (60, 158): lambda x: hardcoded_dart(x), (158, 501): lambda x: Dart(20, 3) } def get_target(score, in_hand): assert score >= 2 if in_hand == 1: if score == 50: return Dart(25, 2) elif score in (182, 185, 188): return Dart(18, 3) elif score in (183, 186, 189): return Dart(19, 3) elif in_hand == 2: if score in range(61, 71): return Dart(score - 50, 3) elif score in (101, 104, 107, 110):
def test_double(self): self.assertEqual(Dart('D20').score(), 40)
import numpy as np import random from darts.dart import Dart BOARD_SIZE = 1000 SECTOR_ANGLE = 18 BULL = 0.018676 OUTER_BULL = 0.046765 TRIPLE_RING = (0.291176, 0.314706) DOUBLE_RING = (0.476471, 0.5) MULTIPLIERS = { (0, BULL): lambda x: Dart(25, 2), (BULL, OUTER_BULL): lambda x: Dart(25, 1), TRIPLE_RING: lambda x: Dart(x, 3), DOUBLE_RING: lambda x: Dart(x, 2), (0.5, float(np.inf)): lambda x: Dart(0, 1) } NUMBERS = [ 6, 13, 4, 18, 1, 20, 5, 12, 9, 14, 11, 8, 16, 7, 19, 3, 17, 2, 15, 10 ] # [351, 360) u [0,9) -> 6, [9,27) -> 13, [27,45) -> 4, [45,63) -> 18, ..., [81,99) -> 20, ... def dart_from_polar(distance: float, angle: float) -> Dart: sector = NUMBERS[int((angle + 9) % 360 / SECTOR_ANGLE)] for range_multipliers, dart in MULTIPLIERS.items(): if range_multipliers[0] <= distance / BOARD_SIZE < range_multipliers[1]:
def test_bull(self): self.assertEqual(Dart('BULL').score(), 50)
def test_triple(self): self.assertEqual(Dart('T20').score(), 60)
def test_out(self): self.helper(Dart(0, 1), 0, 'OUT')
def test_20(self): self.helper(Dart(20, 1), 20, '20')
def test_d20(self): self.helper(Dart(20, 2), 40, 'D20')
def test_t20(self): self.helper(Dart(20, 3), 60, 'T20')
def test_bull(self): self.helper(Dart(50, 1), 50, 'BULL')