def auto_set_numerical( set ): from paigow.pghand import PGHand if PGStrategyLogging.logging: print "\nauto_set_numerical BEGIN" picked_ordering = None # create sets with the three possible combinations. We'll be re-arranging # one of these to create sets so make them editable lists. tiles = set.tiles if PGStrategyLogging.logging: print " ... tiles: [ " + tiles[0].name + ", " + tiles[1].name + ", " + tiles[2].name + ", " + tiles[3].name + " ]" tiles1 = [ tiles[0], tiles[1], tiles[2], tiles[3] ] tiles2 = [ tiles[0], tiles[2], tiles[1], tiles[3] ] tiles3 = [ tiles[0], tiles[3], tiles[1], tiles[2] ] sets = ( None, PGSet.create( tiles1 ), PGSet.create( tiles2 ), PGSet.create( tiles3 ) ) ordering = picked_ordering_for_sets( sets ) # we founds something, re-order the tiles for it. if ordering > 0: reorder_hands_for_setting( set, ordering ) else: print "WTF? auto_sort didn't find anything?" return ordering
def test_player_is_ready(self): from paigow.pgset import PGSet pid = PGPlayerInDeal.create(self.game, self.player, 0) pid.set_dealt_sets((self.set1, self.set2, self.set3)) # original sets set1new = PGSet.create_with_tile_names( ("teen", "high eight", "high ten", "day")) set2new = PGSet.create_with_tile_names( ("mixed seven", "teen", "mixed five", "harmony four")) set3new = PGSet.create_with_tile_names( ("gee joon", "low ten", "mixed eight", "eleven")) pid.player_is_ready( set1new.tile_chars(), set2new.tile_chars(), set3new.tile_chars()) # original sets with tiles moved self.assertEqual(pid.set1, set1new.tile_chars()) self.assertEqual(pid.set2, set2new.tile_chars()) self.assertEqual(pid.set3, set3new.tile_chars()) pid.player_is_ready( set2new.tile_chars(), set3new.tile_chars(), set1new.tile_chars()) # re-ordered sets with tiles moved self.assertEqual(pid.set1, set2new.tile_chars()) self.assertEqual(pid.set2, set3new.tile_chars()) self.assertEqual(pid.set3, set1new.tile_chars()) self.assertRaises( ValueError, pid.player_is_ready, set2new.tile_chars(), set3new.tile_chars(), self.set4.tile_chars() ) # re-ordered sets with tiles moved, one bad # run the test when invoked as a test (this is boilerplate
def auto_set_numerical(set): from paigow.pghand import PGHand if PGStrategyLogging.logging: print "\nauto_set_numerical BEGIN" picked_ordering = None # create sets with the three possible combinations. We'll be re-arranging # one of these to create sets so make them editable lists. tiles = set.tiles if PGStrategyLogging.logging: print " ... tiles: [ " + tiles[0].name + ", " + tiles[ 1].name + ", " + tiles[2].name + ", " + tiles[3].name + " ]" tiles1 = [tiles[0], tiles[1], tiles[2], tiles[3]] tiles2 = [tiles[0], tiles[2], tiles[1], tiles[3]] tiles3 = [tiles[0], tiles[3], tiles[1], tiles[2]] sets = (None, PGSet.create(tiles1), PGSet.create(tiles2), PGSet.create(tiles3)) ordering = picked_ordering_for_sets(sets) # we founds something, re-order the tiles for it. if ordering > 0: reorder_hands_for_setting(set, ordering) else: print "WTF? auto_sort didn't find anything?" return ordering
def test_x321_set_ordering( self ): set1 = PGSet.create_with_tile_names( ( "low ten", "mixed nine", "harmony four", "low four" ) ) set2 = PGSet.create_with_tile_names( ( "low six", "low six", "low ten", "high seven" ) ) self.assertFalse( first_set_is_better( set1, set2 ) ) set1 = PGSet.create_with_tile_names( ( "eleven", "mixed five", "high eight", "high seven" ) ) set2 = PGSet.create_with_tile_names( ( "low ten", "mixed nine", "high ten", "mixed five" ) ) self.assertFalse( first_set_is_better( set1, set2 ) )
def preview_hands( request, game_id, deal_number ): new_set1 = PGSet.create_with_tile_chars( request.GET['set1'] ).tile_ordering_for_set_hands() new_set2 = PGSet.create_with_tile_chars( request.GET['set2'] ).tile_ordering_for_set_hands() new_set3 = PGSet.create_with_tile_chars( request.GET['set3'] ).tile_ordering_for_set_hands() game = PGGame.objects.get( id = game_id ) player = session_player( request ) pig = game.player_in_game( player ) pig.player_is_previewing_hands() return HttpResponse( "|" + new_set1 + "|" + new_set2 + "|" + new_set3 + "|" )
def setUp( self ): from paigow.pgset import PGSet self.player = PGPlayer.create( "pidTest", "*****@*****.**", "xxx" ) self.player.save() self.game = PGGame.create( "test" ) self.game.save() self.set1 = PGSet.create_with_tile_names( ( "teen", "high ten", "day", "high eight" ) ) self.set2 = PGSet.create_with_tile_names( ( "mixed seven", "mixed five", "harmony four", "teen" ) ) self.set3 = PGSet.create_with_tile_names( ( "gee joon", "low ten", "mixed eight", "eleven" ) ) self.set4 = PGSet.create_with_tile_names( ( "gee joon", "low ten", "mixed eight", "high ten" ) )
def preview_hands(request, game_id, deal_number): new_set1 = PGSet.create_with_tile_chars( request.GET['set1']).tile_ordering_for_set_hands() new_set2 = PGSet.create_with_tile_chars( request.GET['set2']).tile_ordering_for_set_hands() new_set3 = PGSet.create_with_tile_chars( request.GET['set3']).tile_ordering_for_set_hands() game = PGGame.objects.get(id=game_id) player = session_player(request) pig = game.player_in_game(player) pig.player_is_previewing_hands() return HttpResponse("|" + new_set1 + "|" + new_set2 + "|" + new_set3 + "|")
def test_x321_set_ordering(self): set1 = PGSet.create_with_tile_names( ("low ten", "mixed nine", "harmony four", "low four")) set2 = PGSet.create_with_tile_names( ("low six", "low six", "low ten", "high seven")) self.assertFalse(first_set_is_better(set1, set2)) set1 = PGSet.create_with_tile_names( ("eleven", "mixed five", "high eight", "high seven")) set2 = PGSet.create_with_tile_names( ("low ten", "mixed nine", "high ten", "mixed five")) self.assertFalse(first_set_is_better(set1, set2))
def setUp(self): from paigow.pgset import PGSet self.player = PGPlayer.create("pidTest", "*****@*****.**", "xxx") self.player.save() self.game = PGGame.create("test") self.game.save() self.set1 = PGSet.create_with_tile_names( ("teen", "high ten", "day", "high eight")) self.set2 = PGSet.create_with_tile_names( ("mixed seven", "mixed five", "harmony four", "teen")) self.set3 = PGSet.create_with_tile_names( ("gee joon", "low ten", "mixed eight", "eleven")) self.set4 = PGSet.create_with_tile_names( ("gee joon", "low ten", "mixed eight", "high ten"))
def special_ordering_for_high_pair( self, teen_or_day ): # test reordering to get pair set = PGSet.create_with_tile_names( ( teen_or_day, "eleven", "high eight", teen_or_day ) ) self.assertEqual( ordering_for_special_hands( set ), 1 ) # test no reordering to get pair set = PGSet.create_with_tile_names( ( "eleven", teen_or_day, "high eight", teen_or_day ) ) self.assertEqual( ordering_for_special_hands( set ), 1 ) # test no reorder to split pair set = PGSet.create_with_tile_names( ( "mixed nine", teen_or_day, "high eight", teen_or_day ) ) self.assertEqual( ordering_for_special_hands( set ), 1 ) # test reorder to split pair set = PGSet.create_with_tile_names( ( "mixed nine", "high eight", teen_or_day, teen_or_day ) ) self.assertEqual( ordering_for_special_hands( set ), 2 )
def test_player_is_ready( self ): from paigow.pgset import PGSet pid = PGPlayerInDeal.create( self.game, self.player, 0 ) pid.set_dealt_sets( ( self.set1, self.set2, self.set3 ) ) # original sets set1new = PGSet.create_with_tile_names( ( "teen", "high eight", "high ten", "day" ) ) set2new = PGSet.create_with_tile_names( ( "mixed seven", "teen", "mixed five", "harmony four" ) ) set3new = PGSet.create_with_tile_names( ( "gee joon", "low ten", "mixed eight", "eleven" ) ) pid.player_is_ready( set1new.tile_chars(), set2new.tile_chars(), set3new.tile_chars() ) # original sets with tiles moved self.assertEqual( pid.set1, set1new.tile_chars() ) self.assertEqual( pid.set2, set2new.tile_chars() ) self.assertEqual( pid.set3, set3new.tile_chars() ) pid.player_is_ready( set2new.tile_chars(), set3new.tile_chars(), set1new.tile_chars() ) # re-ordered sets with tiles moved self.assertEqual( pid.set1, set2new.tile_chars() ) self.assertEqual( pid.set2, set3new.tile_chars() ) self.assertEqual( pid.set3, set1new.tile_chars() ) self.assertRaises( ValueError, pid.player_is_ready, set2new.tile_chars(), set3new.tile_chars(), self.set4.tile_chars() ) # re-ordered sets with tiles moved, one bad # run the test when invoked as a test (this is boilerplate
def test_special_ordering_for_other_pairs( self ): # test reorder sevens to split pair set = PGSet.create_with_tile_names( ( "mixed seven", "mixed seven", "teen", "day" ) ) self.assertEqual( ordering_for_special_hands( set ), 2 ) # test no reorder sevens to split pair set = PGSet.create_with_tile_names( ( "mixed seven", "teen", "mixed seven", "day" ) ) self.assertEqual( ordering_for_special_hands( set ), 1 ) # test reorder eights to make pair set = PGSet.create_with_tile_names( ( "mixed eight", "low ten", "mixed eight", "day" ) ) self.assertEqual( ordering_for_special_hands( set ), 2 ) # test no reorder eights to make pair set = PGSet.create_with_tile_names( ( "low ten", "day", "mixed eight", "mixed eight" ) ) self.assertEqual( ordering_for_special_hands( set ), 1 ) # test reorder nines to make pair set = PGSet.create_with_tile_names( ( "low ten", "day", "mixed nine", "mixed nine" ) ) self.assertEqual( ordering_for_special_hands( set ), 2 ) # test geen joon splitting and not set = PGSet.create_with_tile_names( ( "gee joon", "gee joon", "long six", "low six" ) ) self.assertEqual( ordering_for_special_hands( set ), 2 ) set = PGSet.create_with_tile_names( ( "gee joon", "gee joon", "long six", "mixed seven" ) ) self.assertEqual( ordering_for_special_hands( set ), 1 )
def test_special_ordering_for_other_pairs(self): # test reorder sevens to split pair set = PGSet.create_with_tile_names( ("mixed seven", "mixed seven", "teen", "day")) self.assertEqual(ordering_for_special_hands(set), 2) # test no reorder sevens to split pair set = PGSet.create_with_tile_names( ("mixed seven", "teen", "mixed seven", "day")) self.assertEqual(ordering_for_special_hands(set), 1) # test reorder eights to make pair set = PGSet.create_with_tile_names( ("mixed eight", "low ten", "mixed eight", "day")) self.assertEqual(ordering_for_special_hands(set), 2) # test no reorder eights to make pair set = PGSet.create_with_tile_names( ("low ten", "day", "mixed eight", "mixed eight")) self.assertEqual(ordering_for_special_hands(set), 1) # test reorder nines to make pair set = PGSet.create_with_tile_names( ("low ten", "day", "mixed nine", "mixed nine")) self.assertEqual(ordering_for_special_hands(set), 2) # test geen joon splitting and not set = PGSet.create_with_tile_names( ("gee joon", "gee joon", "long six", "low six")) self.assertEqual(ordering_for_special_hands(set), 2) set = PGSet.create_with_tile_names( ("gee joon", "gee joon", "long six", "mixed seven")) self.assertEqual(ordering_for_special_hands(set), 1)
def assure_player_in_deal(self, player, deal_number): from pgplayerindeal import PGPlayerInDeal from paigow.pgset import PGSet from paigow.pgstrategy import PGStrategy pgpid_player = self.player_in_deal(player, deal_number) if not pgpid_player: # print "cannot find player " + str(player) + "in game " + str(self) + ", creating" is_second_player = len(self.players_in_deal(deal_number)) > 0 # print "... is second player: " + str(is_second_player) pgpid_player = PGPlayerInDeal.create(self, player, deal_number) pgpid_player.save() # so self.players() will find it. # TBD: remove assumption that there are only two players. This # decides whether this player gets the first set of tiles or the # second set of tiles. index = 0 if (is_second_player): index = 1 # Create the hands and fill them. deal = self.deal(deal_number) sets = [] for i in range(3): set = PGSet.create([ deal.tile(index), deal.tile(index + 2), deal.tile(index + 4), deal.tile(index + 6), ]) #from paigow.pgstrategy import auto_set_numerical #auto_set_numerical(set) sets.append(set) index += 8 # if the player is a computer, then set the tiles. if player.name == "computer": sets = PGStrategy.auto_set(sets) # remember what hands were dealt; when it comes time for # the player to say how they set, we want to verify that # they didn't cheat ;) pgpid_player.set_dealt_sets(sets) # remember this player is here, needs to be done before player_is_ready pgpid_player.save() # the computer is done right away, always. if player.name == "computer": pgpid_player.player_is_ready(sets[0].tile_chars(), sets[1].tile_chars(), sets[2].tile_chars()) pgpid_player.save() return pgpid_player
def special_ordering_for_high_pair(self, teen_or_day): # test reordering to get pair set = PGSet.create_with_tile_names( (teen_or_day, "eleven", "high eight", teen_or_day)) self.assertEqual(ordering_for_special_hands(set), 1) # test no reordering to get pair set = PGSet.create_with_tile_names( ("eleven", teen_or_day, "high eight", teen_or_day)) self.assertEqual(ordering_for_special_hands(set), 1) # test no reorder to split pair set = PGSet.create_with_tile_names( ("mixed nine", teen_or_day, "high eight", teen_or_day)) self.assertEqual(ordering_for_special_hands(set), 1) # test reorder to split pair set = PGSet.create_with_tile_names( ("mixed nine", "high eight", teen_or_day, teen_or_day)) self.assertEqual(ordering_for_special_hands(set), 2)
def assure_player_in_deal( self, player, deal_number ): from pgplayerindeal import PGPlayerInDeal from paigow.pgset import PGSet from paigow.pgstrategy import PGStrategy pgpid_player = self.player_in_deal( player, deal_number ) if not pgpid_player: # print "cannot find player " + str(player) + "in game " + str(self) + ", creating" is_second_player = len(self.players_in_deal( deal_number )) > 0 # print "... is second player: " + str(is_second_player) pgpid_player = PGPlayerInDeal.create( self, player, deal_number ) pgpid_player.save() # so self.players() will find it. # TBD: remove assumption that there are only two players. This # decides whether this player gets the first set of tiles or the # second set of tiles. index = 0 if ( is_second_player ): index = 1 # Create the hands and fill them. deal = self.deal( deal_number ) sets = [] for i in range(3): set = PGSet.create( [ deal.tile( index ), deal.tile( index + 2 ), deal.tile( index + 4 ), deal.tile( index + 6 ), ] ) #from paigow.pgstrategy import auto_set_numerical #auto_set_numerical(set) sets.append( set ) index += 8 # if the player is a computer, then set the tiles. if player.name == "computer": sets = PGStrategy.auto_set( sets ) # remember what hands were dealt; when it comes time for # the player to say how they set, we want to verify that # they didn't cheat ;) pgpid_player.set_dealt_sets( sets ) # remember this player is here, needs to be done before player_is_ready pgpid_player.save() # the computer is done right away, always. if player.name == "computer": pgpid_player.player_is_ready( sets[0].tile_chars(), sets[1].tile_chars(), sets[2].tile_chars() ) pgpid_player.save() return pgpid_player
def set( self, set_num ): from paigow.pgset import PGSet if set_num == 1: pgset = self.set1 elif set_num == 2: pgset = self.set2 elif set_num == 3: pgset = self.set3 else: print "PGPlayerInSet asked for invalid set number " + str( set_num ) raise ValueError return PGSet.create_with_tile_chars( pgset )
def set(self, set_num): from paigow.pgset import PGSet if set_num == 1: pgset = self.set1 elif set_num == 2: pgset = self.set2 elif set_num == 3: pgset = self.set3 else: print "PGPlayerInSet asked for invalid set number " + str(set_num) raise ValueError return PGSet.create_with_tile_chars(pgset)
def test_auto_set( self ): #PGStrategyLogging.logging = True set = PGSet.create_with_tile_names( ( "day", "low ten", "mixed five", "eleven" ) ) self.assertEqual( auto_set_numerical( set ), 2 ) set = PGSet.create_with_tile_names( ( "low four", "low ten", "eleven", "low six" ) ) self.assertEqual( auto_set_numerical( set ), 2 ) set = PGSet.create_with_tile_names( ( "teen", "low six", "harmony four", "long six" ) ) self.assertEqual( auto_set_numerical( set ), 1 ) set = PGSet.create_with_tile_names( ( "low four", "mixed nine", "high eight", "mixed eight" ) ) self.assertEqual( auto_set_numerical( set ), 1 ) set = PGSet.create_with_tile_names( ( "teen", "low ten", "eleven", "mixed nine" ) ) self.assertEqual( auto_set_numerical( set ), 3 ) set = PGSet.create_with_tile_names( ( "low ten", "mixed nine", "day", "high ten" ) ) self.assertEqual( auto_set_numerical( set ), 3 )
def test_auto_set(self): #PGStrategyLogging.logging = True set = PGSet.create_with_tile_names( ("day", "low ten", "mixed five", "eleven")) self.assertEqual(auto_set_numerical(set), 2) set = PGSet.create_with_tile_names( ("low four", "low ten", "eleven", "low six")) self.assertEqual(auto_set_numerical(set), 2) set = PGSet.create_with_tile_names( ("teen", "low six", "harmony four", "long six")) self.assertEqual(auto_set_numerical(set), 1) set = PGSet.create_with_tile_names( ("low four", "mixed nine", "high eight", "mixed eight")) self.assertEqual(auto_set_numerical(set), 1) set = PGSet.create_with_tile_names( ("teen", "low ten", "eleven", "mixed nine")) self.assertEqual(auto_set_numerical(set), 3) set = PGSet.create_with_tile_names( ("low ten", "mixed nine", "day", "high ten")) self.assertEqual(auto_set_numerical(set), 3)
def ordering_for_special_hands(set): if PGStrategyLogging.logging: print "\norder for set " + str(set) # we'll be moving tiles around; create a temp set. loc_set = PGSet.create(set.tiles) # always use two pairs ordering = loc_set.ordering_with_two_pair() if PGStrategyLogging.logging: print " order for two pairs " + str(ordering) if ordering: return ordering # if we have a pair, there are exceptions ordering = loc_set.ordering_with_pair() if PGStrategyLogging.logging: print " order for one pairs " + str(ordering) if ordering: switch_it = False # get the tile with the pair reorder_hands_for_setting(loc_set, ordering) pair_tile = loc_set.tiles[0] # we never split pairs of 4s, 5s, 10s or 11s if pair_tile.tile_value == 4 or \ pair_tile.tile_value == 5 or \ pair_tile.tile_value == 10 or \ pair_tile.tile_value == 11: return ordering # we split teens/days if the other two tiles are both seven or higher # TBD: would we really want this in the 3-hand, with 8/9? # Teen-bo/seven seems a safer bet than wong/gong if pair_tile.is_teen_or_day() and last_two_tiles_are_in( set, (7, 8, 9)): switch_it = True # we split nines if the other two are # both within (ten, teen, day) elif pair_tile.tile_value == 9 and last_two_tiles_are_in( set, (2, 12, 10)): switch_it = True # we split eights if the other two are # both within (elevens, teen, day) elif pair_tile.tile_value == 8 and last_two_tiles_are_in( set, (2, 12, 11)): switch_it = True # we split sixes and sevens if the other two are teen/day elif (pair_tile.tile_value == 7 or pair_tile.tile_value == 6) and last_two_tiles_are_in(set, (2, 12)): switch_it = True # we split gee joon if the other two are in (five, six) or (teen, day) # note we don't want to split them if one is five/six and the other is teen/day; # only if both are five/six or both are teen/day. elif pair_tile.tile_value == 3 and \ ( last_two_tiles_are_in( set, ( 2, 12 ) ) or last_two_tiles_are_in( set, ( 5, 6 ) ) ): switch_it = True # if the ordering was 1, then the first two or the last # two are the pair, and we can just switch the middle # two tiles (ordering <-- 2). If the ordering was 2 or three, # then the pair wasn't already there, so we use the # tiles in their original order (ordering <-- 1). We can # tell by the ordering: if it's 1, then the first two and # last two are pairs. if switch_it: if ordering == 1: ordering = 2 else: ordering = 1 return ordering
def ordering_for_special_hands( set ): if PGStrategyLogging.logging: print "\norder for set " + str(set) # we'll be moving tiles around; create a temp set. loc_set = PGSet.create( set.tiles ) # always use two pairs ordering = loc_set.ordering_with_two_pair() if PGStrategyLogging.logging: print " order for two pairs " + str(ordering) if ordering: return ordering # if we have a pair, there are exceptions ordering = loc_set.ordering_with_pair() if PGStrategyLogging.logging: print " order for one pairs " + str(ordering) if ordering: switch_it = False # get the tile with the pair reorder_hands_for_setting( loc_set, ordering ) pair_tile = loc_set.tiles[0] # we never split pairs of 4s, 5s, 10s or 11s if pair_tile.tile_value == 4 or \ pair_tile.tile_value == 5 or \ pair_tile.tile_value == 10 or \ pair_tile.tile_value == 11: return ordering # we split teens/days if the other two tiles are both seven or higher # TBD: would we really want this in the 3-hand, with 8/9? # Teen-bo/seven seems a safer bet than wong/gong if pair_tile.is_teen_or_day() and last_two_tiles_are_in( set, ( 7, 8, 9 ) ): switch_it = True # we split nines if the other two are # both within (ten, teen, day) elif pair_tile.tile_value == 9 and last_two_tiles_are_in( set, ( 2, 12, 10 ) ): switch_it = True # we split eights if the other two are # both within (elevens, teen, day) elif pair_tile.tile_value == 8 and last_two_tiles_are_in( set, ( 2, 12, 11 ) ): switch_it = True # we split sixes and sevens if the other two are teen/day elif (pair_tile.tile_value == 7 or pair_tile.tile_value == 6) and last_two_tiles_are_in( set, ( 2, 12 ) ): switch_it = True # we split gee joon if the other two are in (five, six) or (teen, day) # note we don't want to split them if one is five/six and the other is teen/day; # only if both are five/six or both are teen/day. elif pair_tile.tile_value == 3 and \ ( last_two_tiles_are_in( set, ( 2, 12 ) ) or last_two_tiles_are_in( set, ( 5, 6 ) ) ): switch_it = True # if the ordering was 1, then the first two or the last # two are the pair, and we can just switch the middle # two tiles (ordering <-- 2). If the ordering was 2 or three, # then the pair wasn't already there, so we use the # tiles in their original order (ordering <-- 1). We can # tell by the ordering: if it's 1, then the first two and # last two are pairs. if switch_it: if ordering == 1: ordering = 2 else: ordering = 1 return ordering