자유 게시판

OpenCV 지뢰찾기.py

2022.03.16 13:05

bangul 조회:597 추천:3

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

 

감사합니다.

 

지뢰찾기..png

 

 

 

 

번호 제목 글쓴이 조회 등록일
[공지] 자유 게시판 이용간 유의사항 (정치, 종교, 시사 게시물 자제) [1] gooddew - -
45262 슈팅게임 소개 AngerForce Reloaded 번개 412 02-05
45261 시작메뉴 어떤걸 사용해나요.? [2] ᴾᴸᵃʸ 412 03-17
45260 앤드류 버드 - Sisyphus(삼성 광고음악) [4] 와로 412 04-05
45259 여윽시 모자 사부님의 추천수 ㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷㄷ... [1] 입문자 412 05-20
45258 그래 나 꼰대 일지는 몰라도.. [2] 테츠로 412 08-27
45257 티스토리 초대장좀 부탁드립니다. ^^ [13] 바이티 413 08-04
45256 대 한 민 국~~!!! 텐션 좋구요~ 복태취업했 413 09-01
45255 시소놀이가 이쯤은 되어야지 한결맘 413 05-08
45254 follow the party [15] 반속성 413 05-23
45253 dvdfab 프로그램 구할수 있나요 [2] 모던토킹 413 05-24
45252 오프라인 다운로드를 지원하는 쉰레이 클라우드 서비스 시작 [2] 자위의여신 413 07-21
45251 연애 초반 후반 곤재건 413 08-31
45250 Firefox 세븐좋아 413 08-18
45249 불여우 사용 하시는 분 한번 봐 주셔요 버프 413 09-03
45248 인터넷 장애 [5] parannara 413 10-25
45247 급하게 irst 드라이버 빠져있는 PE를 구해봅니다. [3] heykevin 413 12-15
45246 역시 사진을 잘찍기 위해서는.. 도뽱 413 05-11
45245 폴더 보호 [1] 슈머슈마 413 03-07
45244 공룡 사진 부탁드립니다... [2] 쌘돌 413 03-27
45243 데스스트랜딩, 더라스트오브어스2, 가디언스오브갤럭시2, ... [1] 회탈리카 414 12-04
XE1.11.6 Layout1.4.8