본문 바로가기
Python/Python 기초

파이썬(QT) 오목게임 만들기

by 미눅스[멘토] 2023. 7. 1.
728x90
import sys
from PyQt5 import(uic)
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtGui
from PyQt5.Qt import QPushButton, QSize, QMessageBox


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

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.flag_bw = True
        self.setupUi(self)
        self.arr2D = [
            [0,0,0,0,0, 0,0,0,0,0],
            [0,2,1,2,0, 0,0,0,0,0],
            [0,1,0,0,0, 0,0,0,0,0],
            [0,0,1,2,0, 0,0,0,0,0],
            [0,0,1,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],
                        
            ]
        self.pb2D = []
        
        for i in range(10):
            line=[]
            for j in range(10):
                btn = QPushButton("", self)
                btn.setIcon(QtGui.QIcon('0.png'))    
                btn.setIconSize(QSize(40,40))
                btn.setToolTip("{},{}".format(i,j))
                btn.setGeometry(j*40,i*40,40,40)  
                btn.clicked.connect(self.myclick)
                line.append(btn)
            self.pb2D.append(line)
                
        self.myrender()
    
    
                
            
    def checkLu(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j -=1
                i -=1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkLd(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j -=1
                i +=1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkRd(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j +=1
                i +=1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkRu(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j +=1
                i -=1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkRt(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j += 1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkLt(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j -= 1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
        
    def checkDw(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                i += 1
                if i>9:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
        
        
    def checkUp(self,i,j,stone):
        cnt = 0
        while True : 
            i -= 1
            if i<0:
                return cnt
            if self.arr2D[i][j]== stone :
                cnt += 1
            else:
                return cnt

    
    def myclick(self):
        xy = self.sender().toolTip()
        x = xy.split(",")[0]
        y = xy.split(",")[1]
        
        i = int(x)
        j = int(y)
        
        
        if self.arr2D[i][j]>0:
            return
        
        stone = -1
        if self.flag_bw == True:
            self.arr2D[i][j]=1
            self.myrender()
            stone = 1
        else:
            self.arr2D[i][j]=2
            self.myrender()
            stone = 2
            
        up = self.checkUp(i,j,stone)
        dw = self.checkDw(i,j,stone) 
        
        lt = self.checkLt(i,j,stone) 
        rt = self.checkRt(i,j,stone) 
        
        lu = self.checkLu(i,j,stone) 
        rd = self.checkRd(i,j,stone) 
        
        ru = self.checkRu(i,j,stone) 
        ld = self.checkLd(i,j,stone) 
        
        if up+dw==4 or lt+rt==4 or lu+rd==4 or ru+ld==4:
            QMessageBox.about(self, "결과", "user{}승리!".format(stone))
            return
        
        
        self.flag_bw = not self.flag_bw    
        
        
       
        
        
        
    def myrender(self):
        for i in range(10):
             for j in range(10):
                if self.arr2D[i][j]==0:
                    self.pb2D[i][j].setIcon(QtGui.QIcon('0.png'))
                if self.arr2D[i][j]==1:
                    self.pb2D[i][j].setIcon(QtGui.QIcon('1.png'))
                if self.arr2D[i][j]==2:
                    self.pb2D[i][j].setIcon(QtGui.QIcon('2.png'))




if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

 

결과


 

 

최종코드

import sys
from PyQt5 import(uic)
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtGui
from PyQt5.Qt import QPushButton, QSize, QMessageBox


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

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.flag_bw = True
        self.flag_over = False
        self.setupUi(self)
        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, 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],
            
            [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, 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]
            ]
        
        self.pb2D = []

        
        for i in range(20):
            line=[]
            for j in range(20):
                btn = QPushButton("", self)
                btn.setIcon(QtGui.QIcon('0.png'))    
                btn.setIconSize(QSize(40,40))
                btn.setToolTip("{},{}".format(i,j))
                btn.setGeometry(j*40,i*40,40,40)  
                btn.clicked.connect(self.myclick)
                line.append(btn)
            self.pb2D.append(line)
            
        self.pb.clicked.connect(self.my_reset)
        self.myrender()
            
    def my_reset(self):
        self.flag_bw = True
        self.flag_over = False
        for i in range(20):
            for j in range(20):
                self.arr2D[i][j]=0
        self.myrender()
        print("my_reset")

    def checkLu(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j -=1
                i -=1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkLd(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j -=1
                i +=1
                if i<0:
                    return cnt
                if j<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkRd(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j +=1
                i +=1
                if i<0:
                    return cnt
                if j<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkRu(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j +=1
                i -=1
                if i<0:
                    return cnt
                if j<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkRt(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j += 1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
    def checkLt(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                j -= 1
                if i<0:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
        
    def checkDw(self,i,j,stone):
        cnt = 0
        try:
            while True : 
                i += 1
                if i>19:
                    return cnt
                
                if self.arr2D[i][j]== stone :
                    cnt += 1
                else:
                    return cnt
        except:
            return cnt
        
        
    def checkUp(self,i,j,stone):
        cnt = 0
        while True : 
            i -= 1
            if i<0:
                return cnt
            if self.arr2D[i][j]== stone :
                cnt += 1
            else:
                return cnt

    
    def myclick(self):
        if self.flag_over:
            return
        
        xy = self.sender().toolTip()
        x = xy.split(",")[0]
        y = xy.split(",")[1]
        
        i = int(x)
        j = int(y)
        
        
        if self.arr2D[i][j]>0:
            return
        
        stone = -1
        if self.flag_bw == True:
            self.arr2D[i][j]=1
            self.myrender()
            stone = 1
        else:
            self.arr2D[i][j]=2
            self.myrender()
            stone = 2
            
        up = self.checkUp(i,j,stone)
        dw = self.checkDw(i,j,stone) 
        
        lt = self.checkLt(i,j,stone) 
        rt = self.checkRt(i,j,stone) 
        
        lu = self.checkLu(i,j,stone) 
        rd = self.checkRd(i,j,stone) 
        
        ru = self.checkRu(i,j,stone) 
        ld = self.checkLd(i,j,stone) 
       
        print(up, dw, lt, rt, lu,rd,ru,ld)
        d1 = up+dw +1
        d2 = lt+rt +1
        d3 = lu+rd +1
        d4 = ru+ld +1 
        
        self.myrender()
        if d1==5 or d2==5 or d3==5 or d4==5:
            if self.flag_bw:
                QMessageBox.about(self, "결과","흑돌승리")
           
            else:
                QMessageBox.about(self, "결과","백돌승리")
            self.flag_over = True
        self.flag_bw = not self.flag_bw    
        
        
       
        
        
        
    def myrender(self):
        for i in range(20):
            for j in range(20):
                if self.arr2D[i][j]==0:
                    self.pb2D[i][j].setIcon(QtGui.QIcon('0.png'))
                if self.arr2D[i][j]==1:
                    self.pb2D[i][j].setIcon(QtGui.QIcon('1.png'))
                if self.arr2D[i][j]==2:
                    self.pb2D[i][j].setIcon(QtGui.QIcon('2.png'))




if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

결과