OpenCV 지뢰찾기.py
2022.03.16 13:05
https://codingdojang.com/scode/718?answer_mode=hide
여기서 보고 배웠습니다.
컴맹에 노안... 등등
글을 올리는 것이 잘 안돼서 윈포에 올립니다.
#OpenCV 지뢰찾기.py
def minemap(m,n):
user=[]
for x in range(m*n):
save=[]
if mine[x]=='*':
user.append(mine[x])
else:
if x > n-1:
save.append(mine[x-n])
if x%n > 0:
save.append(mine[x-n-1])
if x%n < n-1:
save.append(mine[x-n+1])
if x%n > 0:
save.append(mine[x-1])
if x%n < n-1:
save.append(mine[x+1])
if x < (m-1)*n:
save.append(mine[x+n])
if x%n > 0:
save.append(mine[x+n-1])
if x%n < n-1:
save.append(mine[x+n+1])
user.append(save.count('*'))
return user
def judge(q):
aaa = []
bbb = []
aaa.append(q)
while 0 < len(aaa):
bbb.clear()
bbb = aaa.copy()
aaa.clear()
for a in bbb:
if lanb[a] == '?':
pass
else:
if lanb[a] == ' ':
if sheet[a] == 0:
if a < usern:
if a == 0:
aaa.append(a+1)
aaa.append(a+usern)
aaa.append(a+usern+1)
elif a == usern-1:
aaa.append(a-1)
aaa.append(a+usern-1)
aaa.append(a+usern)
else:
aaa.append(a-1)
aaa.append(a+1)
aaa.append(a+usern-1)
aaa.append(a+usern)
aaa.append(a+usern+1)
if usern-1 < a < (userm-1)*usern:
if a%usern == 0:
aaa.append(a-usern)
#aaa.append(a-usern+1)
aaa.append(a+1)
aaa.append(a+usern)
aaa.append(a+usern+1)
elif a%usern == usern-1:
aaa.append(a-usern-1)
aaa.append(a-usern)
aaa.append(a-1)
aaa.append(a+usern-1)
aaa.append(a+usern)
else:
aaa.append(a-usern-1)
aaa.append(a-usern)
aaa.append(a-usern+1)
aaa.append(a-1)
aaa.append(a+1)
aaa.append(a+usern-1)
aaa.append(a+usern)
aaa.append(a+usern+1)
if a > (userm-1)*usern-1:
if a == (userm-1)*usern:
aaa.append(a-usern)
aaa.append(a-usern+1)
aaa.append(a+1)
elif a == userm*usern-1:
aaa.append(a-usern-1)
aaa.append(a-usern)
aaa.append(a-1)
else:
aaa.append(a-usern-1)
aaa.append(a-usern)
aaa.append(a-usern+1)
aaa.append(a-1)
aaa.append(a+1)
lanb[a] = sheet[a]
for a in range(userm*usern):
lanb[a] = str(lanb[a])
def onMouse(event, x, y, flags, param):
global t1, t2
img[45:656, 45:656] = capple
cv2.imshow('minesweeper', img)
b = 0
for m in range(userm):
for n in range(usern):
if lanb[b] == ' ':
pass
if lanb[b] == '?':
cv2.putText(img,(lanb[b]),(56+38*n,75+38*m),font,0.9,(0,0,255),3)
cv2.imshow('minesweeper', img)
if lanb[b] == '0':
cv2.putText(img,(lanb[b]),(56+38*n,75+38*m),font,0.9,(255,255,255),3)
cv2.imshow('minesweeper', img)
if lanb[b] == '1' or lanb[b] == '2' or lanb[b] == '3' or lanb[b] == '4' or lanb[b] == '5' or lanb[b] == '6' or lanb[b] == '7' or lanb[b] == '8':
cv2.putText(img,(lanb[b]),(56+38*n,75+38*m),font,0.9,(255,0,0),3)
cv2.imshow('minesweeper', img)
if lanb[b] == '*':
cv2.putText(img,(lanb[b]),(56+38*n,75+38*m),font,0.9,(0,0,255),3)
cv2.imshow('minesweeper', img)
b += 1
if 46 < x < 654 and 46 < y < 654:
if event == cv2.EVENT_MOUSEMOVE:
for m in range(userm):
for n in range(usern):
if 46+38*n < x < 84+38*n and 46+38*m < y < 84+38*m:
cv2.rectangle(img, (46+38*n, 46+38*m), (84+38*n, 84+38*m), (0, 0, 255), 2)
cv2.imshow('minesweeper', img)
if event == cv2.EVENT_LBUTTONDOWN:
if t1 == 0:
t1 = time.time()
judge(((y-46)//38)*usern+(x-46)//38)
if event == cv2.EVENT_RBUTTONDOWN:
if lanb[((y-46)//38)*usern+(x-46)//38] == ' ':
lanb[((y-46)//38)*usern+(x-46)//38] = '?'
elif lanb[((y-46)//38)*usern+(x-46)//38] == '?':
lanb[((y-46)//38)*usern+(x-46)//38] = ' '
if lanb.count('*') > 0:
if t2 == 0:
t2 = round(time.time()-t1)
cv2.putText(img,('%d seconds')%t2,(670,35),font,1,(0,255,0),2)
cv2.imshow('minesweeper', img)
cv2.putText(img,('gameover'),(670,135),font,1,(0,0,255),2)
cv2.imshow('minesweeper', img)
cv2.waitKey(3000)
cv2.destroyWindow('minesweeper')
if lanb.count(' ')+lanb.count('?') == sheet.count('*'):
if t2 == 0:
t2 = round(time.time()-t1)
cv2.putText(img,('%d seconds')%t2,(670,35),font,1,(0,255,0),2)
cv2.imshow('minesweeper', img)
cv2.putText(img,('perfect!!'),(670,135),font,1,(0,255,0),2)
cv2.imshow('minesweeper', img)
cv2.waitKey(3000)
cv2.destroyWindow('minesweeper')
############################################################
import cv2, numpy, random, time
while True:
img = numpy.full((700, 900, 3), (255, 255, 255), dtype=numpy.uint8)
cv2.imshow('minesweeper', img)
font=cv2.FONT_HERSHEY_SIMPLEX
userm = usern = 16
userl = 40
t1 = t2 = 0
lanb = [' ']*userm*usern
mine = ['*']*userl + ['.']*(userm*usern-userl)
random.shuffle(mine)
sheet = minemap(userm, usern)
if userm%2 == 0:
sen = userm*usern//2-usern//2
if userm%2 == 1:
sen = userm*usern//2
while not sheet[sen] == 0:
random.shuffle(mine)
sheet = minemap(userm, usern)
for m in range(userm):
for n in range(usern):
cv2.rectangle(img, (46+38*n, 46+38*m), (84+38*n, 84+38*m), (0, 255, 0), 1)
cv2.rectangle(img, (59+38*n, 57+38*m), (71+38*n, 73+38*m), (200, 255, 100), 3)
cv2.imshow('minesweeper', img)
cv2.putText(img,('LandMine=%d')%userl,(670,235),font,1,(0,255,0),2)
cv2.imshow('minesweeper', img)
apple = img[45:656, 45:656]
capple = apple.copy()
cv2.setMouseCallback('minesweeper', onMouse)
cv2.waitKey(0)
cv2.destroyAllWindows()
replay=9
while not replay==0 and not replay==1:
replay=int(input('다시하려면 1, 종료하시려면 0을 눌러주세요: '))
if not replay==0 and not replay==1:
print('\n0 또는 1을 입력하셔야합니다.\n')
if replay==0:
exit()
else:
continue
감사합니다.
모듈이 없다고 뜨네용