diff --git a/saper.py b/saper.py new file mode 100644 index 0000000..f369c65 --- /dev/null +++ b/saper.py @@ -0,0 +1,92 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +from random import sample + +import random + + +class AppPython(object): + + def __init__(self): + + self.n = 8 + self.window = Gtk.Window() + self.window.set_title("Saper Python") + self.window.set_default_size(50*self.n, 50*self.n) + self.window.connect("delete-event", Gtk.main_quit) + self.znaki_tab = {9: 'M', + 0: '0', + 1: '1', + 2: '2', + 3: '3', + 4: '4', + 5: '5', + 6: '6', + 7: '7', + 8: '8'} + + grid = Gtk.Grid() + self.pole = [] + self.plansza = [] + self.ruchy_gracza = 0 + + for i in range(self.n): + self.pole.append([]) + for j in range(self.n): + p = Gtk.Button.new_with_label(" ") + self.pole[i].append(p) + grid.attach(p, i, j, 1, 1) + p.connect("clicked", self.odkrywanie_pola, i, j) + + for i in range(self.n + 1): + self.plansza.append([]) + for j in range(self.n + 1): + self.plansza[i].append(0) + + self.rozstawianie_min() + self.window.add(grid) + grid.set_column_homogeneous(1) + grid.set_row_homogeneous(1) + self.window.show_all() + + def odkrywanie_pola(self, btn, x, y): + + self.pole[x][y].get_child().set_markup(self.znaki_tab[self.plansza[x][y]]) + self.pole[x][y].set_sensitive(0) + + self.ruchy_gracza += 1 + + if (self.plansza[x][y] == 9) | (self.ruchy_gracza == ((self.n ** 2) - self.n)): + self.koniec_gry() + + def koniec_gry(self): + + for i in range(self.n): + for j in range(self.n): + self.pole[i][j].get_child().set_markup(self.znaki_tab[self.plansza[i][j]]) + self.pole[i][j].set_sensitive(0) + + def rozstawianie_min(self): + + wsp = ([]) + for i in range(self.n): + for j in range(self.n): + wsp.append([i, j]) + + wsp_min = sample(wsp, self.n) + for i in wsp_min: + self.plansza[i[0]][i[1]] = 9 + + for i in range(self.n): + for j in range(self.n): + if self.plansza[i][j] == 9: + for k in range(3): + for l in range(3): + if (self.plansza[i - 1 + k][j - 1 + l] != 9): + self.plansza[i - 1 + k][j - 1 + l] += 1 + + +if __name__ == "__main__": + a = AppPython() + Gtk.main() \ No newline at end of file