Created
July 22, 2014 20:39
-
-
Save kmorel/f26bc008ab53e100ec02 to your computer and use it in GitHub Desktop.
A Python script to compute the odds of a game of Screw Your Neighbor (a.k.a. Ranter-Go-Round) by trying all possible combinations of deals.
This file contains 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
#! /usr/bin/env python | |
opponent_switch_threshold = 8 | |
dealer_pulls_threshold = 10 | |
def opponent_switches(dealer_card, opponent_card): | |
"""Given that the dealer and opponent have the given cards, returns | |
(dealer_win, opponent_win, tie) as the odds for each outcome if the opponent | |
switches with the dealer.""" | |
if dealer_card == 13: | |
# Special rule if the dealer has a king. In this case he does not have | |
# to switch. Technically this could result in a loss or a tie if the | |
# opponent was switching with an ace or king, but we'll just call it a | |
# loss for the opponent being so stupid. | |
return (1.0, 0.0, 0.0) | |
if dealer_card < opponent_card: | |
# Dealer gave a lower card so the dealer's win is assured. | |
return (1.0, 0.0, 0.0) | |
else: | |
dealer_win = 0 | |
opponent_win = 0 | |
tie = 0 | |
for new_card in xrange(2, 15): | |
if dealer_card < new_card: | |
dealer_win += 1.0/13.0 | |
elif dealer_card == new_card: | |
tie += 1.0/13.0 | |
else: | |
opponent_win += 1.0/13.0 | |
return (dealer_win, opponent_win, tie) | |
def dealer_pulls(opponent_card): | |
"""Given that the opponent has the given card, the opponent decided to keep | |
the card, and the dealer decided to pull a new card, returns | |
(dealer_win, opponent_win, tie) as the odds for each outcome.""" | |
dealer_win = 0 | |
opponent_win = 0 | |
tie = 0 | |
for new_card in xrange(2, 15): | |
if new_card > opponent_card: | |
dealer_win += 1.0/13.0 | |
elif new_card == opponent_card: | |
tie += 1.0/13.0 | |
else: | |
opponent_win += 1.0/13.0 | |
return (dealer_win, opponent_win, tie) | |
def opponent_keeps(dealer_card, opponent_card): | |
"""Given that the dealer and opponent have the given cards, returns | |
(dealer_win, opponent_win, tie) as the odds for each outcome if the opponent | |
keeps the card.""" | |
if dealer_card <= dealer_pulls_threshold: | |
return dealer_pulls(opponent_card) | |
else: | |
return (float(dealer_card > opponent_card), \ | |
float(dealer_card < opponent_card), \ | |
float(dealer_card == opponent_card)) | |
def odds_from_deal(dealer_card, opponent_card): | |
"""Given that the dealer and opponent have the given cards, returns | |
(dealer_win, opponent_win, tie) as the odds for each outcome using the | |
defined thresholds.""" | |
if opponent_card <= opponent_switch_threshold: | |
return opponent_switches(dealer_card, opponent_card) | |
else: | |
return opponent_keeps(dealer_card, opponent_card) | |
def game_odds(): | |
"""Trys all possible hands to determine odds for game using defined thresholds.""" | |
dealer_win = 0 | |
opponent_win = 0 | |
tie = 0 | |
for dealer_card in xrange(2,15): | |
for opponent_card in xrange(2,15): | |
(game_dealer_win, game_opponent_win, game_tie) = \ | |
odds_from_deal(dealer_card, opponent_card) | |
dealer_win += game_dealer_win/169.0 | |
opponent_win += game_opponent_win/169.0 | |
tie += game_tie/169.0 | |
return (dealer_win, opponent_win, tie) | |
def try_all_thresholds(): | |
print "dealer pulls threshold,opponent switch threshold,dealer win,opponent win,tie" | |
global dealer_pulls_threshold | |
for dealer_pulls_threshold in xrange(5,13): | |
global opponent_switch_threshold | |
for opponent_switch_threshold in xrange(5,13): | |
(dealer_win, opponent_win, tie) = game_odds() | |
print "%d,%d,%f,%f,%f" % (dealer_pulls_threshold, opponent_switch_threshold, dealer_win, opponent_win, tie) | |
def try_all_deals(): | |
"""Finds the odds for all possible deals.""" | |
print "dealer card,opponent card,dealer odds,opponent odds,tie odds" | |
for dealer_card in xrange(2,15): | |
for opponent_card in xrange(2,15): | |
(dealer_odds, opponent_odds, tie_odds) = \ | |
odds_from_deal(dealer_card, opponent_card) | |
print "%d,%d,%f,%f,%f" % (dealer_card, opponent_card, dealer_odds, opponent_odds, tie_odds) | |
if __name__ == '__main__': | |
try_all_thresholds() | |
#try_all_deals() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment