def roundattempt(self, cards): if len(self.allowedwords) > 200: sample = random.sample(self.allowedwords, 200) else: sample = self.allowedwords # we want short highscore words return max(sample, key=lambda w: (gamecore.wordscore(cards, w), -len(w)))
def attempt(self, player, word): if not self.isround(): raise gamecore.WrongStateError if not word in self.allowedwords: raise gamecore.BadWordError(word) score = gamecore.wordscore(self._curround().cards, word) self._curround().saveattempt(player, word, score)
def roundattempt(self, cards): c1, c2 = heapq.nlargest(2, cards, key=lambda c: c.score) l1, l2 = c1.letter, c2.letter best = (self.wordsbyalphabet[l1] & self.wordsbyalphabet[l2]) if not best: best = self.wordsbyalphabet[l1] if len(best) > 10: best = heapq.nlargest(10, best, key=lambda w: (gamecore.wordscore(cards, w)) -len(w)) else: best = list(best) return random.choice(best)
def roundattempt(self, cards): best, secondbest = -1, -1 secondbestlist = [] bestlist = [] for word in self.allowedwords: score = gamecore.wordscore(cards, word) if score < secondbest: continue elif score == secondbest: secondbestlist.append(word) elif secondbest < score < best: secondbestlist = [word] secondbest = score elif score == best: bestlist.append(word) else: secondbest = best secondbestlist = bestlist best = score bestlist = [word] return random.choice(secondbestlist)
m = { 'male': (MALE_FIRST_NAMES, MALE_SECONDS_NAMES, Vasiliy), 'female': (FEMALE_FIRST_NAMES, FEMALE_SECOND_NAMES, Fekla), 'vasserman': ([u"Анатолий"], [u"Вассерман"], Vasserman), } first, second, cls = m[gender] return random.choice(first) + u" " + random.choice(second), cls def generate_name(gender): first, second = { 'male': (MALE_FIRST_NAMES, MALE_SECONDS_NAMES), 'female': (FEMALE_FIRST_NAMES, FEMALE_SECOND_NAMES), }[gender] return random.choice(first) + u" " + random.choice(second) if __name__ == '__main__': curdir = os.path.dirname(__file__) with open(os.path.join(curdir, 'data', 'nouns.txt')) as inf: NOUNS = frozenset( line.strip().decode('utf-8') for line in inf) decks = gamecore.shuffled_cards() cards = decks[0] for word in (Vasiliy(NOUNS).roundattempt(cards), Fekla(NOUNS).roundattempt(cards)): print generate_name('male') print u' '.join(u'{0}({1})'.format(*c) for c in cards) print word print gamecore.wordscore(cards, word)
def _attempt(self, player, word): if not self.isround(): raise gamecore.WrongStateError score = gamecore.wordscore(self._curcards(), word) self._curround().saveattempt(player, word, score)