diff --git a/README.md b/README.md index a48a230..c497804 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ You are now ready to run the client. ### Client -`launch_player.sh` is a shell script that directly launches the client with a given role (etiher "WHITE" or "BLACK") and +`runmyplayer.sh` is a shell script that directly launches the client with a given role (etiher "WHITE" or "BLACK") and a given ip address. Alternatively, the client can be run using the following arguments: diff --git a/Tablut.py b/Tablut.py index a1a0fe8..38bf2ce 100644 --- a/Tablut.py +++ b/Tablut.py @@ -173,18 +173,7 @@ class Tablut(Game): possible_moves = self.actions(self.board) escapes = self.board.available_escapes() k_pos = self.board.king - - # Check escapes - if len(escapes) > 0: - if self.role == "WHITE": - return self.board.king, escapes[0] - elif self.role == "BLACK": - if len(escapes) == 1: - pm = [move for move in possible_moves if is_between(k_pos, move[1], escapes[0])] - - if len(pm) > 0: - return pm[0] - + if self.role == "BLACK": king_neighb = get_cell_neighbors(k_pos[0], k_pos[1]) black_neighb = [n for n in king_neighb if self.board[n].checker == CheckerType.BLACK] @@ -230,6 +219,17 @@ class Tablut(Game): if len(pm) > 0: return pm[0] + # Check escapes + if len(escapes) > 0: + if self.role == "WHITE": + return self.board.king, escapes[0] + elif self.role == "BLACK": + if len(escapes) == 1: + pm = [move for move in possible_moves if is_between(k_pos, move[1], escapes[0])] + + if len(pm) > 0: + return pm[0] + return None def search_move(self, depth): diff --git a/board.py b/board.py index 2e7e719..1649811 100644 --- a/board.py +++ b/board.py @@ -3,6 +3,7 @@ from enum import Enum import numpy as np from icecream import ic +from numpy import sign # ic.disable() @@ -392,6 +393,14 @@ class Board: :param start_c: starting column :return: True if the move is valid """ + # Forbid diagonal moves + if start_r != destination_r and start_c != destination_c: + return False + + # Moving the checker to the same position it's already in is trivially always possible + if start_r == destination_r and start_c == destination_c: + return True + checker_to_move = self.grid[start_r, start_c].checker dest_cell_checker = self.grid[destination_r, destination_c].checker @@ -467,38 +476,49 @@ class Board: """ r, c = self.king - if 3<=r<=5 and 3<=c<=5: - # Around the center of the board there are definetely no visible escapes + if 3 <= r <= 5 and 3 <= c <= 5: + # Around the center of the board there are definitely no visible escapes return [] escapes = [] + # Explore left - for j in range(c - 1, -1, -1): - if self[r, j].checker != CheckerType.EMPTY: - break - if self[r, j].type == CellType.ESCAPE: - escapes.append((r, j)) + if self.path_between_cells(self.king, (r, 0)): + escapes.append((r, 0)) # Explore right - for j in range(c + 1, 9): - if self[r, j].checker != CheckerType.EMPTY: - break - if self[r, j].type == CellType.ESCAPE: - escapes.append((r, j)) + if self.path_between_cells(self.king, (r, 8)): + escapes.append((r, 8)) # Explore up - for i in range(r - 1, -1, -1): - if self[i, c].checker != CheckerType.EMPTY: - break - if self[i, c].type == CellType.ESCAPE: - escapes.append((i, c)) + if self.path_between_cells(self.king, (0, c)): + escapes.append((0, c)) # Explore down - for i in range(r + 1, 9): - if self[i, c].checker != CheckerType.EMPTY: - break - if self[i, c].type == CellType.ESCAPE: - escapes.append((i, c)) + if self.path_between_cells(self.king, (8, c)): + escapes.append((8, c)) return escapes + def path_between_cells(self, start, end): + sr, sc = start + er, ec = end + + if sr != er and sc != ec: + return False + + if sr == er and sc == ec: + return True + + if sr == er: + direction = -sign(sc - ec) + cells = [self[sr, j] for j in range(sc + direction, ec + direction, direction)] + else: + direction = -sign(sr - er) + cells = [self[i, sc] for i in range(sr + direction, er + direction, direction)] + + for cell in cells: + if cell.checker != CheckerType.EMPTY or cell.type in [CellType.CAMP, CellType.CASTLE]: + return False + return True + def is_between(a, m, b) -> bool: """ diff --git a/launch_player.sh b/runmyplayer.sh similarity index 100% rename from launch_player.sh rename to runmyplayer.sh