-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
920a12c
commit a78d15f
Showing
8 changed files
with
101 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import logging | ||
|
||
from modules.bcolors.bcolors import bcolors | ||
from modules.exporters.pgn_exporter import PgnExporter | ||
|
||
|
||
def post_puzzle(puzzle): | ||
def post_puzzle(puzzle, include_blunder=True): | ||
logging.debug(bcolors.WARNING + "NEW PUZZLE GENERATED" + bcolors.ENDC) | ||
logging.info(bcolors.OKBLUE + str(puzzle.to_pgn()) + bcolors.ENDC) | ||
return str(puzzle.to_pgn()) | ||
|
||
result = PgnExporter.export(puzzle, include_blunder) | ||
logging.info(bcolors.OKBLUE + result + bcolors.ENDC) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class BaseExporter: | ||
@staticmethod | ||
def export(puzzle, include_blunder=True): | ||
""" | ||
The method responsible for exporting Puzzle object into desired form. | ||
:return: string representation of Puzzle object | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import chess | ||
from chess import Move | ||
from chess.pgn import Game | ||
|
||
from modules.exporters import BaseExporter | ||
|
||
|
||
class PgnExporter(BaseExporter): | ||
@staticmethod | ||
def determine_result_tag(board): | ||
# In the generated tactics puzzle, the first to move is the one who lost | ||
return '0-1' if board.turn else '1-0' | ||
|
||
@staticmethod | ||
def export(puzzle, include_first_move=True): | ||
fen = puzzle.last_pos.fen() | ||
board = chess.Board(fen) | ||
game = Game().from_board(board) | ||
|
||
result = PgnExporter.determine_result_tag(board) | ||
moves = puzzle.positions.move_list() | ||
|
||
if include_first_move: | ||
first_move = puzzle.last_move | ||
else: | ||
# simulate the move (blunder) | ||
board.push(puzzle.last_move) | ||
board.clear_stack() | ||
# take resulting board and create new game | ||
game = Game().from_board(board) | ||
|
||
first_move = Move.from_uci(moves.pop(0)) | ||
|
||
# start the line | ||
node = game.add_main_variation(first_move) | ||
|
||
# add the rest of the moves | ||
for m in moves: | ||
node = node.add_variation(Move.from_uci(m)) | ||
|
||
# copy headers from the original game and override result tag | ||
for h in puzzle.game.headers: | ||
game.headers[h] = puzzle.game.headers[h] | ||
game.headers['Result'] = result | ||
return str(game) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters