-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmove.py
81 lines (64 loc) · 3.28 KB
/
move.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
77
78
79
80
81
from castling import Castling
from check import Check
from color import Color
from piece import to_letter, Piece
class Move:
"""Represents a move on the board"""
# pylint: disable=too-many-instance-attributes
# I may refactor this one day, but all those attributes are needed for a move description
def __init__(self, piece: Piece, i: int, j: int, is_capture: bool, check: Check, promotion: str,
castling: Castling, en_passant: Piece):
self.piece = piece
self.i = i
self.j = j
self.is_capture = is_capture or en_passant.name != ""
self.check = check
self.print_i = False
self.print_j = False
self.promotion = promotion
self.castling = castling
self.en_passant = en_passant
self.an_string = self.__to_an()
def set_print_i(self, print_i: bool) -> None:
"""Set the print i value"""
self.print_i = print_i
self.an_string = self.__to_an()
def set_print_j(self, print_j: bool) -> None:
"""Set the print i value"""
self.print_j = print_j
self.an_string = self.__to_an()
def __to_an(self) -> str:
"""Returns the "an" representation of this move"""
if self.castling == Castling.KING_SIDE:
return "O-O"
if self.castling == Castling.QUEEN_SIDE:
return "O-O-0"
output = self.piece.name if self.piece.name != "P" else ""
output += to_letter(self.piece.i) if self.print_i or (self.piece.name == "P" and self.is_capture) else ""
output += f'{self.piece.j}' if self.print_j else ""
output += "x" if self.is_capture else ""
output += to_letter(self.i)
output += f'{self.j}'
output += f'={self.promotion}' if self.promotion != "" else ""
output += "+" if self.check == Check.CHECK else ""
output += "#" if self.check == Check.CHECKMATE else ""
return output
def build_move(piece: Piece, i: int, j: int, is_capture: bool) -> Move:
"""Creates a Move object with some default parameters"""
return Move(piece, i, j, is_capture, Check.NONE, "", Castling.NONE, Piece(Color.WHITE, "", -1, -1))
def copy_move_edit_check(original: Move, check: Check) -> Move:
"""Copy the given move modifying the check value"""
return Move(original.piece, original.i, original.j, original.is_capture, check, original.promotion,
original.castling, original.en_passant)
def copy_move_edit_promotion(original: Move, promotion: str) -> Move:
"""Copy the given move modifying the promotion value"""
return Move(original.piece, original.i, original.j, original.is_capture, original.check, promotion,
original.castling, original.en_passant)
def copy_move_edit_castling(original: Move, castling: Castling) -> Move:
"""Copy the given move modifying the castling value"""
return Move(original.piece, original.i, original.j, original.is_capture, original.check, original.promotion,
castling, original.en_passant)
def copy_move_edit_en_passant(original: Move, en_passant: Piece) -> Move:
"""Copy the given move modifying the en_passant value"""
return Move(original.piece, original.i, original.j, original.is_capture, original.check, original.promotion,
original.castling, en_passant)