#!/usr/bin/python3
import gi, copy, random
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Pango

PUZZLES = [
    ("530070000600195000098000060800060003400803001700020006060000280000419005000080079",
     "534678912672195348198342567859761423426853791713924856961537284287419635345286179"),
    ("003020600900305001001806400008102900700000008006708200002609500800203009005010300",
     "483921657967345821251876493548132976729564138136798245372689514814253769695417382"),
]

class App(Gtk.Window):
    def __init__(self):
        super().__init__(title='Sudoku')
        self.set_default_size(400, 460)
        self.puzzle_idx = 0
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        vbox.set_margin_top(10); vbox.set_margin_bottom(10)
        vbox.set_margin_start(10); vbox.set_margin_end(10)
        self.add(vbox)
        # grid
        self.entries = [[None]*9 for _ in range(9)]
        grid = Gtk.Grid(column_spacing=2, row_spacing=2)
        for r in range(9):
            for c in range(9):
                e = Gtk.Entry()
                e.set_max_length(1)
                e.set_width_chars(2)
                e.set_alignment(0.5)
                e.override_font(Pango.FontDescription('Monospace Bold 14'))
                grid.attach(e, c + (c//3), r + (r//3), 1, 1)
                self.entries[r][c] = e
        vbox.pack_start(grid, True, True, 0)
        hb = Gtk.Box(spacing=8)
        check_btn = Gtk.Button(label='Check Solution')
        check_btn.connect('clicked', self.check)
        hb.pack_start(check_btn, True, True, 0)
        new_btn = Gtk.Button(label='New Game')
        new_btn.connect('clicked', self.new_game)
        hb.pack_start(new_btn, True, True, 0)
        vbox.pack_start(hb, False, False, 0)
        self.status = Gtk.Label(label='Fill in the grid')
        vbox.pack_start(self.status, False, False, 0)
        self.new_game()
    def new_game(self, *_):
        puzzle, self.solution = PUZZLES[self.puzzle_idx % len(PUZZLES)]
        self.puzzle_idx += 1
        for r in range(9):
            for c in range(9):
                ch = puzzle[r*9+c]
                e = self.entries[r][c]
                if ch != '0':
                    e.set_text(ch); e.set_editable(False)
                    e.override_font(Pango.FontDescription('Monospace Bold 14'))
                else:
                    e.set_text(''); e.set_editable(True)
                    e.override_font(Pango.FontDescription('Monospace 14'))
        self.status.set_text('Fill in the grid')
    def check(self, *_):
        for r in range(9):
            for c in range(9):
                val = self.entries[r][c].get_text().strip()
                if val != self.solution[r*9+c]:
                    self.status.set_markup('<span foreground="red">Not correct yet — keep trying!</span>')
                    return
        self.status.set_markup('<span foreground="green" weight="bold">Congratulations! Solved!</span>')

win = App()
win.connect('destroy', Gtk.main_quit)
win.show_all()
Gtk.main()
