-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
76 lines (65 loc) · 2.25 KB
/
helper.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
from board import Board
import pieces
def get_cell_row_or_col(cell: str):
if cell.isnumeric():
return 8 - int(cell[0]), -1
else:
return -1, ord(cell[0]) - 97
def get_cell_tuple(cell: str):
# a b c d e f g h
# 0 1 2 3 4 5 6 7
return 8 - int(cell[1]), ord(cell[0]) - 97
def check_valid(move):
if move == "resign":
return True
if any(x not in "abcdefgh12345678x+QKNRB" for x in move):
return False
if len(move) > 2 and not move[0].isupper() and 'x' not in move:
return False
return True
def process_move(move, b: Board):
piece = ''
startpos = ''
endpos = ''
if move.startswith("x"):
return None
if 'x' in move:
move = move.replace('x', '')
if len(move) == 3 and move[0] in "abcdefgh":
endpos = get_cell_tuple(move[1:])
piece = "p"
startpos = b.find_piece(piece, endpos)
elif len(move) == 3:
endpos = get_cell_tuple(move[1:])
piece = move[0].lower()
startpos = b.find_piece(piece, endpos)
else:
endpos = get_cell_tuple(move[2:])
piece = move[0].lower()
startpos = b.find_piece(piece, endpos, get_cell_row_or_col(move[1]))
if isinstance(b.board_piece[endpos[0]][endpos[1]], pieces.Blank) and not (isinstance(b.board_piece[startpos[0]][endpos[1]], pieces.Pawn) and b.board_piece[startpos[0]][endpos[1]].en_passant):
startpos = None
else:
if len(move) == 2:
endpos = get_cell_tuple(move)
piece = "p"
startpos = b.find_piece(piece, endpos)
elif len(move) == 3:
endpos = get_cell_tuple(move[1:])
piece = move[0].lower()
startpos = b.find_piece(piece, endpos)
else:
endpos = get_cell_tuple(move[2:])
piece = move[0].lower()
startpos = b.find_piece(piece, endpos, get_cell_row_or_col(move[1]))
if startpos == None:
return ""
return (startpos, endpos)
def rotate_board(b):
return [x[::-1] for x in b[::-1]]
def rotate_moves(moves):
if moves == []:
return []
if moves == None:
return None
return [(7 - x[0], 7 - x[1]) for x in moves]