자유 게시판

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 - -
46814 혹시 예전 1997년 드라마 볼 수 있는 곳 있을까요? [2] 바트 581 12-30
46813 아래 자료 Mega 드라이브로 링크 변경 [17] 타천사둘리 1356 12-29
46812 인터넷 + TV 가입 문의 점 드립니다. [3] 삼시세끼 611 12-29
46811 마우스 복사 글 복사 붙여넣기 좀 알려 주세요 [4] 달맞이꽃 589 12-28
46810 리눅스 우분투계열 Zorin을... [7] [벗님] 521 12-28
46809 하하 시간 참 빠르네요. [3] 초보87아재 689 12-28
46808 Bluesky Frame Rate Converter 4.6.1에서 AMD 6000/7000 시... [5] 큰돌 623 12-27
46807 중국 장정 3B 로켓 부스터 추락 [1] asklee 1049 12-27
46806 팰컨 9 로켓 1단 부스터 운송 중 파괴 [1] asklee 827 12-27
46805 나쁜 ㄴ ㅓ ㅁ [1] 대충 1631 12-27
46804 이 사이트에 이 자료 믿을만 할까요? [6] 현민지 1556 12-27
46803 이가격이 맞는지요? [7] nissan 1243 12-27
46802 안티 바이러스고 뭐고... [4] [벗님] 839 12-27
46801 이게 무슨 이야기 인지 링크를 봐도 헷갈리네여.. [2] dadf 723 12-27
46800 SSD 드라이브를 파괴하는 요인들 [4] 날개잃은통 1507 12-27
46799 ‘윈도우 10’ 서비스 종료가 막대한 쓰레기를 생성한다? 생... [3] 날개잃은통 1000 12-27
46798 Drive Snapshot 1.5 한글화 자료를 찾고있습니다. [2] 정상고집 414 12-27
46797 여자들이 싫어하는 남자 외모 순위라는데.. [7] 도뽱 1255 12-26
46796 한국천문연구원 주차장에 세겨진 그림 asklee 741 12-26
46795 작업표시줄의 windows보안 아이콘 복원 테스트 [1] 체리마키아 382 12-26
XE1.11.6 Layout1.4.8