자유 게시판

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 - -
4799 Lee 릿나워 [6] 이젠 606 05-12
4798 종이가구 책꽂이장(?)을 마련해봤습니다. [6] 뽀로로 606 10-13
4797 은둔형 외톨이 [1] 스마트(SMART 606 01-29
4796 아.....큰일났습니다...ㅠㅠ [2] 크루또이 606 02-20
4795 바탕화면에서 콘트롤 + 마우스 스크롤 아이콘 크기조절되네요 [3] 1992 606 12-03
4794 기술혁신 스노우 모드 주행 [2] 거미손 606 11-25
4793 2024년도 국가기술자격 검정 시행계획 공고 [1] asklee 605 12-07
4792 나이가 들어서 일까요? [9] 테츠로 605 06-15
4791 도시바 새틀라이트 청소및 서멀 도포하여 보았습니다. [16] 입문자 605 03-13
4790 (종료) [9] 無名 605 09-06
4789 자동차 판매 사이트인데요. 왠지 대포차나, 절도차 판매 같... [7] 나노오백 605 03-08
4788 옴니아에 스타크래프트를 실행해봤습니다. [2] iGreenPC 605 01-06
4787 비대면 진료 괜찮네요ㅎ [2] 승후야호 605 01-15
4786 아침에 인터넷 as신청 했는데 월요일에 온대요 [2] 펭수대스타 605 05-01
4785 오늘 창문... [7] 박삿갓 605 07-23
4784 파일분활 이나 변환 좀 부탁드림니다 [8] 허니 605 10-25
4783 Windows Vista 32비트 ISO 구해봅니다. [5] Paezaker 605 10-04
4782 우리 동서네집 김장 담았네유 ㅋ [28] 선우 605 12-15
4781 저가형 패드 추천 부탁드립니다...(게임용) [2] ♡오르골 605 07-14
4780 11 워크스테이션 찾던분? 옆집놈 605 10-06
XE1.11.6 Layout1.4.8