def generateGoodCombo(seed=None): """ Generates two cards which can be played on top of each other in the hanabi game """ random.seed(seed) fireWork = Card() card = Card() while not (fireWork.getSuit() == card.getSuit() and fireWork.getValue() == card.getValue() - 1): fireWork.setSuit(Suit(random.randint(1, 5))) fireWork.setValue(random.randint(1, 5)) card.setSuit(Suit(random.randint(1, 5))) card.setValue(random.randint(1, 5)) return (Suit.toInt(fireWork.getSuit()), fireWork.getValue(), Suit.toInt(card.getSuit()), card.getValue())
def generateUnplayableCard(seed=None): random.seed(seed) fireWorks = [] for i in range(5): fireWorks.append( random.randint(0, 5) ) # creating random firework values (intentionally normally distributed) card = Card() card.setSuit(Suit(random.randint(1, 5))) forbiddenValue = fireWorks[Suit.toInt(card.getSuit()) - 1] + 1 card.setValue(random.randint(1, 5)) while card.getValue() == forbiddenValue: card.setValue(random.randint(1, 5)) fireWorks.append(Suit.toInt(card.getSuit())) fireWorks.append(card.getValue()) return fireWorks
def generatePlayableCard(seed=None): random.seed(seed) fireWorks = [] for i in range(5): fireWorks.append( random.randint(0, 5) ) # creating random firework values (intentionally normally distributed) card = Card() card.setSuit(Suit(random.randint(1, 5))) card.setValue(fireWorks[Suit.toInt(card.getSuit()) - 1] + 1) if card.getValue() == 6: fireWorks[Suit.toInt(card.getSuit()) - 1] = random.randint(1, 4) card.setValue(fireWorks[Suit.toInt(card.getSuit()) - 1] + 1) fireWorks.append(Suit.toInt(card.getSuit())) fireWorks.append(card.getValue()) return fireWorks
def generateIndispensableCard(seed=None): if seed is not None: random.seed(seed) card = Card() card.setSuit(Suit(random.randint(1, 5))) card.setValue(random.randint(1, 5)) graveyard = [random.randint(0, 3) if i % 5 == 0 else (random.randint(0, 1) if i % 5 == 4 else random.randint(0, 2)) for i in range(25)] # 0-3 discarded if a 1 0-1 discarded if a 5 else 0-2 discarded graveyard[(Suit.toInt(card.getSuit()) - 1) * 5] = random.randint(0, 2) for i in range((Suit.toInt(card.getSuit()) - 1) * 5 + 1, int(card) - 1): graveyard[i] = random.randint(0, 1) graveyard[int(card) - 1] = 0 if card.getValue() == 5 else 2 if card.getValue() == 1 else 1 # 5 can't be discarded must be the last 1 must be the last of its kind graveyard.append(Suit.toInt(card.getSuit())) graveyard.append(card.getValue()) return graveyard
def generateBadSample(): inputs = [] inputs.append("white") inputs.append("") inputs.append("cardcol") inputs.append("cardvalue") inputs[0] = Suit.randomColor() inputs[1] = random.randint(1, 5) c = Card(suit=Suit.randomColor(), value=random.randint(1, 5)) inputs[2] = Suit.toInt(c.getSuit()) inputs[3] = c.getValue() while ((inputs[0] == c.getSuit()) and (inputs[1] == (c.getValue() - 1))): inputs[0] = Suit.randomColor() inputs[1] = random.randint(1, 5) c = Card(suit=Suit.randomColor(), value=random.randint(1, 5)) inputs[2] = Suit.toInt(c.getSuit()) inputs[3] = c.getValue() # print("bad: ", inputs, "card: ", c.getSuit(), c .getValue()) return [Suit.toInt(inputs[0]), inputs[1], inputs[2], inputs[3]]
def train(self, player, table): for card in player.hand: inputs = [] for suit, value in table.field.items(): inputs.append(value) inputs.append(Suit.toInt(card.getSuit())) inputs.append(card.getValue()) self.compute(inputs) expectedValue = 0 if table.cardPlayable(card): expectedValue = 1 self.backprop([expectedValue])
def generateDispensableCard(seed=None): if seed is not None: random.seed(seed) card = Card() card.setSuit(Suit(random.randint(1, 5))) card.setValue(random.randint(1, 4)) # 5s are indispensable graveyard = [random.randint(0, 3) if i % 5 == 0 else (random.randint(0, 1) if i % 5 == 4 else random.randint(0, 2)) for i in range(25)] # 0-3 discarded if a 1 0-1 discarded if a 5 else 0-2 discarded graveyard[int(card) - 1] = 0 if card.getValue() > 1 else random.randint(0, 1) graveyard.append(Suit.toInt(card.getSuit())) graveyard.append(card.getValue()) return graveyard
def testOnGame(self, player, table): for card in player.hand: inputs = [] for suit, value in table.field.items(): inputs.append(value) inputs.append(Suit.toInt(card.getSuit())) inputs.append(card.getValue()) self.compute(inputs) expectedValue = 0 if table.cardPlayable(card): expectedValue = 1 output = self.getOutput()[0] if output > 0.5: output = 1 else: output = 0 self.learnError.append(abs(expectedValue - output))
def generateIndispensableCard(seed=None): if seed is not None: random.seed(seed) card = Card() card.setSuit(Suit(random.randint(1, 5))) card.setValue(random.randint(1, 5)) graveyard = [random.randint(0, 1) for i in range(25) ] # 0 if some corresponding cards are left else 1 for i in range( (Suit.toInt(card.getSuit()) - 1) * 5, int(card) - 1 ): # none of the cards with same suit and lower value as the current card should have all of their identical cards discarded graveyard[i] = 0 graveyard[ int(card) - 1] = 1 # This one means that all the cards identical to the current card have been discarded but not the current card, which is necessary but not in keeping with the other slots graveyard += card.toBinary() return graveyard