카테고리 없음

오목2

logloglog 2021. 1. 28. 12:42
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import uic
from PyQt5.uic.Compiler.qtproxies import QtGui, QtCore
#uic : html body부분을 프로그램쪽으로 끌고오는 소스
Ui_MainWindow, QtBaseClass = uic.loadUiType("omock.ui")#멀티리턴. qtbaseclass 안쓰니까 첫번쨰방에있는 [0]으로 받아오됨
 
class MyWindow(QMainWindow):#상속
    def __init__(self):
        super(MyWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.flag_turn = 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]
                    ]
        

        self.pb2d = []
        
        for i in range(10):
            line = []
            for j in range(10):
                pb = QPushButton('',self)
                pb.setGeometry(j*40,i*40,40,40)
                pb.setIcon(QIcon('0.png'))
                pb.setIconSize(QSize(40,40))
                pb.clicked.connect(self.pb_click)
                pb.setToolTip('{},{}'.format(i, j))
                line.append(pb)
            self.pb2d.append(line)
            #self라는 윈도우판에 pb가 100게 붙어잇는거임 
        self.myRender()
            
    def myRender(self):
        for i in range(10):
            for j in range(10):
                if self.arr2d[i][j]==0:
                    self.pb2d[i][j].setIcon(QIcon('0.png'))
                if self.arr2d[i][j]==1:
                    self.pb2d[i][j].setIcon(QIcon('1.png'))
                if self.arr2d[i][j]==2:
                    self.pb2d[i][j].setIcon(QIcon('2.png'))
       
    def pb_click(self):
        tt = self.sender().toolTip().split(',')
        i = int(tt[0])
        j = int(tt[1])
        if self.arr2d[i][j]>0:#중복막음
            return
        if self.flag_turn :
            self.arr2d[i][j]=1
        else:
            self.arr2d[i][j]=2
        self.myRender()
        self.flag_turn = not self.flag_turn
            
if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = MyWindow()
            window.show()           
            app.exec_()

 

break  는 이중ㅂ포문에서 안쪽포문만 나감

return은 두개 다 나가버림

 

return은 break의 초강력버전이라고 생각할수도있음

 

 

이게 파이썬만의 문제점임

 

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import uic
from PyQt5.uic.Compiler.qtproxies import QtGui, QtCore
from day4.my_arr_2d import arr2d
#uic : html body부분을 프로그램쪽으로 끌고오는 소스
Ui_MainWindow, QtBaseClass = uic.loadUiType("omock.ui")#멀티리턴. qtbaseclass 안쓰니까 첫번쨰방에있는 [0]으로 받아오됨
 
class MyWindow(QMainWindow):#상속
    def __init__(self):
        super(MyWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.flag_turn = 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]
                    ]
        

        self.pb2d = []
        
        for i in range(10):
            line = []
            for j in range(10):
                pb = QPushButton('',self)
                pb.setGeometry(j*40,i*40,40,40)
                pb.setIcon(QIcon('0.png'))
                pb.setIconSize(QSize(40,40))
                pb.clicked.connect(self.pb_click)
                pb.setToolTip('{},{}'.format(i, j))
                line.append(pb)
            self.pb2d.append(line)
            #self라는 윈도우판에 pb가 100게 붙어잇는거임 
        self.myRender()
            
    def myRender(self):
        for i in range(10):
            for j in range(10):
                if self.arr2d[i][j]==0:
                    self.pb2d[i][j].setIcon(QIcon('0.png'))
                if self.arr2d[i][j]==1:
                    self.pb2d[i][j].setIcon(QIcon('1.png'))
                if self.arr2d[i][j]==2:
                    self.pb2d[i][j].setIcon(QIcon('2.png'))
       
    def pb_click(self):
        tt = self.sender().toolTip().split(',')
        i = int(tt[0])
        j = int(tt[1])
        if self.arr2d[i][j]>0:#중복막음
            return
        int_turn = 0   
        if self.flag_turn :
            self.arr2d[i][j]=1
            int_turn=1
        else:
            self.arr2d[i][j]=2
            int_turn=2
        
        up = self.getUP(i, j, int_turn)
        dw = self.getDown(i, j, int_turn)
        le = self.getLeft(i, j, int_turn)
        ri = self.getRight(i, j, int_turn)
        ul = self.getUpLeft(i, j, int_turn)
        ur = self.getUpRight(i, j, int_turn)
        dl = self.getDownLeft(i, j, int_turn)
        dr = self.getDownRight(i, j, int_turn)
        
        print("up",up)
        print("dw",dw)
        print("le",le)
        print("ri",ri)
        print("ul",ul)
        print("ur",ur)
        print("dl",dl)
        print("dr",dr)
        
        self.myRender()
        self.flag_turn = not self.flag_turn
        
  
        #렌더링위아래다 되지만 1,2 정해지기전에는 안됨
    def getUP(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                i += -1
                if i < 0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getDown(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                i += +1
                if i < 0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getLeft(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                j += -1
                if j < 0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getRight(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                j += +1
                if j < 0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getUpLeft(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                i += -1
                j += -1
                if i<0 or j < 0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getUpRight(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                i += -1
                j += +1
                if i<0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getDownLeft(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                i += +1
                j += -1
                if j<0:
                    return cnt
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
    def getDownRight(self,i,j,turn):
        cnt = 0
        while (True):
            try:
                i += +1
                j += +1
                if self.arr2d[i][j]==turn:
                    cnt += 1
                else:
                    return cnt
            except:
                return cnt
if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = MyWindow()
            window.show()           
            app.exec_()