-
Notifications
You must be signed in to change notification settings - Fork 1
/
pieces.py
151 lines (114 loc) · 4.4 KB
/
pieces.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import pygame as pg
from move_checker import axial_distance, move_is_not_blocked_or_jump, \
path_exists, is_straight_line
from settings import PIECE_WHITE
class Piece:
def __init__(self, color=PIECE_WHITE):
self.old_pos = None
self.color = color
def update_pos(self, pos):
self.old_pos = pos
def move_is_valid(self, state, old_tile, new_tile):
pass
class Queen(Piece):
def __init__(self, color=PIECE_WHITE):
super().__init__(color)
def draw(self, surface, hex_pos):
image = \
pg.image.load('images/{}.png'.format(type(self).__name__))
(x, y) = hex_pos
pos = (x - 16, y - 14)
surface.blit(image, pos)
def move_is_valid(self, state, old_tile, new_tile):
dist = axial_distance(old_tile.axial_coords,
new_tile.axial_coords)
if dist == 1 and move_is_not_blocked_or_jump(state, old_tile,
new_tile):
return True
else:
return False
class Ant(Piece):
def __init__(self, color=PIECE_WHITE):
super().__init__(color)
def draw(self, surface, hex_pos):
image = \
pg.image.load('images/{}.png'.format(type(self).__name__))
(x, y) = hex_pos
pos = (x - 16, y - 17)
surface.blit(image, pos)
def move_is_valid(self, state, old_tile, new_tile):
if path_exists(state, old_tile, new_tile):
return True
else:
return False
class Spider(Piece):
def __init__(self, color=PIECE_WHITE):
super().__init__(color)
def draw(self, surface, hex_pos):
image = \
pg.image.load('images/{}.png'.format(type(self).__name__))
(x, y) = hex_pos
pos = (x - 16, y - 17)
surface.blit(image, pos)
def move_is_valid(self, state, old_tile, new_tile):
if path_exists(state, old_tile, new_tile, spider=True) \
and move_is_not_blocked_or_jump(state, old_tile, new_tile):
return True
else:
return False
class Beetle(Piece):
def __init__(self, color=PIECE_WHITE):
super().__init__(color)
def draw(self, surface, hex_pos):
image = \
pg.image.load('images/{}.png'.format(type(self).__name__))
(x, y) = hex_pos
pos = (x - 16, y - 16)
surface.blit(image, pos)
def move_is_valid(self, state, old_tile, new_tile):
dist = axial_distance(old_tile.axial_coords,
new_tile.axial_coords)
if dist == 1 and (move_is_not_blocked_or_jump(state, old_tile,
new_tile) or new_tile.has_pieces()
or len(old_tile.pieces) > 1):
# can't slide into a blocked hex but it can go up or down into one
return True
else:
return False
class Grasshopper(Piece):
def __init__(self, color=PIECE_WHITE):
super().__init__(color)
def draw(self, surface, hex_pos):
image = \
pg.image.load('images/{}.png'.format(type(self).__name__))
(x, y) = hex_pos
pos = (x - 12, y - 14)
surface.blit(image, pos)
def move_is_valid(self, state, old_tile, new_tile):
# dist > 1, straight line, must hop over pieces
dist = axial_distance(old_tile.axial_coords,
new_tile.axial_coords)
if dist > 1:
visited = [old_tile]
queue = [old_tile]
while queue and new_tile not in visited:
current_tile = queue.pop(0)
for neighbor_tile in [x for x in
current_tile.adjacent_tiles if x.has_pieces()
and is_straight_line(old_tile.axial_coords,
x.axial_coords)]:
if neighbor_tile not in visited:
visited.append(neighbor_tile)
queue.append(neighbor_tile)
# have to check last tile seperately bc it will never have a piece
for penultimate_tile in [x for x in new_tile.adjacent_tiles
if x.has_pieces()]:
if penultimate_tile in visited \
and is_straight_line(old_tile.axial_coords,
new_tile.axial_coords):
return True
else:
return False