forked from BuddhiGamage/chess_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturn_move.py
More file actions
93 lines (80 loc) · 3.44 KB
/
return_move.py
File metadata and controls
93 lines (80 loc) · 3.44 KB
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
def to_chess_notation(x, y):
return chr(y + ord('a')) + str(8 - x)
def detect_white_king_castling(initial_board, final_board):
"""
Detect if the White King has castled (kingside or queenside).
Args:
initial_board (list of list): The initial 8x8 chessboard matrix.
final_board (list of list): The final 8x8 chessboard matrix.
Returns:
str: The type of castling ("start coordinates, end coordinates, castling_availability) or "King did not castle".
"""
# Check if the King is still in its starting position (e1)
if initial_board[7][4] == 4 and final_board[7][4] != 4:
# King moved, determine where it moved
if final_board[7][6] == 4: # King moved to g1
return (7,4),(7,6),False
elif final_board[7][2] == 4: # King moved to c1
return (7,4),(7,2),False
else:
return None,None,False
return None,None,True
def find_chess_move(initial_board, final_board,castling_availability):
# Convert the board positions into chess notation (0-indexed to chess coordinates)
start_pos = None
end_pos = None
if (castling_availability):
start_pos,end_pos,castling_availability=detect_white_king_castling(initial_board,final_board)
if(start_pos != None):
start_square = to_chess_notation(start_pos[0], start_pos[1])
end_square = to_chess_notation(end_pos[0], end_pos[1])
return start_square+end_square,castling_availability
for i in range(8):
for j in range(8):
if (initial_board[i][j] < 6) or (final_board[i][j] < 6 ):
if initial_board[i][j] != final_board[i][j] :
if final_board[i][j] != -1 and initial_board[i][j]==-1: # Updated position
end_pos = (i, j)
elif final_board[i][j] != -1 and initial_board[i][j]!=-1:
# capture=True
end_pos = (i, j)
if end_pos==None:
return None,castling_availability
for i in range(8):
for j in range(8):
if (initial_board[i][j] < 6 ) or (final_board[i][j] < 6 ):
if initial_board[i][j] != final_board[i][j] :
if final_board[i][j] == -1 and final_board[end_pos[0]][end_pos[1]]==initial_board[i][j]: # Updated position
start_pos = (i, j)
# Convert to chess notation
if start_pos and end_pos:
start_square = to_chess_notation(start_pos[0], start_pos[1])
end_square = to_chess_notation(end_pos[0], end_pos[1])
print(start_square+end_square)
return start_square + end_square,castling_availability
else:
return None,castling_availability
# # Example matrices
# initial_matrix = [
# [9, 7, 8, 10, 11, 8, 7, 9],
# [6, 6, 6, 6, 6, 6, 6, 6],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [0, 0, 0, 0, 0, 0, 0, 0],
# [3, 1, 2, 5, 4, 2, 1, 3]
# ]
# final_matrix = [
# [9, 7, 8, 10, 11, 8, 7, 9],
# [6, 6, 6, 6, 6, 6, 6, 6],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [0, -1, -1, -1, -1, -1, -1, -1],
# [-1, -1, -1, -1, -1, -1, -1, -1],
# [-1, 0, 0, 0, 0, 0, 0, 0],
# [3, 1, 2, 5, 4, 2, 1, 3]
# ]
# # Find the move
# move = find_chess_move(initial_matrix, final_matrix)
# print("The move is:", move)