<s> from itertools import product <EOL> from random import shuffle <EOL> from deuces import Card , Evaluator <EOL> from sys import argv <EOL> """<STR_LIT>""" <EOL> ranks = [ "<STR_LIT:2>" , "<STR_LIT:3>" , "<STR_LIT:4>" , "<STR_LIT:5>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:T>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:A>" ] <EOL> suits = [ "<STR_LIT:s>" , "<STR_LIT:d>" , "<STR_LIT:c>" , "<STR_LIT:h>" ] <EOL> WIN = <NUM_LIT> <EOL> TIE = <NUM_LIT> <EOL> LOSE = <NUM_LIT> <EOL> evaluator = Evaluator ( ) <EOL> def expected_gain ( wins , ties ) : <EOL> """<STR_LIT>""" <EOL> my_val = wins * other_players <EOL> their_val = ( <NUM_LIT:1> - wins - ties ) * <NUM_LIT:1> <EOL> return my_val - their_val <EOL> def eval_table ( my_hand , shared_cards , other_hands ) : <EOL> """<STR_LIT>""" <EOL> my_score = evaluator . evaluate ( shared_cards , my_hand ) <EOL> max_other = <NUM_LIT> <EOL> for hand in other_hands : <EOL> opponent_result = evaluator . evaluate ( shared_cards , hand ) <EOL> if opponent_result < my_score : <EOL> return LOSE <EOL> max_other = min ( max_other , opponent_result ) <EOL> if max_other == my_score : <EOL> return TIE <EOL> else : <EOL> return WIN <EOL> def get_probabilities ( card_1 , card_2 , other_players , trials = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> deck = map ( lambda ( x , y ) : Card . new ( y + x ) , list ( product ( suits , ranks ) ) ) <EOL> wins = <NUM_LIT:0> <EOL> ties = <NUM_LIT:0> <EOL> deck . remove ( card_1 ) <EOL> deck . remove ( card_2 ) <EOL> for i in range ( trials ) : <EOL> shuffle ( deck ) <EOL> shared_cards = deck [ : <NUM_LIT:5> ] <EOL> other_hands = [ deck [ <NUM_LIT:5> + ( <NUM_LIT:2> * x ) : <NUM_LIT:7> + ( <NUM_LIT:2> * x ) ] for x in range ( other_players ) ] <EOL> result = eval_table ( [ card_1 , card_2 ] , shared_cards , other_hands ) <EOL> if result == WIN : <EOL> wins += <NUM_LIT:1> <EOL> elif result == TIE : <EOL> ties += <NUM_LIT:1> <EOL> return wins / float ( trials ) , ties / float ( trials ) <EOL> def write_to_file ( f , r1 , r2 , suiting , wins , ties ) : <EOL> """<STR_LIT>""" <EOL> cards = r1 + "<STR_LIT:\t>" + r2 + "<STR_LIT:\t>" + suiting <EOL> outcome = str ( wins ) + "<STR_LIT:\t>" + str ( ties ) + "<STR_LIT:\t>" + str ( expected_gain ( wins , ties ) ) <EOL> f . write ( cards + "<STR_LIT:\t>" + outcome + "<STR_LIT:\n>" ) <EOL> return <EOL> def main ( other_players , trials ) : <EOL> with open ( "<STR_LIT>" + str ( other_players ) , "<STR_LIT:w>" ) as f : <EOL> count = <NUM_LIT:0> <EOL> for c1 in range ( len ( ranks ) ) : <EOL> for c2 in range ( c1 , len ( ranks ) ) : <EOL> r1 = ranks [ c1 ] <EOL> r2 = ranks [ c2 ] <EOL> if r1 == r2 : <EOL> wins , ties = get_probabilities ( ( Card . new ( r1 + "<STR_LIT:s>" ) ) , Card . new ( r2 + "<STR_LIT:c>" ) , <EOL> other_players , trials ) <EOL> write_to_file ( f , r1 , r2 , "<STR_LIT>" , wins , ties ) <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> wins , ties = get_probabilities ( ( Card . new ( r1 + "<STR_LIT:s>" ) ) , Card . new ( r2 + "<STR_LIT:c>" ) , <EOL> other_players , trials ) <EOL> write_to_file ( f , r1 , r2 , "<STR_LIT>" , wins , ties ) <EOL> count += <NUM_LIT:1> <EOL> wins , ties = get_probabilities ( ( Card . new ( r1 + "<STR_LIT:s>" ) ) , Card . new ( r2 + "<STR_LIT:s>" ) , <EOL> other_players , trials ) <EOL> write_to_file ( f , r1 , r2 , "<STR_LIT>" , wins , ties ) <EOL> count += <NUM_LIT:1> <EOL> print "<STR_LIT>" + str ( count ) + "<STR_LIT>" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> other_players = int ( argv [ <NUM_LIT:1> ] ) <EOL> if other_players < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif other_players > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( argv ) == <NUM_LIT:3> : <EOL> trials = int ( argv [ <NUM_LIT:2> ] ) <EOL> else : <EOL> trials = <NUM_LIT> <EOL> main ( other_players , trials ) </s>
