def get(self): letters = self.request.get(u'letters') if len(letters) > 0: game.set(letters) else: game.shake() if self.request.get(u'solve'): answers = game.solve() answer_score = boggle.score(answers) user_words = self.request.get(u'words').split() words = cross_out(answers, user_words) lines_per_col = max(25, (len(answers)+7)/3) else: answers = None answer_score = None lines_per_col = None words = {} template_values = { u'rank': boggle.rank, u'solve': self.request.get(u'solve'), u'letters': string.join(game.letters, u''), u'display_letters': display_letters(game.letters), u'answers': answers, u'answer_score': answer_score, u'words': words, u'word_score': boggle.score(words), u'lines_per_col' : lines_per_col } template = jinja_environment.get_template(u'bawgle-template.html') self.response.out.write(template.render(template_values))
def makeResponseObj(letters, user_words): if len(letters) > 0: game.set(letters) else: game.shake() answers = game.solve() answer_score = boggle.score(answers) words = cross_out(answers, user_words.split()) return { u'answers': answers, u'answer_score': answer_score, u'word_score': boggle.score(words), }
def genetic(popcap, elite, reproduce, mutchance, crosschance, ratiostart=True, ratioletter=True): if ratiostart: population = [ratio.randboard() for _ in range(popcap)] else: population = [uniform.randboard() for _ in range(popcap)] boring = 0 highscore = 0 #while boring < 20: for g in range(1000): print >> sys.stderr, g #population = uniq(population) scores = [(p, boggle.score(p)) for p in population] scores.sort(key=lambda pair: -pair[1]) parents = [a[0] for a in scores[:reproduce]] print scores[0][1] if highscore < scores[0][1]: highscore = scores[0][1] boring = 0 else: boring += 1 nextgen = parents[:elite] random.shuffle(parents) numkids = (popcap - len(nextgen))/len(parents) extrakids = popcap - len(nextgen) - len(parents)*numkids for p1,p2 in zip(parents[::2], parents[1::2]): for _ in range(numkids): nextgen.append(mutate(crossover(combine(p1, p2), crosschance), mutchance, ratioletter=ratioletter)) if extrakids > 0: extrakids -= 1 nextgen.append(mutate(crossover(combine(p1, p2), mutchance), mutchance)) population = nextgen print scores[0][1] printboard(scores[0][0])
def anneal(kmax, maxtemp, ratioBoard=True, ratioLetter=True): """ Main annealing function: kmax: max iterations maxtemp: maximum temperature ratioBoard: whether to start with a board with dictionary probability distribution ratioLetter: whether to select letters with dictionary probability distribution """ if ratioBoard: board = ratio.randboard() else: board = uniform.randboard() score = boggle.score(board) maxboard = board maxscore = score for k in range(kmax): temp = temperature(k / float(kmax), maxtemp) newboard = crossover(board) newscore = boggle.score(newboard) if p(score, newscore, temp) > random.random(): board = newboard score = newscore print 'crossover' if newscore > maxscore: maxscore = newscore maxboard = newboard newboard = mutate(board, ratioLetter=ratioLetter) newscore = boggle.score(newboard) if p(score, newscore, temp) > random.random(): board = newboard score = newscore print 'mutate' if newscore > maxscore: maxscore = newscore maxboard = newboard newboard = rcrossover(board) newscore = boggle.score(newboard) if p(score, newscore, temp) > random.random(): board = newboard score = newscore print 'rcrossover' if newscore > maxscore: maxscore = newscore maxboard = newboard print k, score return maxscore, maxboard
def submit(): game = get_current_game() word = request.form['word'].lower() valid, message = game.check(word) if game.state != Game.ACTIVE: flash('The game has ended.') elif valid: entry = Entry.query.filter_by(game_id=game.id, user_id=g.user.id, word=word).first() if entry: flash('You already submitted "%s."' % word) else: entry = Entry(game, g.user, word, boggle.score(word)) db.session.add(entry) db.session.commit() flash('+%d points for "%s."' % (entry.score, word)) else: flash(message) return redirect(url_for('index'))
def randscore(): return boggle.score(randboard())