almost fully working
This commit is contained in:
parent
6fb8dbeaf6
commit
0c59a36efb
1 changed files with 104 additions and 58 deletions
162
player.py
162
player.py
|
@ -33,13 +33,13 @@ def has_neighbors(p, q, board, pp, pq):
|
|||
return False
|
||||
|
||||
|
||||
def cn(spots, s, visited):
|
||||
def connection_crawler(spots, s, visited):
|
||||
n = 0
|
||||
for i in spots[s][0]:
|
||||
if i not in visited and spots[i][1] == False:
|
||||
if i not in visited and spots[i][1]:
|
||||
visited.add(i)
|
||||
n += len(spots[i][2])
|
||||
n += cn(spots, i, visited)
|
||||
n += 1
|
||||
n += connection_crawler(spots, i, visited)
|
||||
return n
|
||||
|
||||
class Piece:
|
||||
|
@ -121,14 +121,14 @@ class Bee(Piece):
|
|||
def get_valid_jumps(self, spots, s) -> list:
|
||||
result = []
|
||||
for i in spots[s][0]:
|
||||
if spots[i][1]:
|
||||
if spots[i][1] is None:
|
||||
hive = False
|
||||
empty = False
|
||||
for j in spots[i][0]:
|
||||
if hive and empty:
|
||||
break
|
||||
if j in spots[s][0]:
|
||||
if spots[j][1]:
|
||||
if spots[j][1] is None:
|
||||
empty = True
|
||||
else:
|
||||
hive = True
|
||||
|
@ -138,15 +138,20 @@ class Bee(Piece):
|
|||
|
||||
|
||||
class Beetle(Piece):
|
||||
def __init__(self, p, q, team, stack=""):
|
||||
super().__init__(p, q, team)
|
||||
self.stack = stack
|
||||
|
||||
def get_valid_jumps(self, spots, s):
|
||||
if len(spots[s][2]) > 1:
|
||||
if len(spots[s][1].stack) > 0:
|
||||
return spots[s][0]
|
||||
result = []
|
||||
for i in spots[s][0]:
|
||||
if spots[i][1]:
|
||||
if spots[i][1] is None:
|
||||
cs = copy.deepcopy(spots)
|
||||
cs[s][1] = None
|
||||
for j in spots[i][0]:
|
||||
if (j[0] != s[0] or j[1] != s[1]) and spots[j][1] == False:
|
||||
if j in spots.keys() and cs[j][1]:
|
||||
result.append(i)
|
||||
break
|
||||
else:
|
||||
|
@ -167,17 +172,17 @@ class Spider(Piece):
|
|||
for i in spots[s][0]:
|
||||
hive = False
|
||||
empty = False
|
||||
if i in visited and spots[i][1]:
|
||||
if i not in visited and spots[i][1] is None:
|
||||
for j in spots[i][0]:
|
||||
if empty and hive:
|
||||
break
|
||||
if j in spots[s][0]:
|
||||
if spots[str(j)][1] or j in visited:
|
||||
if spots[j][1] is None or j in visited:
|
||||
empty = True
|
||||
else:
|
||||
hive = True
|
||||
if (empty and hive):
|
||||
result += self.spider(spots, i, n, visited.copy())
|
||||
result += self.crawl(spots, i, n, visited.copy())
|
||||
return result
|
||||
|
||||
|
||||
|
@ -187,13 +192,15 @@ class Grasshopper(Piece):
|
|||
result = []
|
||||
for i in get_neighbors(0, 0):
|
||||
q = (s[0]+i[0], s[1]+i[1])
|
||||
if not spots[q][1]:
|
||||
if q in spots.keys() and spots[q][1]:
|
||||
while True:
|
||||
q = (q[0]+i[0], q[1]+i[1])
|
||||
if q in spots.keys() and spots[q][1]:
|
||||
result.append(q)
|
||||
if q in spots.keys() and spots[q][1] is None:
|
||||
nei = [1 for n in spots[q][0] if n!=s and spots[n][1]]
|
||||
if sum(nei)>0:
|
||||
result.append(q)
|
||||
break
|
||||
else:
|
||||
if q not in spots.keys():
|
||||
break
|
||||
return result
|
||||
|
||||
|
@ -208,18 +215,18 @@ class Ant(Piece):
|
|||
for i in spots[s][0]:
|
||||
hive = False
|
||||
empty = False
|
||||
if i in visited and spots[i][1]:
|
||||
if i not in visited and spots[i][1] is None:
|
||||
for j in spots[i][0]:
|
||||
if empty and hive:
|
||||
break
|
||||
if j in spots[s][0]:
|
||||
if spots[str(j)][1] or visited.get(str(j)):
|
||||
if spots[j][1] is None or j in visited:
|
||||
empty = True
|
||||
else:
|
||||
hive = True
|
||||
if empty and hive:
|
||||
result += [i]
|
||||
result += self.ant(spots, i, visited)
|
||||
result += self.crawl(spots, i, visited)
|
||||
return result
|
||||
|
||||
|
||||
|
@ -236,20 +243,25 @@ class Player(Base.Board):
|
|||
self.figures_player = list()
|
||||
self.bee = None
|
||||
self.total_piece_count = 18
|
||||
self.all_placed = list()
|
||||
|
||||
@property
|
||||
def all_remaining(self):
|
||||
return self.total_piece_count - sum(self.myPieces.values()+self.rivalPieces.values())
|
||||
return self.total_piece_count - sum(list(self.myPieces.values())+list(self.rivalPieces.values()))
|
||||
|
||||
@property
|
||||
def remaining_pieces(self):
|
||||
return sum(self.myPieces.values())
|
||||
|
||||
def connected(self, graph, cell):
|
||||
for i in graph[str(cell)][0]:
|
||||
if (graph[i][1] == False):
|
||||
break
|
||||
return self.remaining_pieces-1 == cn(graph, i, {str(cell): True, i: True}) + len(graph[i][2])
|
||||
def connections_checker(self, spots, s):
|
||||
for i in spots[s][0]:
|
||||
if spots[i][1] is None:
|
||||
continue
|
||||
visited = set()
|
||||
visited.add(s)
|
||||
visited.add(i)
|
||||
return len(self.all_placed)-2 == connection_crawler(spots, i, visited)
|
||||
return False
|
||||
|
||||
def getAllEmptyCells(self):
|
||||
result = []
|
||||
|
@ -267,7 +279,7 @@ class Player(Base.Board):
|
|||
result.append([p, q])
|
||||
return result
|
||||
|
||||
def translate_board(self, board):
|
||||
def translate_board(self):
|
||||
# mapper dict
|
||||
piece_class_mapping = {
|
||||
"Q": Bee,
|
||||
|
@ -278,75 +290,109 @@ class Player(Base.Board):
|
|||
}
|
||||
|
||||
spots = dict()
|
||||
|
||||
self.all_placed = list()
|
||||
figures_player = list()
|
||||
|
||||
for p, r in self.board.items():
|
||||
for q, a in r.items():
|
||||
surr = [s for s in get_neighbors(
|
||||
p, q) if self.inBoard(s[0], s[1])]
|
||||
spots[(p, q)] = [surr, piece_class_mapping[a[-1].upper()](
|
||||
p, q, self.myColorIsUpper == a[-1].isupper()) if a else None]
|
||||
if (a, self.myColorIsUpper == a.isupper()):
|
||||
|
||||
if a:
|
||||
if len(a) > 1 and a[-1].lower == "b":
|
||||
spots[(p,q)] = [surr, piece_class_mapping["B"](p, q, self.myColorIsUpper == a[-1].isupper(), a[:-1])]
|
||||
else:
|
||||
spots[(p, q)] = [surr, piece_class_mapping[a[-1].upper()](
|
||||
p, q, self.myColorIsUpper == a[-1].isupper())]
|
||||
else:
|
||||
spots[(p,q)] = [surr, None]
|
||||
|
||||
if a and self.myColorIsUpper == a[-1].isupper():
|
||||
figures_player.append((p, q))
|
||||
if "Q" in list(map(lambda x: x.upper(),a)):
|
||||
self.bee = Bee(p, q, True)
|
||||
if a != "":
|
||||
self.all_placed.append(piece_class_mapping[a[-1].upper()](
|
||||
p, q, self.myColorIsUpper == a[-1].isupper()))
|
||||
|
||||
self.spots = spots
|
||||
self.figures_player = figures_player
|
||||
|
||||
def get_movable_pieces(self):
|
||||
# GET Movable,Empty
|
||||
def get_movable_and_empties(self):
|
||||
moveable = {}
|
||||
empty_spots = []
|
||||
for cell in self.figures_player:
|
||||
# Get all empty cells
|
||||
if (self.remaining_pieces != 0):
|
||||
for i in get_neighbors(0, 0):
|
||||
q = (cell[0]+i[0], cell[1]+i[1])
|
||||
if q in self.spots.keys():
|
||||
if self.spots[q][1]:
|
||||
for fig in self.figures_player:
|
||||
# Empty spots
|
||||
if self.remaining_pieces != 0:
|
||||
for i in get_neighbors(fig[0], fig[1]):
|
||||
if i in self.spots.keys():
|
||||
if self.spots[i][1] is None:
|
||||
enemy = False
|
||||
for j in get_neighbors(0, 0):
|
||||
qq = (q[0]+j[0], q[1]+j[1])
|
||||
qqs = str(qq)
|
||||
if (self.spots.get(qqs) != None):
|
||||
if (self.spots[qqs][1] == False):
|
||||
if (self.spots[qqs][2] == False):
|
||||
enemy = True
|
||||
break
|
||||
if (not enemy):
|
||||
empty_spots.append(q)
|
||||
# Get all moves
|
||||
for j in get_neighbors(i[0], i[1]):
|
||||
if j in self.spots.keys():
|
||||
if self.spots[j][1] and self.spots[j][1].team == False:
|
||||
enemy = True
|
||||
break
|
||||
if not enemy:
|
||||
empty_spots.append(i)
|
||||
# actual move calc
|
||||
if self.myMove > 3:
|
||||
if self.connected(self.spots, cell):
|
||||
animal = self.spots[cell][2]
|
||||
arr = animal.get_valid_jumps(self.spots, cell)
|
||||
var = self.connections_checker(self.spots, fig)
|
||||
if var:
|
||||
animal = self.spots[fig][1]
|
||||
arr = animal.get_valid_jumps(self.spots, fig)
|
||||
if (len(arr) > 0):
|
||||
moveable[cell] = arr
|
||||
moveable[fig] = arr
|
||||
return moveable, empty_spots
|
||||
|
||||
def placer(self, empty):
|
||||
animal = random.choice([a for a, v in self.myPieces.items() if v != 0])
|
||||
spot = random.choice(empty)
|
||||
return [animal, None, None, spot[0], spot[1]]
|
||||
|
||||
def mover(self, moveable):
|
||||
animal = random.choice([a for a in moveable.keys()])
|
||||
jump = random.choice(moveable[animal])
|
||||
return self.spots[animal][1].get_piece_info(self.myColorIsUpper) + [jump[0], jump[1]]
|
||||
|
||||
def move(self):
|
||||
if self.myMove > 80:
|
||||
return []
|
||||
|
||||
self.translate_board()
|
||||
|
||||
free = [1 for n in get_neighbors(
|
||||
self.bee.p, self.bee.q) if self.spots[n][1] == None]
|
||||
free = [6]
|
||||
if self.bee:
|
||||
free = [1 for n in get_neighbors(
|
||||
self.bee.p, self.bee.q) if n in self.spots.keys() and self.spots[n][1] == None]
|
||||
if sum(free) <= 0:
|
||||
return []
|
||||
|
||||
if (self.myMove == 0):
|
||||
animal = random.choice(list(self.myPieces.keys()))
|
||||
if (self.getCount() == 0):
|
||||
if (self.all_remaining == 0):
|
||||
return [animal, None, None, 3, 6]
|
||||
else:
|
||||
choice = random.choice(get_neighbors(3, 6))
|
||||
return [animal, None, None, choice[0], choice[1]]
|
||||
|
||||
moveable, empty = self.get_movable_pieces()
|
||||
moveable, empty = self.get_movable_and_empties()
|
||||
if len(moveable) == 0 and len(empty) == 0: # movement impossible
|
||||
return []
|
||||
|
||||
if self.bee == None and (self.myMove == 3 or random.choice([True, False])) and self.myMove<=3:
|
||||
spot = random.choice(empty)
|
||||
return ["Q" if self.myColorIsUpper else 'q', None, None, spot[0], spot[1]]
|
||||
|
||||
if self.myMove <= 3:
|
||||
return self.placer(empty)
|
||||
|
||||
if self.bee:
|
||||
if (random.choice([True, False]) or len(moveable) == 0) and self.remaining_pieces:
|
||||
return self.placer(empty)
|
||||
else:
|
||||
return self.mover(moveable)
|
||||
|
||||
def updatePlayers(move, activePlayer, passivePlayer):
|
||||
"""write move made by activePlayer player
|
||||
this method assumes that all moves are correct, no checking is made
|
||||
|
|
Loading…
Add table
Reference in a new issue