-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
275 lines (176 loc) · 7.36 KB
/
game.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import curses, time, itertools, random
from trie_node import *
from best_word import *
from user_moves import *
CH_P1 = 'X'
CH_P2 = 'O'
X_STEP = 4
Y_STEP = 2
X_OFFSET = 1
Y_OFFSET = 5
tiles_available = tiles_available_at_start
points = [0,0]
bingos = [0,0]
time_taken = [0,0]
moves_made = [0,0]
def initialise():
print("You'll be playing as player 1. Please wait while the bot initializes.")
prefix_trie = node()
insert_words("Collins Scrabble Words (2019).txt", prefix_trie)
tiles = [[],[]]
board = [[" " for x in range(15)] for y in range(15)]
for i in range(2):
s = random.sample(tiles_available , 7)
tiles[i].extend(s)
for char in s:
tiles_available.remove(char)
return [board, prefix_trie, tiles]
def update_tiles(tiles, turn, new_chars, points_made):
for char in new_chars:
tiles[turn].remove(char)
moves_made[turn] = moves_made[turn] + 1
points[turn] = points[turn] + points_made
if(len(new_chars) == 7):
bingos[turn] = bingos[turn] + 1
length = min(len(tiles_available), len(new_chars))
if(length != 0):
s = random.sample(tiles_available, length)
tiles[turn].extend(s)
for char in s:
tiles_available.remove(char)
if(length == 0):
return -1
return 1
def print_board(stdscr, board):
stdscr.addstr(0, 0, 'Scrabble')
stdscr.hline(1, 0, '-', 50)
stdscr.addstr(2, 0, 'Use arrows to move, [ESC] Quit, [a-z] enter at position, [BACKSPACE] to remove character, [ENTER] play move, [SPACE] Skip turn')
curses.use_default_colors()
try:
for j in range(15):
stdscr.addstr(Y_OFFSET-1, X_OFFSET + j*X_STEP, "-----")
for i in range(15):
stdscr.addstr(Y_OFFSET + i*2, X_OFFSET, "| ")
for j in range(15):
stdscr.addstr(Y_OFFSET + 2*i+1, X_OFFSET + j*X_STEP, "-----")
for j in range(15):
if(board[i][j] == " " and position_points[i][j] == "T"):
curses.init_pair(1, curses.COLOR_RED, -1)
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2, position_points[i][j], curses.color_pair(1))
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2 + 1, " | ")
elif(board[i][j] == " " and position_points[i][j] == "t"):
curses.init_pair(2, curses.COLOR_BLUE, -1)
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2, position_points[i][j], curses.color_pair(2))
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2 + 1, " | ")
elif(board[i][j] == " " and position_points[i][j] == "d"):
curses.init_pair(3, curses.COLOR_CYAN, -1)
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2, position_points[i][j], curses.color_pair(3))
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2 + 1, " | ")
elif(board[i][j] == " " and (position_points[i][j] == "D" or position_points[i][j] == "X") ):
curses.init_pair(4, curses.COLOR_MAGENTA, -1)
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2, position_points[i][j], curses.color_pair(4))
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2 + 1, " | ")
else:
stdscr.addstr(Y_OFFSET + Y_STEP*i, X_OFFSET + j*X_STEP + 2, str(board[i][j] + " |"))
except Exception as e:
raise Exception("Please use the terminal in full screen.")
def print_tiles(stdscr, tiles, user):
stdscr.addstr(Y_OFFSET, X_OFFSET + 65, "Your Tiles : ")
stdscr.addstr(Y_OFFSET, X_OFFSET + 80, "[")
for j in range(len(tiles[user])):
stdscr.addstr(Y_OFFSET, X_OFFSET + (j+1)*X_STEP + 80, tiles[user][j])
stdscr.addstr(Y_OFFSET, X_OFFSET + 8*X_STEP + 80, "]")
stdscr.addstr(Y_OFFSET+2, X_OFFSET + 80, " Your Scrore | Computer's Score ")
stdscr.addstr(Y_OFFSET+3, X_OFFSET + 80, " " + str(points[user]) + " ")
stdscr.addstr(Y_OFFSET+3, X_OFFSET + 102," " + str(points[1 - user]) + " ")
def user_move(stdscr, prefix_trie, board, new_char_pos, first_move, user, tiles):
new_char_pos = list(set(new_char_pos))
x = check_word_new_pos(prefix_trie, board, tiles[user], new_char_pos, first_move)
stdscr.addstr(Y_OFFSET + 6, X_OFFSET + 70, " "*55)
if(x[0] == -1):
for i in new_char_pos:
board[i[0]][i[1]] = " "
print_board(stdscr, board)
print_tiles(stdscr, tiles, user)
stdscr.addstr(Y_OFFSET + 6, X_OFFSET + 70, x[1][:55])
return -1
stdscr.addstr(Y_OFFSET + 5, X_OFFSET + 70, "Points added : " + str(x[1]))
update_tiles(tiles, user, x[2], x[1])
return 1
def bot_move(stdscr, prefix_trie, board, first_move, bot, tiles):
x = get_best_word(prefix_trie, board, tiles[bot])
stdscr.addstr(Y_OFFSET + 5, X_OFFSET + 70, " "*50)
if(x[1] == -1):
return -1
stdscr.addstr(Y_OFFSET + 5, X_OFFSET + 70, "Move made by computer : " + str(x[0][0]) + " for " + str(x[1]) + " points")
if(x[2][0] == x[3][0]):
play_word(prefix_trie, board, tiles[bot], x[0][0], x[2], "right", first_move)
else:
play_word(prefix_trie, board, tiles[bot], x[0][0], x[2], "down", first_move)
update_tiles(tiles, bot, x[4], x[1])
print_board(stdscr, board)
print_tiles(stdscr, tiles, 1 - bot)
def main(stdscr):
user = 0
bot = 1
turn = 0
board, prefix_trie, tiles = initialise()
print_board(stdscr, board)
print_tiles(stdscr, tiles, user)
x_pos = 0
y_pos = 0
new_char_pos = []
first_move = 1
while True:
stdscr.move(Y_OFFSET + y_pos * Y_STEP, X_OFFSET + x_pos * X_STEP + 2)
c = stdscr.getch()
if c == curses.KEY_UP:
y_pos = max(0, y_pos - 1)
elif c == curses.KEY_DOWN:
y_pos = min(14, y_pos + 1)
elif c == curses.KEY_LEFT:
x_pos = max(0, x_pos - 1)
elif c == curses.KEY_RIGHT:
x_pos = min(14, x_pos + 1)
elif c == ord(' ') and len(new_char_pos) == 0:
y = bot_move(stdscr, prefix_trie, board, first_move, bot, tiles)
if(y == -1):
break
new_char_pos = []
elif c == 27:
break
elif c == curses.KEY_BACKSPACE and ((y_pos, x_pos, board[y_pos][x_pos]) in new_char_pos):
new_char_pos.remove( (y_pos, x_pos, board[y_pos][x_pos]) )
board[y_pos][x_pos] = " "
y, x = stdscr.getyx()
stdscr.addstr(y,x, " ")
elif c >= ord('a') and c <= ord('z') and board[y_pos][x_pos] == " ":
y, x = stdscr.getyx()
stdscr.addstr(y,x, chr(c))
# stdscr.addstr(Y_OFFSET + 14, X_OFFSET + 93, str(first_move))
# stdscr.addstr(Y_OFFSET + 14, X_OFFSET + 93, str(y_pos))
# stdscr.addstr(Y_OFFSET + 14, X_OFFSET + 96, str(x_pos))
# stdscr.addstr(Y_OFFSET + 14, X_OFFSET + 99, chr(c))
board[y_pos][x_pos] = chr(c)
new_char_pos.append( (y_pos, x_pos, chr(c)) )
elif c == curses.KEY_ENTER or c == 10 or c == 13:
x = user_move(stdscr, prefix_trie, board, new_char_pos, first_move, user, tiles)
if(x == -1):
new_char_pos = []
continue
first_move = 0
if(len(tiles[user]) == 0):
break
y = bot_move(stdscr, prefix_trie, board, first_move, bot, tiles)
if(y == -1):
break
new_char_pos = []
stdscr.erase()
stdscr.addstr(Y_OFFSET + 9, X_OFFSET + 50," User Bot ")
stdscr.addstr(Y_OFFSET + 10, X_OFFSET + 50," Points " + str(points[0]) + " "+ str(points[1]) +" ")
stdscr.addstr(Y_OFFSET + 11, X_OFFSET + 50," Bingos " + str(bingos[0]) + " "+ str(bingos[1]) +" ")
stdscr.addstr(Y_OFFSET + 12, X_OFFSET + 50," Moves " + str(moves_made[0]) + " "+ str(moves_made[1]) +" ")
stdscr.addstr(Y_OFFSET + 20, X_OFFSET + 50," Press any key to exit.")
c = stdscr.getch()
if __name__ == '__main__':
curses.wrapper(main)