바둑돌 파이썬

 


import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import * 

from PyQt5.Qt import QSize


from PyQt5 import uic





form_class = uic.loadUiType("omok03.ui")[0]



class WindowClass(QMainWindow, form_class) :

    #생성자 

    def __init__(self):

        super().__init__()

        self.setupUi(self)

        #바둑판

        self.icon0 = QIcon('0.png')

        #흰돌

        self.icon1 = QIcon('1.png')

        #검정돌

        self.icon2 = QIcon('2.png')

        #돌이 놓일 배열 생성

        self.arr2dpb = []

        #중복되지 않게 돌을 놓을때 사용할 flag 

        self.flag_wb = True

        

        

        #바둑판 기본 

        self.arr2d = [

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                             

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

                        [0,0,0,0,0, 0,0,0,0,0],

    

                     ]

        #i 는 0부터 9 까지 

        for i in range(10) :

            line = []

            #j는 0부터 9까지 

            for j in range(10) : 

                button = QPushButton("", self)

            #버튼이 놓일 자리 지정     

                button.setGeometry(j * 40, i * 40 , 40, 40)

            # 바둑판 1개한가의 사이즈 지정     

                button.setIconSize(QSize(40, 40))

            #바둑판 1개 넣기     

                button.setIcon(QIcon(self.icon0))

            #tootip 에 숨겨서 버튼의 인덱스 박아놓기     

                button.setToolTip(str(i) + "," + str(j))

            # 한개한개 바둑돌 클릭하면 연결    

                button.clicked.connect(self.pb_click)

            #버튼을 아까 만들어놓은 배열 line 에 등록 10줄  

                line.append(button)

                #arr2dpb는 바둑판 기본 10줄 만듬 

            self.arr2dpb.append(line)

        self.myrender() 

        

        #늘 항상 이쪽으로 넘어와야 한다.    

    def myrender(self):

        for i in range(10) : 

            for j in range(10) :

                #srr2d[i][j] -> 기본판 숫자가 변경된다. 숫자에 따라서 -> arr2dpb 사진을 변경해준다.  

                if self.arr2d[i][j] == 0 :         

                    self.arr2dpb[i][j].setIcon(self.icon0)

                elif self.arr2d[i][j] == 1 : 

                    self.arr2dpb[i][j].setIcon(self.icon1)

                elif self.arr2d[i][j] == 2 : 

                    self.arr2dpb[i][j].setIcon(self.icon2)

    

    def pb_click(self):       

        #print(self.sender().toolTip())

        a = self.sender().toolTip()

        #tooltip 에 저장해놓은것. 

        arr_ij = a.split(",")

        #print(arr_ij)

        #각각자리의 인덱스. tootip 에 저장해 놓은것으로 얻어옴 

        i = int(arr_ij[0])

        j = int(arr_ij[1])

        

        #return 은 break의 더 강력한 버전 

        

        if self.arr2d[i][j] > 0 :

            return         

        

        stone_info = 0  

        if self.flag_wb :  

            self.arr2d[i][j] = 1

            stone_info = 1 

        elif not self.flag_wb :

            self.arr2d[i][j] = 2 

            stone_info = 2             

# 지금 클릭한 위치의 x좌표 , y좌표, 돌이 흰색인지 아닌지를 파라미터로 가지고 getUP 함수 호출한다.    

        up = self.getUP(i , j , stone_info)  

        dw = self.getDW(i , j , stone_info) 

        le = self.getLE(i , j , stone_info) 

        ri = self.getRI(i , j , stone_info) 

        ur = self.getUR(i , j , stone_info) 

        ul = self.getUL(i , j , stone_info) 

        dr = self.getDR(i , j , stone_info) 

        dl = self.getDL(i , j , stone_info)    

#        

        print(up)

        self.myrender() 

        self.flag_wb = not self.flag_wb

            

#i는 x좌표 j는 y좌표 돌을 놓을때마다 이 메소드를 호출 

    def getUP(self, i, j, stone_info):   

        cnt = 0

        while True:

            i -= 1 

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt 

    

    def getDW(self, i, j, stone_info):   

        cnt = 0

        while True:

            i += 1 

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt     


    def getLE(self, i, j, stone_info):   

        cnt = 0

        while True:

            j -= 1 

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt  


    def getRI(self, i, j, stone_info):   

        cnt = 0

        while True:

            j += 1 

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt 

    def getUR(self, i, j, stone_info):   

        cnt = 0

        while True:

            i -= 1 

            j += 1 

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt

            

    def getUL(self, i, j, stone_info):   

        cnt = 0

        while True:

            i -= 1

            j -= 1  

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt   

            

    def getDR(self, i, j, stone_info):   

        cnt = 0

        while True:

            i += 1

            j += 1  

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt 

            

    def getDL(self, i, j, stone_info):   

        cnt = 0

        while True:

            i += 1

            j -= 1  

            if i < 0 : 

                return cnt 

            if j < 0 :

                return cnt  

            try : 

                if self.arr2d[i][j] == stone_info :

                    cnt += 1 

                else : 

                    return cnt 

            except :

                return cnt    

                                         

if __name__ == "__main__" :

    app = QApplication(sys.argv) 

    myWindow = WindowClass() 

    myWindow.show()

    app.exec_()            





댓글