자유 게시판

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 - -
43103 갤럭시 22울트라 사전예약 으로 호구 되었는데.. [1] system 1134 03-30
43102 알리익스프레스 12주년 이벤트 크게 하네요 [4] 맹맹이 1029 03-30
43101 해외 직구로 하면 후라이 판이 거의 빈 값이네요. [7] 복두꺼비 1138 03-30
43100 백업 -> 복구 후 진행경과 참고 하세요 [1] 줄넘기 688 03-30
43099 갤S22 울트라 빨리 팔고싶은데 연락이 안오네요. [4] leeeeeeeee 1061 03-30
43098 사랑 그대로의 사랑 · 푸른하늘 눈이 239 03-30
43097 급하게 요청 드립니다. 부팅되는 PE ISO 파일 공유 요청 [4] 줄넘기 632 03-30
43096 이럴때는 어떻게 해야 하나요? [1] 테츠로 432 03-30
43095 조님 PE 구합니다 10용 지룡자 495 03-30
43094 네트워크 프린터 드라이버 설치 [4] 슈머슈마 841 03-30
43093 One UI 4.1 업그레이드 (안드로이드 12) [3] 박삿갓 716 03-30
43092 해외 직구는 처음 ..그것도 중국에서.. [3] 복두꺼비 673 03-29
43091 하아 따분하네요. 도뽱 258 03-29
43090 전기 매트 추천 좀 해주세요.. [1] 테츠로 467 03-29
43089 중국에서 원산지 증명서FTA Certificate 발급하는 비용이 ... [1] 복태취업했 336 03-29
43088 혹시 저 랜섬웨어 걸린건가요? [15] 더불어~~~ 1169 03-29
43087 HDD [2] Killertige 901 03-29
43086 Ransomware/Win.Magniber.R468087 일까? [1] 풍경소리 662 03-29
43085 술을 끊고자 작심 3일 안되길 기원하며. [9] closeup 442 03-29
43084 크롬 북마크 폴더째 통으로 백업하기 [4] 타천사둘리 691 03-29
XE1.11.6 Layout1.4.8