File size: 2,396 Bytes
2311079
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import io
import os

from path import csv_file_path

os.environ['DYLD_LIBRARY_PATH'] = '/opt/homebrew/Cellar/cairo/1.18.4/lib:' + os.environ.get('DYLD_LIBRARY_PATH', '')
import tkinter as tk
from tkinter import ttk

import cairosvg
import chess
import chess.svg
from PIL import Image, ImageTk

engine_path = "/opt/homebrew/Cellar/stockfish/17/bin/stockfish"


class TacticsApp:
    def __init__(self, root, file_path):
        self.root = root
        self.root.title("Tactics Viewer")
        self.file_path = file_path
        self.current_index = 0
        self.problems = self.load_problems()

        self.board_label = ttk.Label(root)
        self.board_label.pack()

        self.info_label = ttk.Label(root, text="", wraplength=400)
        self.info_label.pack()

        self.prev_button = ttk.Button(root, text="Previous", command=self.prev_problem)
        self.prev_button.pack(side=tk.LEFT)

        self.next_button = ttk.Button(root, text="Next", command=self.next_problem)
        self.next_button.pack(side=tk.RIGHT)

        self.display_problem(self.current_index)

    def load_problems(self):
        problems = []
        with open(self.file_path, newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                problems.append(row)
        return problems

    def display_problem(self, index):
        problem = self.problems[index]
        fen = problem['FEN']
        board = chess.Board(fen)

        # Convert the board to an image
        svg_data = chess.svg.board(board=board)
        png_data = cairosvg.svg2png(bytestring=svg_data)
        image = Image.open(io.BytesIO(png_data))
        photo = ImageTk.PhotoImage(image)

        self.board_label.config(image=photo)
        self.board_label.image = photo

        info_text = f"Puzzle ID: {problem['PuzzleId']}\nRating: {problem['Rating']}\nThemes: {problem['Themes']}\nGame URL: {problem['GameUrl']}"
        self.info_label.config(text=info_text)

    def prev_problem(self):
        if self.current_index > 0:
            self.current_index -= 1
            self.display_problem(self.current_index)

    def next_problem(self):
        if self.current_index < len(self.problems) - 1:
            self.current_index += 1
            self.display_problem(self.current_index)

root = tk.Tk()
app = TacticsApp(root, csv_file_path)
root.mainloop()