|
@@ -14,6 +14,20 @@ class IllegalMove(Exception):
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
+def board2svg(board, size=256, shallow=False):
|
|
|
|
+ arrows = []
|
|
|
|
+ if board.move_stack and board.move_stack[-1] != chess.Move.null():
|
|
|
|
+ arrows.append((board.move_stack[-1].from_square, board.move_stack[-1].to_square))
|
|
|
|
+
|
|
|
|
+ if not shallow and len(board.move_stack) > 1 and board.move_stack[-2] != chess.Move.null():
|
|
|
|
+ arrows.append((board.move_stack[-2].from_square, board.move_stack[-2].to_square))
|
|
|
|
+
|
|
|
|
+ if board.is_check():
|
|
|
|
+ return chess.svg.board(board, orientation=board.turn, size=size, arrows=arrows, fill={checker: "#cc0000cc" for checker in board.checkers()}, check=board.king(board.turn))
|
|
|
|
+
|
|
|
|
+ return chess.svg.board(board, orientation=board.turn, size=size, arrows=arrows)
|
|
|
|
+
|
|
|
|
+
|
|
class ChessSession:
|
|
class ChessSession:
|
|
def __init__(self, engine):
|
|
def __init__(self, engine):
|
|
self.engine = engine
|
|
self.engine = engine
|
|
@@ -163,17 +177,7 @@ class ChessManager:
|
|
if not session:
|
|
if not session:
|
|
raise KeyError(id)
|
|
raise KeyError(id)
|
|
|
|
|
|
- arrows = []
|
|
|
|
- if session.board.move_stack and session.board.move_stack[-1] != chess.Move.null():
|
|
|
|
- arrows.append((session.board.move_stack[-1].from_square, session.board.move_stack[-1].to_square))
|
|
|
|
-
|
|
|
|
- if not shallow and len(session.board.move_stack) > 1 and session.board.move_stack[-2] != chess.Move.null():
|
|
|
|
- arrows.append((session.board.move_stack[-2].from_square, session.board.move_stack[-2].to_square))
|
|
|
|
-
|
|
|
|
- if session.board.is_check():
|
|
|
|
- return chess.svg.board(session.board, orientation=session.board.turn, size=size, arrows=arrows, fill={checker: "#cc0000cc" for checker in session.board.checkers()}, check=session.board.king(session.board.turn))
|
|
|
|
-
|
|
|
|
- return chess.svg.board(session.board, orientation=session.board.turn, size=size, arrows=arrows)
|
|
|
|
|
|
+ return board2svg(session.board, size=size, shallow=shallow)
|
|
|
|
|
|
def ascii(self, id):
|
|
def ascii(self, id):
|
|
session = self.sessions.get(id)
|
|
session = self.sessions.get(id)
|
|
@@ -215,3 +219,19 @@ class ChessManager:
|
|
|
|
|
|
return session.board.turn
|
|
return session.board.turn
|
|
|
|
|
|
|
|
+ def animate(self, id, count=2, size=256, shallow=False):
|
|
|
|
+ session = self.sessions.get(id)
|
|
|
|
+ if not session:
|
|
|
|
+ raise KeyError(id)
|
|
|
|
+
|
|
|
|
+ board = chess.Board()
|
|
|
|
+ frames = []
|
|
|
|
+
|
|
|
|
+ frames.append(board2svg(board, size=size, shallow=shallow))
|
|
|
|
+
|
|
|
|
+ for move in session.board.move_stack:
|
|
|
|
+ board.push(move)
|
|
|
|
+
|
|
|
|
+ frames.append(board2svg(board, size=size, shallow=shallow))
|
|
|
|
+
|
|
|
|
+ return frames
|