-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
253 lines (230 loc) · 8.33 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
'contains the Game class'
import curses
import math
import time
from badCursesDebugger import buffer
from boardDrawer import drawBoard
from nnTrainer import Trainer
char_map = {
True: "X",
False: "0"
}
render_map = {
True: 1,
False: 2
}
game_movecursor_map = {
"KEY_UP": [-1, 0],
"KEY_DOWN": [1, 0],
"KEY_LEFT": [0, -1],
"KEY_RIGHT": [0, 1],
"w": [-1, 0],
"s": [1, 0],
"a": [0, -1],
"d": [0, 1]
}
def board_val_to_char(num):
'returns the assigned character of a board value e.g. 1 -> X, 2 -> 0'
return char_map[{render_map[k]:k for k in render_map}[num]]
SQUARE_LENGTH = 9
SQUARE_HEIGHT = 5
CURSOR_OFFSET = 2
WIN_POS = [5, 0]
WIN_WAIT = 1
class Game:
'the class for the tic tac toe game'
def __init__(self, options, stdscr=None):
self.ai = None
self.length = options["board length"]
if options["ai"]:
self.generate_trainer(options, self.length, stdscr)
self.training = True
else:
self.training = False
if stdscr is not None:
self.stdscr = stdscr
stdscr.clear()
stdscr.refresh()
self.running = False
self.won = None
if self.length > 4:
self.match_length = math.ceil(self.length*0.75)
else:
self.match_length = 3
self.true_length = self.length*SQUARE_LENGTH+self.length
self.board = [[0 for i in range(self.length)] \
for j in range(self.length)]
self.win = None
self.rendered_board = drawBoard(
board_length=self.length,
square_length=SQUARE_LENGTH,
square_height=SQUARE_HEIGHT
)
self.original_rendered_board = self.rendered_board.copy()
self.turn = options["start first"]
self.past_game_pos = None
self.cursor_pos = [0, 0]
def generate_trainer(self, options, length, stdscr):
'generates player for ai'
self.ai_file_name = options["ai file"]
self.ai = Trainer(
{
"board_length": length,
"select file1": self.ai_file_name,
"select file2": "",
"show training": True,
"start first": options["start first"],
"slowAmt": 0,
"new": False,
"rounds": 1
},
stdscr,
self
)
def update_rendered_board(self):
'updates the rendered board'
for row_no, row in enumerate(self.board):
for index in range(len(self.board[row_no])):
box = row[index]
if box != 0:
box = board_val_to_char(box)
true_pos_y = math.ceil(SQUARE_HEIGHT/2) \
+ (row_no-1) \
* SQUARE_HEIGHT \
+ SQUARE_HEIGHT-1
true_pos_x = math.ceil(SQUARE_LENGTH/2) + ((index) * SQUARE_LENGTH) + index-1
chars = list(self.rendered_board[true_pos_y])
chars[true_pos_x] = box
self.rendered_board[true_pos_y] = "".join(chars)
def render_board(self):
'renders the board'
for i,string in enumerate(self.rendered_board):
self.win.addstr(i,0,string)
self.win.box()
self.win.refresh()
def render_cursor(self):
'renders the cursor'
cursor = curses.newwin(3, 5, (
self.cursor_pos[0]
* (SQUARE_HEIGHT))
+ round(CURSOR_OFFSET/2)
+ WIN_POS[0],
(self.cursor_pos[1]
* (SQUARE_LENGTH+1)
) + CURSOR_OFFSET+WIN_POS[1])
cursor.box()
if self.get_cursor_selected_val() != 0:
cursor.move(1,2)
cursor.addch(board_val_to_char(self.get_cursor_selected_val()))
cursor.refresh()
def render(self):
'combines all the render methods and renders some additional text'
self.win = curses.newwin(
self.length*SQUARE_HEIGHT,
self.true_length,
WIN_POS[0],
WIN_POS[1]
)
self.update_rendered_board()
self.render_board()
self.render_cursor()
self.stdscr.move(WIN_POS[0]-1, round(WIN_POS[1]+self.true_length/3))
self.stdscr.addstr(char_map[self.turn]+"'s turn")
self.stdscr.move(WIN_POS[0]-2, round(WIN_POS[1]+self.true_length/3))
self.stdscr.addstr("match "+str(self.match_length)+" in a row to win")
self.stdscr.move(WIN_POS[0]-3, round(WIN_POS[1]+self.true_length/3))
self.stdscr.addstr(str(self.length)+"x"+str(self.length)+" grid")
def get_cursor_selected_val(self):
'returns the value at the selected position'
return self.board[self.cursor_pos[0]][self.cursor_pos[1]]
def move_cursor(self, directions):
'changes cursor_pos according to the direction'
if (0 <= self.cursor_pos[0] + directions[0] <= self.length-1) \
and (0 <= self.cursor_pos[1] + directions[1] <= self.length-1):
self.cursor_pos[0] += directions[0]
self.cursor_pos[1] += directions[1]
def has_draw(self):
'checks if the game has drawn'
draw = False
for row in self.board:
draw = row.count(0) == 0
if draw is False:
break
return draw
def haswon(self):
'checks if the game has ended and its outcome'
current_turn = not self.turn
if self.has_draw():
self.won = 3
return
for row_no in range(self.length):
check_list = [
self.board[row_no], # horizontally
[self.board[x][row_no] for x in range(self.length)], # vertically
[self.board[x][x] for x in range(self.length)], # diagonally downwards
[self.board[x][self.length-x-1] for x in range(self.length)] # diagonally upwards
]
for compre in check_list:
if compre.count(render_map[current_turn]) == self.match_length:
self.won = current_turn
return
def terminate(self, immediate=False):
'terminates the game object'
self.stdscr.move(round(self.true_length/3), round(self.true_length/3))
if self.won != 3:
self.stdscr.addstr(char_map[self.won]+" has won!")
else:
self.stdscr.addstr("Draw!")
self.stdscr.refresh()
if not immediate:
time.sleep(WIN_WAIT)
self.running = False
def reset(self):
'resets the board and everything for playing again'
if self.ai:
self.ai.save()
self.terminate()
return
self.board = [[0 for i in range(self.length)] for j in range(self.length)]
self.turn = True
self.cursor_pos = [0, 0]
self.won = None
self.rendered_board = self.original_rendered_board.copy()
def place(self):
'attempts to place x or 0 according to the cursor_pos'
if self.get_cursor_selected_val() == 0:
self.board[self.cursor_pos[0]][self.cursor_pos[1]] = render_map[self.turn]
self.turn = not self.turn
self.haswon()
if not self.training:
self.render()
if (self.won is not None) and (not self.training):
self.terminate()
return True
return False
def process_input(self):
'receives input and makes the game respond accordingly'
INPUT = self.stdscr.getkey()
if INPUT in game_movecursor_map:
directions = game_movecursor_map[INPUT]
self.move_cursor(directions)
elif INPUT in (" ", "\n"):
self.place()
elif INPUT == "q":
self.won = not self.turn
self.terminate(True)
def run(self):
'runs the game'
self.running = True
if self.ai is not None:
self.past_game_pos = []
while self.running:
self.render()
if not self.turn:
self.ai.network_play(self.ai.network1, self, self.past_game_pos, 500)
else:
self.process_input()
else:
while self.running:
self.render()
self.process_input()