Advertisement
Guest User

Permuting Aces into the Dealer's Hand

a guest
Jun 6th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. # Place four aces from the top of the deck into the dealer's hand
  2. # by Mark Gritter, June 6 2012
  3.  
  4. from Queue import deque
  5.  
  6. def inShuffleSource( p, numCards ):
  7.     if p % 2 == 0:
  8.         return numCards / 2 + p / 2
  9.     else:
  10.         return ( p - 1 ) / 2
  11.  
  12. def outShuffleSource( p, numCards ):
  13.     if p % 2 == 0:
  14.         return p / 2
  15.     else:
  16.         return numCards / 2 + ( p - 1 ) / 2
  17.  
  18. inShuffleIdx = [ inShuffleSource( p, 52 ) for p in xrange( 52 ) ]
  19. outShuffleIdx = [ outShuffleSource( p, 52 ) for p in xrange( 52 ) ]
  20.  
  21. def inShuffle( deck ):
  22.     return ''.join( deck[inShuffleIdx[p]] for p in xrange( 52 ) )
  23.  
  24. def outShuffle( deck ):
  25.     return ''.join( deck[outShuffleIdx[p]] for p in xrange( 52 ) )
  26.  
  27. def search( deck, goal ):
  28.     visited = { deck : ( 'origin', None ) }
  29.     queue = deque( [deck] )
  30.     while len( queue ) > 0:
  31.         parent = queue.popleft()
  32.        
  33.         left = inShuffle( parent )
  34.         if left == goal:
  35.             visited[left] = ( 'out', parent )
  36.             return visited
  37.         elif left in visited:
  38.             continue
  39.         else:
  40.             visited[left] = ( 'in', parent )
  41.             queue.append( left )
  42.  
  43.         right = outShuffle( parent )
  44.         if right == goal:
  45.             visited[right] = ( 'out', parent )
  46.             return visited
  47.         elif right in visited:
  48.             continue
  49.         else:
  50.             visited[right] = ( 'out', parent )
  51.             queue.append( right )
  52.            
  53.     raise Exception, "No path found."
  54.  
  55. def path( goal, visited ):
  56.     lastMove = None
  57.     curr = goal
  58.     moves = []
  59.     while lastMove != 'origin':
  60.         ( move, prev ) = visited[ curr ]
  61.         moves.append( (move, curr) )
  62.         lastMove = move
  63.         curr = prev
  64.  
  65.     moves.reverse()
  66.     return moves
  67.  
  68. def solveAces( ):
  69.     current = "AAAA" + "x" * 48
  70.     #goal = "xxxxA" * 4 + "x" * 32
  71.     goal = "xxxxA" * 1 + "xxxxx" + "xxxxA" * 3 + "x" * 27
  72.     visit = search( current, goal )
  73.     moves = path( goal, visit )
  74.     for ( s, d ) in moves:
  75.         print s, d
  76.     print len( moves ) - 1, "moves"
  77.     print len( visit ), "nodes generated"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement