자유 게시판

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 - -
46223 영화 람보르기니 ʕ·ᴥ·ʔ 715 08-19
46222 채석강의 신비 [7] gooddew 903 08-19
46221 세상 쿨한 북유럽 [2] FINAL 746 08-19
46220 로컬 네트워크 파일 공유 - 허망한 결말... [4] 읍민Kaine 807 08-19
46219 건강이 최고네요 [2] 고고주주 576 08-19
46218 GIGABYTE 오래된 메인보드인데...... [4] adv 725 08-19
46217 논란중인 티맵. 프로그래머님들의 견해는.. [18] 뷰리풀투데 1118 08-19
46216 대학생 및 대학원생이라면 필수인 정보 모르면 큰일 [2] dkskzl 652 08-18
46215 오늘따라 컴퓨터 렉이 심하다. 도뽱 434 08-18
46214 미사일 꽂은 곳에 또 꽂았다 - 국산 '장사정포 킬러'의 놀... asklee 669 08-18
46213 [lk99] Iris가 네이쳐에 한마디 했군요. [8] [벗님] 558 08-18
46212 Windows Forum USB 감사합니다... [32] SPiRiTY 878 08-18
46211 이게 키보드 고장인가요 [4] 복구시디 580 08-17
46210 교회재정관리프로그램 - 추가연장접수 2023-08-18 록시2 418 08-17
46209 올바른 고속도로 통행방법 - 지정차로제 [2] asklee 808 08-17
46208 김삿갓 계곡 [8] gooddew 1115 08-16
46207 <요청자료> 상가임대료관리 [7] 록시2 528 08-16
46206 제 주위에도 이런 분이 계시네요.. [11] 테츠로 1391 08-16
46205 이번에 소액 대출 잠시 받았는데.. [4] dkskzl 1069 08-15
46204 유튜브 국뽕티비 진실 [8] FINAL 1428 08-15
XE1.11.6 Layout1.4.8