-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_player.py
77 lines (58 loc) · 2.19 KB
/
example_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
# (c) 2011, Marcus Svensson <[email protected]>
# See gpl-2.0.txt for license
import heapq
import time
from wordfeudplayer.wordlist import Wordlist
from wordfeudplayer.board import Board
def example():
# create a Wordlist and tell it to read one or more wordlists
t = time.time()
w = Wordlist()
print('Reading wordlist...')
dsso_id = w.read_wordlist('ordlista.txt')
print('Done in %2fs' % (time.time()-t))
# create a Board with standard bonus square placement and
# set the current state of the game (where tiles are placed)
b = Board()
state = [' ',
' ',
' d ',
' r ',
' m ärm',
' bang ',
' ex g ',
' tar se ',
' j lät ',
' o ån g ',
' påtat skarv',
' t t i ',
' cidErs ',
' lus ed',
' o nu']
b.set_state(state)
# the letters we have on hand, '*' is a blank tile
letters = '*shetgj'
print('The board is:\n%s' % b)
# calculate all possible words and their scores
print('\nThinking...')
top20 = calc_best_moves(b, w, letters, dsso_id)
(x, y, horizontal, word, score) = top20[0]
print('\nBest word is "%s", playing it' % word)
b.play_word(word, x, y, horizontal)
print('\nBoard is:\n%s' % b)
print("\nOpponent's turn, thinking...")
next_top20 = calc_best_moves(b, w, letters, dsso_id)
def calc_best_moves(b, w, letters, variant):
words = b.calc_all_word_scores(letters, w, variant)
# pick out the 20 words with highest score and print them
top20 = heapq.nlargest(20, words, lambda w: w[4])
if len(top20) == 0:
print('There are no possible words')
return []
print('Score StartX StartY Direction Word (capital letter means "use wildcard")')
for (x, y, horizontal, word, score) in top20:
print('%5d %6d %6s %10s %s' % (score, x, y, 'Horizontal' if horizontal else 'Vertical', word))
return top20
if __name__ == '__main__':
example()