Last active
November 20, 2017 22:46
-
-
Save ilkermanap/a621c8f2429bc789fb1adfe978bf75d8 to your computer and use it in GitHub Desktop.
Mayin tarlasi icin giris / a very simple minesweeper game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
class Mayin: | |
def __init__(self, x,y): | |
self.x = x | |
self.y = y | |
class Tarla: | |
def __init__(self, x, y, mayin_sayisi): | |
self.x = x | |
self.y = y | |
self.mayinlar = {} | |
self.mayinla(mayin_sayisi) | |
def mayinKontrol(self, yenimayin): | |
return (yenimayin.x, yenimayin.y) in self.mayinlar.keys() | |
def mayinla(self, sayi): | |
eklenen = 0 | |
while (eklenen != sayi): | |
x = randint(0,self.x-1) | |
y = randint(0,self.y-1) | |
m = Mayin(x,y) | |
if self.mayinKontrol(m) is False: | |
self.mayinlar[(x,y)] = m | |
eklenen += 1 | |
class Oyun(Tarla): | |
#TODO: minesweeper oyunundaki gibi acilan yer icin etrafindaki mayin sayisini verebilen birseyler eklenebilir | |
def __init__(self, x, y, mayin_sayisi): | |
Tarla.__init__(self, x,y, mayin_sayisi) | |
self.oynanan = {} | |
def adim(self, x,y): | |
m = Mayin(x,y) | |
if self.mayinKontrol(m) is True: | |
# mayinKontrol True ise, adimimizi attigimiz yerde mayin vardir | |
return False | |
return True | |
def oyna(self, tursayisi): | |
adim = 0 | |
if tursayisi > (self.x * self.y): | |
tursayisi = (self.x * self.y) | |
while (adim != tursayisi): | |
x = randint(0,self.x-1) | |
y = randint(0,self.y-1) | |
if (x,y) not in self.oynanan.keys(): | |
adim +=1 | |
m = Mayin(x,y) | |
if self.mayinKontrol(m) is True: | |
print "adim %d : %d , %d koordinatinda mayina bastik" % (adim, x,y) | |
# burada oyunu durdurmak mumkun. | |
else: | |
print "adim %d : %d , %d koordinati temiz" % (adim, x,y) | |
self.oynanan[(x,y)] = 0 | |
else: | |
print "adim %d : %d , %d koordinati daha once oynanmis, yeniden deneyecek" % (adim, x,y) | |
if __name__ == "__main__": | |
oyun = Oyun(20,20,100) | |
oyun.oyna(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment