implement castling

This commit is contained in:
Thomas Lindner 2022-12-30 17:32:55 +01:00
parent 1a6f85465a
commit bd334c9abe

View file

@ -72,18 +72,104 @@ class TakeState(State):
class TakeTakeState(State):
def __init__(
self, blunder_evaluator: BlunderEvaluator, from_field: str, to_field: str
self, blunder_evaluator: BlunderEvaluator, from_field: str, from2_field: str
):
super().__init__(blunder_evaluator)
self.from_field = from_field
self.to_field = to_field
self.from2_field = from2_field
def put(self, row: int, column: int) -> State:
field = coords_to_field(row, column)
if self.to_field == field:
move = self.from_field + field
elif self.from_field == field:
move = self.to_field + field
to_field = coords_to_field(row, column)
if self.from2_field == to_field:
move = self.from_field + to_field
elif self.from_field == to_field:
move = self.from2_field + to_field
elif (
self.from_field[1] == self.from2_field[1]
and self.from_field[1] == to_field[1]
):
# king-side castling
if (
self.from_field in ["e1", "e8"]
and self.from2_field in ["h1", "h8"]
and to_field in ["f1", "f8", "g1", "g8"]
):
return KingCastleState(
self.blunder_evaluator, self.from_field, self.from2_field, to_field
)
# queen-side castling
if (
self.from_field in ["e1", "e8"]
and self.from2_field in ["a1", "a8"]
and to_field in ["c1", "c8", "d1", "d8"]
):
return QueenCastleState(
self.blunder_evaluator, self.from_field, self.from2_field, to_field
)
print("ignored invalid put")
return self
else:
print("ignored invalid put")
return self
print("move %s" % move)
self.blunder_evaluator.move(move)
return InitState(self.blunder_evaluator)
class KingCastleState(State):
def __init__(
self,
blunder_evaluator: BlunderEvaluator,
from_field: str,
from2_field: str,
to_field: str,
):
super().__init__(blunder_evaluator)
self.from_field = from_field
self.from2_field = from2_field
self.to_field = to_field
def put(self, row: int, column: int) -> State:
to2_field = coords_to_field(row, column)
if self.to_field[1] == to2_field[1]:
if to2_field in ["f1", "g1"]:
move = "e1g1"
elif to2_field in ["f8", "g8"]:
move = "e8g8"
else:
print("ignored invalid put")
return self
else:
print("ignored invalid put")
return self
print("move %s" % move)
self.blunder_evaluator.move(move)
return InitState(self.blunder_evaluator)
class QueenCastleState(State):
def __init__(
self,
blunder_evaluator: BlunderEvaluator,
from_field: str,
from2_field: str,
to_field: str,
):
super().__init__(blunder_evaluator)
self.from_field = from_field
self.from2_field = from2_field
self.to_field = to_field
def put(self, row: int, column: int) -> State:
to2_field = coords_to_field(row, column)
if self.to_field[1] == to2_field[1]:
if to2_field in ["c1", "d1"]:
move = "e1c1"
elif to2_field in ["c8", "d8"]:
move = "e8c8"
else:
print("ignored invalid put")
return self
else:
print("ignored invalid put")
return self