Advertisement
Guest User

Yserbius

a guest
Jan 21st, 2010
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. ##############################################
  2. #                   Guess Who?               #
  3. # No Copyright by Yserbius                   #
  4. # a work that I hope my boss doesn't see     #
  5. #                                            #
  6. ##############################################
  7.  
  8. import random
  9. #number of characteristics
  10. char_num = 13
  11.  
  12. #number of cards (people)
  13. card_num = 20
  14.  
  15. #minimal number of characteristics on a card
  16. floor = 2
  17.  
  18. #maximum characteristics on a card
  19. ceiling = 3
  20.  
  21. #number of characteristics to distribute
  22. use = 5
  23.  
  24. #a list of lists. Each item represents a card. The items in the card are the characteristics
  25. card_char = []
  26.  
  27. #build deck
  28.  
  29. for i in range(card_num):
  30.     card_char.append([]) #initialize the cards to zero
  31.  
  32. #populate the deck
  33. for i in range(char_num): #for each characteristic
  34.     for j in range(use):  #max number of times a characteristic can be used
  35.         card_full = False
  36.         times_around = 0
  37.         #cycle till we hit a card that still has room for extra characteristics
  38.         #and this one has not yet been used
  39.         while (not card_full) and (times_around < card_num):
  40.             card = random.randrange(card_num)
  41.             if (len(card_char[card]) >= ceiling) or (i in card_char[card]):
  42.                 card_full = False
  43.                 times_around = times_around +1
  44.             else:
  45.                 card_char[card].append(i)
  46.                 card_full = True
  47.        
  48.    
  49.    
  50. #start game
  51. mycards = card_char
  52. #get the card to guess
  53. opponent_card = card_char[random.randrange(card_num)]
  54.  
  55. #don't tell the computer!
  56. print("opponents card is:")
  57. print(opponent_card)
  58.  
  59. #a list of characteristics to guess
  60. guesses = (range(char_num))
  61.  
  62. #how many turns were taken
  63. turns = 0
  64.  
  65. #keep looping till we eliminate all cards
  66. while len(mycards) > 1:
  67.     turns = turns +1 #increment turn
  68.     estimate = []   #this will be a complete list of all characteristics remaining
  69.     for i in mycards:
  70.         for j in i:
  71.             estimate.append(j)  #populate with all the chrtstc of all the cards
  72.     highest = random.choice(estimate) #start from a random chrrctst
  73.     highest_num = 0
  74.     #find the characteristic that is in most cards
  75.     for x in guesses:
  76.         current = estimate.count(x)
  77.         if current > highest_num:
  78.             highest_num = current
  79.             highest = x
  80.     #use it as a guess
  81.     guess = highest
  82.     guesses.remove(guess)
  83.  
  84.     #does he have brown eyes?
  85.     if guess in opponent_card:
  86.         right = True
  87.     else:
  88.         right = False
  89.        
  90.     size = len(mycards)
  91.     to_remove = []
  92.  
  93.     #loop through the cards and remove those that don't fit the criteria
  94.     for card in range(size):
  95.         cur_card = mycards[card]
  96.         if right:
  97.             if not(guess in cur_card):
  98.                 to_remove.append(cur_card)
  99.         if not right:
  100.             if guess in cur_card:
  101.                 to_remove.append(cur_card)
  102.     for card in to_remove:
  103.         mycards.remove(card)
  104.  
  105. #print out the result
  106. print 'I guessed:'
  107. print mycards
  108. print 'in ', turns,' turns'
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement