|
6 | 6 | from vision import SquareType
|
7 | 7 |
|
8 | 8 |
|
9 |
| -Square = namedtuple("Square", ["x", "y"]) |
10 |
| -SquarePair = namedtuple("SquarePair", ["start", "end"]) |
11 |
| - |
12 |
| - |
13 | 9 | def print_position(position):
|
14 | 10 | for x in range(8):
|
15 | 11 | for y in range(8):
|
16 | 12 | print(position[x][y], sep="", end="")
|
17 | 13 | print()
|
18 | 14 |
|
19 | 15 |
|
20 |
| -# Convert a move(point pair) to algebraic notation. |
21 |
| -def pair_to_algebraic(pair): |
22 |
| - def square_to_string(square): |
23 |
| - mapping = "abcdefgh" |
24 |
| - return mapping[square.y] + str(8 - square.x) |
25 |
| - |
26 |
| - return square_to_string(pair.start) + square_to_string(pair.end) |
27 |
| - |
28 |
| - |
29 | 16 | # Extracts position from a chess.Board.
|
30 | 17 | def get_position(board):
|
31 | 18 | mapping = {
|
@@ -68,79 +55,89 @@ def type_of(x):
|
68 | 55 | return [[type_of(lst[i * 8 + j]) for j in range(8)] for i in range(8)]
|
69 | 56 |
|
70 | 57 |
|
71 |
| -# Returns a move(point pair) by analysing differences between two positions. |
72 |
| -# A position is an 8x8 array of SquareType. |
73 |
| -def diff_position(old_position, new_position): |
74 |
| - diff = [ |
75 |
| - Square(x, y) |
76 |
| - for x in range(8) |
77 |
| - for y in range(8) |
78 |
| - if old_position[x][y] != new_position[x][y] |
79 |
| - ] |
80 |
| - |
81 |
| - # Number of differences between boards: |
82 |
| - # Castling has 4. |
83 |
| - # Capture en passant has 3. |
84 |
| - # Any other move, promotion, or normal capture has 2. |
85 |
| - if len(diff) == 4: |
86 |
| - # Castling |
87 |
| - king_points = [p for p in diff if p.y == 4] |
88 |
| - rook_points = [p for p in diff if p.y == 0 or p.y == 7] |
89 |
| - if len(king_points) != 1 or len(rook_points) != 1: |
90 |
| - raise IllegalMoveError |
91 |
| - |
92 |
| - p = king_points[0] |
93 |
| - q = rook_points[0] |
94 |
| - if q.y == 0: |
95 |
| - # Castle long |
96 |
| - return SquarePair(p, Square(p.x, 2)) |
97 |
| - elif q.y == 7: |
98 |
| - # Castle short |
99 |
| - return SquarePair(p, Square(p.x, 6)) |
100 |
| - else: |
101 |
| - raise Exception("") |
102 |
| - |
103 |
| - elif len(diff) == 3: |
104 |
| - # Capture en passant |
105 |
| - pawn_points = [ |
106 |
| - p for p in diff if new_position[p.x][p.y] != SquareType.empty |
107 |
| - ] |
108 |
| - if len(pawn_points) != 1: |
109 |
| - raise IllegalMoveError |
110 |
| - |
111 |
| - p = pawn_points[0] |
112 |
| - start_points = [ |
113 |
| - q |
114 |
| - for q in diff |
115 |
| - if q != p and old_position[q.x][q.y] == new_position[p.x][p.y] |
| 58 | +# Returns a chess.Move object by analysing change of positions. |
| 59 | +# [board] should be the board of the [new_position] |
| 60 | +def get_move_from_diff(board, old_position, new_position): |
| 61 | + # A SquarePair denotes the starting and ending point of a move. |
| 62 | + Square = namedtuple("Square", ["x", "y"]) |
| 63 | + SquarePair = namedtuple("SquarePair", ["start", "end"]) |
| 64 | + |
| 65 | + # Convert a move(point pair) to algebraic notation. |
| 66 | + def pair_to_algebraic(pair): |
| 67 | + def square_to_string(square): |
| 68 | + mapping = "abcdefgh" |
| 69 | + return mapping[square.y] + str(8 - square.x) |
| 70 | + |
| 71 | + return square_to_string(pair.start) + square_to_string(pair.end) |
| 72 | + |
| 73 | + # Returns a move(point pair) by analysing differences between two positions. |
| 74 | + def diff_position(old_position, new_position): |
| 75 | + diff = [ |
| 76 | + Square(x, y) |
| 77 | + for x in range(8) |
| 78 | + for y in range(8) |
| 79 | + if old_position[x][y] != new_position[x][y] |
116 | 80 | ]
|
117 |
| - if len(start_points) != 1: |
118 |
| - raise IllegalMoveError |
119 | 81 |
|
120 |
| - return SquarePair(start_points[0], p) |
121 |
| - |
122 |
| - elif len(diff) == 2: |
123 |
| - # Normal move, capture, or promotion |
124 |
| - start_points = [ |
125 |
| - p for p in diff if new_position[p.x][p.y] == SquareType.empty |
126 |
| - ] |
127 |
| - if len(start_points) != 1: |
128 |
| - raise IllegalMoveError |
129 |
| - start = start_points[0] |
| 82 | + # Number of differences between boards: |
| 83 | + # Castling has 4. |
| 84 | + # Capture en passant has 3. |
| 85 | + # Any other move, promotion, or normal capture has 2. |
| 86 | + if len(diff) == 4: |
| 87 | + # Castling |
| 88 | + king_points = [p for p in diff if p.y == 4] |
| 89 | + rook_points = [p for p in diff if p.y == 0 or p.y == 7] |
| 90 | + if len(king_points) != 1 or len(rook_points) != 1: |
| 91 | + raise IllegalMoveError |
| 92 | + |
| 93 | + p = king_points[0] |
| 94 | + q = rook_points[0] |
| 95 | + if q.y == 0: |
| 96 | + # Castle long |
| 97 | + return SquarePair(p, Square(p.x, 2)) |
| 98 | + elif q.y == 7: |
| 99 | + # Castle short |
| 100 | + return SquarePair(p, Square(p.x, 6)) |
| 101 | + else: |
| 102 | + raise Exception("") |
| 103 | + |
| 104 | + elif len(diff) == 3: |
| 105 | + # Capture en passant |
| 106 | + pawn_points = [ |
| 107 | + p for p in diff if new_position[p.x][p.y] != SquareType.empty |
| 108 | + ] |
| 109 | + if len(pawn_points) != 1: |
| 110 | + raise IllegalMoveError |
| 111 | + |
| 112 | + p = pawn_points[0] |
| 113 | + start_points = [ |
| 114 | + q |
| 115 | + for q in diff |
| 116 | + if q != p and old_position[q.x][q.y] == new_position[p.x][p.y] |
| 117 | + ] |
| 118 | + if len(start_points) != 1: |
| 119 | + raise IllegalMoveError |
| 120 | + |
| 121 | + return SquarePair(start_points[0], p) |
| 122 | + |
| 123 | + elif len(diff) == 2: |
| 124 | + # Normal move, capture, or promotion |
| 125 | + start_points = [ |
| 126 | + p for p in diff if new_position[p.x][p.y] == SquareType.empty |
| 127 | + ] |
| 128 | + if len(start_points) != 1: |
| 129 | + raise IllegalMoveError |
| 130 | + start = start_points[0] |
| 131 | + |
| 132 | + end_points = [p for p in diff if p != start] |
| 133 | + if len(end_points) != 1: |
| 134 | + raise IllegalMoveError |
| 135 | + end = end_points[0] |
| 136 | + return SquarePair(start, end) |
130 | 137 |
|
131 |
| - end_points = [p for p in diff if p != start] |
132 |
| - if len(end_points) != 1: |
| 138 | + else: |
133 | 139 | raise IllegalMoveError
|
134 |
| - end = end_points[0] |
135 |
| - return SquarePair(start, end) |
136 |
| - |
137 |
| - else: |
138 |
| - raise IllegalMoveError |
139 |
| - |
140 | 140 |
|
141 |
| -# Returns a chess.Move object by analysing change of positions. |
142 |
| -# [board] should be the board of the [new_position] |
143 |
| -def get_move_from_diff(board, old_position, new_position): |
144 | 141 | square_pair = diff_position(old_position, new_position)
|
145 | 142 | assert square_pair is not None
|
146 | 143 |
|
|
0 commit comments