Prześlij pliki do ''

This commit is contained in:
Kamil Kluczyński 2019-04-30 15:02:10 +00:00
parent 1d4ffe57da
commit 2b25323205

92
saper.py Normal file
View File

@ -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: '<span font_weight="bold" foreground="red"><b>M</b></span>',
0: '<span font_weight="bold" foreground="black"><b>0</b></span>',
1: '<span font_weight="bold" foreground="orange"><b>1</b></span>',
2: '<span font_weight="bold" foreground="orangered"><b>2</b></span>',
3: '<span font_weight="bold" foreground="tomato"><b>3</b></span>',
4: '<span font_weight="bold" foreground="tomato"><b>4</b></span>',
5: '<span font_weight="bold" foreground="tomato"><b>5</b></span>',
6: '<span font_weight="bold" foreground="tomato"><b>6</b></span>',
7: '<span font_weight="bold" foreground="tomato"><b>7</b></span>',
8: '<span font_weight="bold" foreground="tomato"><b>8</b></span>'}
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()