chess: added knight moves

This commit is contained in:
Jack Halford 2025-04-18 20:17:21 +02:00
parent 8fe4020ffe
commit f807011f79
No known key found for this signature in database
2 changed files with 36 additions and 33 deletions

Binary file not shown.

View file

@ -18,21 +18,19 @@ deck = genanki.Deck(
) )
model= genanki.Model( model= genanki.Model(
614661503, 1120858226,
'Simple Model', 'Chess Model',
fields=[ fields=[
{'name': 'Slug'}, {'name': 'guid'},
{'name': 'Square'},
{'name': 'Piece'},
{'name': 'Question'}, {'name': 'Question'},
{'name': 'Answer'}, {'name': 'Answer'},
{'name': 'Board'}, {'name': 'Board'},
], ],
templates=[ templates=[
{ {
'name': 'Card 1', 'name': 'Base Card with SVG board in answer',
'qfmt': '{{Question}} {{Square}}', 'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}} <div id="answer">{{Board}}</div>', 'afmt': '{{FrontSide}}<hr id="answer">{{Answer}} {{Board}}',
}, },
] ]
) )
@ -41,34 +39,39 @@ class ChessNote(genanki.Note):
@property @property
def guid(self): def guid(self):
# slug-square-piece # slug-square-piece
return genanki.guid_for(self.fields[0], self.fields[1], self.fields[2]) return genanki.guid_for(self.fields[0])
board = chess.BaseBoard() def square_color(sq_name):
board.clear_board()
notes = []
for sq_name in chess.SQUARE_NAMES:
# square colors
sq_id = chess.parse_square(sq_name) sq_id = chess.parse_square(sq_name)
sq_file = chess.square_file(sq_id) sq_file = chess.square_file(sq_id)
sq_rank = chess.square_rank(sq_id) sq_rank = chess.square_rank(sq_id)
sq_color = 'black' if (sq_file+sq_rank) % 2 == 0 else 'white'
board_svg = chess.svg.board( guid = f'{sq_name}-color'
board, question = f'What is the color of square {sq_name}?'
squares=chess.SquareSet([sq_id]), answer = 'black' if (sq_file+sq_rank) % 2 == 0 else 'white'
) board_svg = chess.svg.board(None, squares=sq_id)
note = ChessNote( return ChessNote(model, [guid, question, answer, board_svg])
model=model,
fields=[ def knight_moves(sq_name):
'color', board = chess.Board().empty()
sq_name, sq_id = chess.parse_square(sq_name)
'', board.set_piece_at(sq_id, chess.Piece(chess.KNIGHT, chess.WHITE))
'What is the color of square', attacks = board.attacks(sq_id)
sq_color,
board_svg, guid = f'{sq_name}-knight-moves' #guid
], question = f'Where can the knight move from square {sq_name}?'
) answer = ', '.join(chess.SQUARE_NAMES[sq] for sq in list(attacks))
notes.append(note) board_svg = chess.svg.board(board, fill=dict.fromkeys(attacks, "#cc0000cc"))
# return ChessNote(model, [guid, question, answer, board_svg])
return ChessNote(model, [guid,question,answer,board_svg])
def notes():
# square colors
for sq_name in chess.SQUARE_NAMES:
yield square_color(sq_name)
yield knight_moves(sq_name)
notes = list(notes())
# randomize the order of the notes # randomize the order of the notes
random.shuffle(notes) random.shuffle(notes)
@ -76,4 +79,4 @@ for note in notes:
deck.add_note(note) deck.add_note(note)
genanki.Package(deck).write_to_file('chess.apkg') genanki.Package(deck).write_to_file('chess.apkg')
print('Deck generated: chess.apkg') print(f'Created deck chess.apkg with {len(deck.notes)} notes')