Python

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

redbear0077 2021. 3. 17. 20:01
반응형

오목 게임 만들기

PyQt5 설명

사용 툴-eclipse-아나콘다사용 설정은 인포트가 전부다

사진을 다운받아 사용하세요

오목판사진 : 0.png백돌사진 : 1.png흑돌사진 : 2png

 

0.png
1.png
2.png

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic,QtGui,QtCore


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

class WindowClass(QMainWindow, form_class) :
#오목판 배열
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        self.flag_wb = True
        self.flag_ing = True
        self.pb2d = []
        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]

        ]
#오목판 사진 반복 보여주기
        for i in range(19):
            line = []
            for j in range(19):
                temp = QPushButton("", self)
                temp.setIcon(QtGui.QIcon('0.png'))
                temp.setGeometry(j*40, i*40, 40, 40) 
                temp.setIconSize(QtCore.QSize(40,40))
                temp.setToolTip(str(i)+","+str(j))
                temp.clicked.connect(self.myclick)
                line.append(temp)
            self.pb2d.append(line)
        self.pb.clicked.connect(self.myreset)   
        self.myrender() 
            
    def myreset(self):
        self.flag_wb = True
        self.flag_ing = True
        for i in range(19):
            for j in range(19):
                self.arr2d[i][j] = 0
        self.myrender()
        
                
    def myrender(self):
        for i in range(19):
            for j in range(19):
                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')) 
                 
                    
#클릭시 바둑돌 놓는다
    def myclick(self):
        if not self.flag_ing :
            return
        
        str_ij = self.sender().toolTip()
        arr_ij = str_ij.split(",")
        int_i = int(arr_ij[0])
        int_j = int(arr_ij[1])
        
        if self.arr2d[int_i][int_j] > 0:
            return
        
        int_stone = 0
        if self.flag_wb :
            self.arr2d[int_i][int_j] = 1
            int_stone = 1
        else:
            self.arr2d[int_i][int_j] = 2
            int_stone = 2
#오목인지 확인
        up = self.getUP(int_i,int_j,int_stone) 
        dw = self.getDW(int_i,int_j,int_stone)   
        le = self.getLE(int_i,int_j,int_stone) 
        ri = self.getRI(int_i,int_j,int_stone)
        
        ur = self.getUR(int_i,int_j,int_stone)
        ul = self.getUL(int_i,int_j,int_stone)
        dr = self.getDR(int_i,int_j,int_stone)
        dl = self.getDL(int_i,int_j,int_stone)
        
        
        d1 = up + dw + 1
        d2 = le + ri + 1
        d3 = ur + dl + 1
        d4 = ul + dr + 1

        self.myrender()
#승자확인
        if d1 == 5 or d2 == 5 or d3 == 5 or d4 == 5 :
            self.flag_ing = False
            if self.flag_wb :  
                QMessageBox.about(self, "omok", "백돌이 이겼습니다.") 
            else :
                QMessageBox.about(self, "omok", "흑돌이 이겼습니다.") 

        self.flag_wb = not self.flag_wb
        
    def getDL(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_i += +1
                int_j += -1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except: 
            return cnt
        
#오목 확인 메서드들
    def getDR(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_i += +1
                int_j += +1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:   
            return cnt
        
        
    def getUL(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_i += -1
                int_j += -1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:                
            return cnt
        
        
        
        
    def getUR(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_i += -1
                int_j += 1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:                
            return cnt
        
        
    def getRI(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_j += 1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:                
            return cnt
        
    def getLE(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_j += -1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:                
            return cnt
        
        
        
    def getUP(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_i += -1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:                
            return cnt
            
    def getDW(self,int_i,int_j,int_stone):
        cnt = 0
        try:
            while True:
                int_i += +1
                if int_i < 0:
                    return cnt
                if int_j < 0:
                    return cnt
                
                if self.arr2d[int_i][int_j] == int_stone:
                    cnt += 1
                else :
                    return cnt
        except:                
            return cnt
        
#메인
if __name__ == "__main__" :
    app = QApplication(sys.argv) 
    myWindow = WindowClass() 
    myWindow.show()
    app.exec_()

    

1. 오목판 만들기

-오목판을 배열로 만들어 기본값을 0으로 만든다

-흑돌과 백 돌을 두는 위치에 0이 아닌 값으로 변경해 준다.

 

2. 오목판 사진 반복 보여주기

-오목판의 구조는 0이라는 사진이 반복적으로 출력하여 오목판을 만든다

-1이라는 돌을 두면 0 사진이 1로 변경

-2 사진을 두면 0 사진이 2로 변경

 

3. 클릭 시 바둑돌 놓는다.

- 바둑돌 사진을 변경해준다.

 

4. 오목인지 확인

-돌이 5개가 되는지 확인

 

5. 승자 확인

-오목 확인에서 흑돌인지 백 돌인지 확인 후 승자 표시

 

6. 오목 확인 메서드

-모든 방향의 오목을 확인

 

7. 메인

-오목을 실행하기 위해 필요한 메인 코드

반응형