-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.py
211 lines (174 loc) · 7.11 KB
/
Board.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
import pygame
from Devil import Devil
from pygame.locals import *
from Angel import Angel
class Board:
def __init__(self, margin, sides, angel_trained=False, devil_trained=False):
self.margin = margin
self.sides = sides
self.side_length = self.sides * self.margin
self.angel = Angel(self.sides, angel_trained)
self.devil = Devil(self.sides, devil_trained)
self.winner = None
self.reason = None
self.turn = True
def reset(self):
self.angel.reset()
self.devil.reset()
self.winner = None
def get_reason(self):
return self.reason
def representation(self):
board_rep = []
for i in range(self.sides ** 2):
if i == self.angel.get_position():
board_rep.append(0.5)
elif i in self.devil.get_blocks():
board_rep.append(0.99)
else:
board_rep.append(0.0)
return board_rep
def check_winner(self, current_player):
if current_player == "angel":
if self.angel.get_position() in self.devil.get_blocks():
self.winner = "devil"
self.reason = "move into devil block"
elif self.angel.get_last_move() == 3 and self.angel.get_position() % self.sides == self.sides - 1:
self.winner = "angel"
self.reason = "angel escaped"
elif self.angel.get_last_move() == 1 and self.angel.get_position() % self.sides == 0:
self.winner = "angel"
self.reason = "angel escaped"
elif self.angel.get_position() < 0 or self.angel.get_position() > self.sides ** 2 - 1:
self.winner = "angel"
self.reason = "angel escaped"
if current_player == "devil":
if self.angel.get_position() in self.devil.get_blocks():
self.winner = "angel"
self.reason = "place on angel"
if len(self.devil.get_blocks()) != len(set(self.devil.get_blocks())):
self.winner = "angel"
self.reason = "repeat place"
def angels_turn(self):
self.angel.angel_move(self.representation())
self.check_winner("angel")
def smart_angel(self):
self.angel.smart_angel(self.representation(), self.devil.get_blocks())
self.check_winner("angel")
def devils_turn(self):
self.devil.place_block(self.representation())
self.check_winner("devil")
def devils_turn_random(self):
self.devil.random_place_block(self.angel.position)
def train_angel(self):
self.angel.train(self.winner)
def train_devil(self):
self.devil.train(self.winner)
def get_winner(self):
return self.winner
def get_angel(self):
return self.angel
def get_devil(self):
return self.devil
def rect_equ(self, position):
return (
(position % self.sides) * self.margin, int(position / self.sides) * self.margin, self.margin,
self.margin)
def init_draw(self):
pygame.init()
self.window = pygame.display.set_mode((self.side_length, self.side_length))
self.canvas = self.window.copy()
self.black = (0, 0, 0, 255)
self.white = (255, 255, 255)
self.gray = (220, 220, 220)
self.silver = (192, 192, 192)
self.red = (255, 0, 0)
def display_board(self):
self.window.fill(self.white)
for side in range(0, self.side_length, self.margin):
pygame.draw.lines(self.window, self.black, False, ((side, 0), (side, self.side_length)))
pygame.draw.lines(self.window, self.black, False, ((0, side), (self.side_length, side)))
if self.winner is None:
pygame.draw.rect(self.window, self.silver, self.rect_equ(self.angel.get_position()))
for devil in self.devil.get_blocks():
pygame.draw.rect(self.window, self.red, (self.rect_equ(devil)))
pygame.display.update()
def players_play(self):
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONUP and not self.turn:
pos = pygame.mouse.get_pos()
x_box = int(pos[0] / self.margin)
y_box = int(pos[1] / self.margin)
box_index = x_box + self.sides * y_box
self.devil.god_place(box_index)
self.turn = not self.turn
self.check_winner("devil")
if e.type == pygame.KEYDOWN and self.turn:
if e.key == K_RIGHT:
self.angel.god_move(1)
self.turn = not self.turn
if e.key == K_LEFT:
self.angel.god_move(-1)
self.turn = not self.turn
if e.key == K_UP:
self.angel.god_move(-self.sides)
self.turn = not self.turn
if e.key == K_DOWN:
self.angel.god_move(self.sides)
self.turn = not self.turn
self.check_winner("angel")
if e.type == pygame.QUIT:
pygame.quit()
return True
pygame.display.update()
return False
def debug(self):
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
x_box = int(pos[0] / self.margin)
y_box = int(pos[1] / self.margin)
box_index = x_box + self.sides * y_box
self.devil.god_place(box_index)
if e.type == pygame.KEYDOWN:
if e.key == K_RIGHT:
self.angel.god_move(1)
if e.key == K_LEFT:
self.angel.god_move(-1)
if e.key == K_UP:
self.angel.god_move(-self.sides)
if e.key == K_DOWN:
self.angel.god_move(self.sides)
if e.type == pygame.QUIT:
pygame.quit()
return True
pygame.display.update()
return False
def god_as_devil(self):
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
x_box = int(pos[0] / self.margin)
y_box = int(pos[1] / self.margin)
box_index = x_box + self.sides * y_box
if box_index == self.angel.get_position():
return False
self.devil.god_place(box_index)
return True
return False
def god_as_angel(self):
for e in pygame.event.get():
if e.type == pygame.KEYDOWN and self.turn:
if e.key == K_RIGHT:
self.angel.god_move(1)
return True
if e.key == K_LEFT:
self.angel.god_move(-1)
return True
if e.key == K_UP:
self.angel.god_move(-self.sides)
return True
if e.key == K_DOWN:
self.angel.god_move(self.sides)
return True
return False