Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############################################
- # Guess Who? #
- # No Copyright by Yserbius #
- # a work that I hope my boss doesn't see #
- # #
- ##############################################
- import random
- #number of characteristics
- char_num = 13
- #number of cards (people)
- card_num = 20
- #minimal number of characteristics on a card
- floor = 2
- #maximum characteristics on a card
- ceiling = 3
- #number of characteristics to distribute
- use = 5
- #a list of lists. Each item represents a card. The items in the card are the characteristics
- card_char = []
- #build deck
- for i in range(card_num):
- card_char.append([]) #initialize the cards to zero
- #populate the deck
- for i in range(char_num): #for each characteristic
- for j in range(use): #max number of times a characteristic can be used
- card_full = False
- times_around = 0
- #cycle till we hit a card that still has room for extra characteristics
- #and this one has not yet been used
- while (not card_full) and (times_around < card_num):
- card = random.randrange(card_num)
- if (len(card_char[card]) >= ceiling) or (i in card_char[card]):
- card_full = False
- times_around = times_around +1
- else:
- card_char[card].append(i)
- card_full = True
- #start game
- mycards = card_char
- #get the card to guess
- opponent_card = card_char[random.randrange(card_num)]
- #don't tell the computer!
- print("opponents card is:")
- print(opponent_card)
- #a list of characteristics to guess
- guesses = (range(char_num))
- #how many turns were taken
- turns = 0
- #keep looping till we eliminate all cards
- while len(mycards) > 1:
- turns = turns +1 #increment turn
- estimate = [] #this will be a complete list of all characteristics remaining
- for i in mycards:
- for j in i:
- estimate.append(j) #populate with all the chrtstc of all the cards
- highest = random.choice(estimate) #start from a random chrrctst
- highest_num = 0
- #find the characteristic that is in most cards
- for x in guesses:
- current = estimate.count(x)
- if current > highest_num:
- highest_num = current
- highest = x
- #use it as a guess
- guess = highest
- guesses.remove(guess)
- #does he have brown eyes?
- if guess in opponent_card:
- right = True
- else:
- right = False
- size = len(mycards)
- to_remove = []
- #loop through the cards and remove those that don't fit the criteria
- for card in range(size):
- cur_card = mycards[card]
- if right:
- if not(guess in cur_card):
- to_remove.append(cur_card)
- if not right:
- if guess in cur_card:
- to_remove.append(cur_card)
- for card in to_remove:
- mycards.remove(card)
- #print out the result
- print 'I guessed:'
- print mycards
- print 'in ', turns,' turns'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement