한 파일에 모든코드를 여러가지 classes 로 나누는법?
제가 오목 게임을 만들어 봤는데,,, 이걸 여러개의 파일로 나눠야 하는데 class 를 여러개로 나눠서 해야되서요,,
어떻게 나누죠 ?
from tkinter import *
from tkinter import messagebox
#from testWin import *
x1=0
y1=0
stoneColor=0
memory={}
w,h=11,11
matrix=[[0 for x in range(w)]for y in range(h)]
class BadArgument(Exception):
def __init__(self):
self.__title="Unable Position"
self.__message="Can't put stone on place where already occupied"
def getTitle(self):
return self.__title
def __str__(self):
return self.__message
class koreanGame:
def __init__(self):
root=Tk()
root.title("O-Mok")
self.__topFrame=Frame(root)
self.__botFrame=Frame(root)
self.__gameBoard=Canvas(self.__topFrame,width=550,height=550,bg='darkorange')
self.__gameBoard.create_rectangle(25,25,525,525)
self.verticalLine()
self.horizonLine()
self.__gameBoard.bind('',self.drawCircle)
self.__resetButton=Button(self.__botFrame,text="RESTART",height=5,width=20,bg='lightpink',command=self.clearBoard)
self.__gameBoard.pack()
self.__resetButton.pack()
self.__topFrame.pack()
self.__botFrame.pack()
root.mainloop()
def getCordinate(self,event):
global x1,y1
x1,y1=(event.x),(event.y)
def verticalLine(self):
self.__cordi1=75
self.__cordi2=25
self.__cordi3=525
for i in range (0,9):
self.__gameBoard.create_line(self.__cordi1,self.__cordi2,self.__cordi1,self.__cordi3)
self.__cordi1+=50
def horizonLine(self):
self.__cordi1=75
self.__cordi2=25
self.__cordi3=525
for i in range(0,9):
self.__gameBoard.create_line(self.__cordi2,self.__cordi1,self.__cordi3,self.__cordi1)
self.__cordi1+=50
def errorMessage(self):
messagebox.showinfo('ERROR',"Can't put stone on place where already OCCUPIED")
def clearBoard(self):
global matrix,stoneColor
self.__gameBoard.delete('all')
self.horizonLine()
self.verticalLine()
self.__gameBoard.create_rectangle(25,25,525,525)
matrix=[[0 for x in range(w)]for y in range(h)]
stoneColor=0
def diagonalL(self,x,y,num):
count=0
#Diagonal test \
while (x>0) and (y>0) and (matrix[y-1][x-1]==num):
x-=1
y-=1
while ((x<=10) and (y<=10) and (matrix[y][x]==num)):
count+=1
x+=1
y+=1
if count==5:
self.winningMessage(num)
count1=0
def diagonalR(self,x,y,num):
global matrix
count=0
realx=y
realy=x
#Diagonal Test /////
realx=y
realy=x
#print(realx,realy)
while (realy<10) and (realx>0):
realy+=1
realx-=1
if realy==10 or realx==0:
while realy>=0 and realx<=10:
if matrix[realx][realy]==num:
count+=1
realy-=1
realx+=1
else:
realy-=1
realx+=1
if count ==5:
self.winningMessage(num)
realy=10
realx=10
count=0
def validCross(self,num):
global matrix
row=0
col=0
count=0
#Horizontal test
for i in range(11):
for x in matrix[i]:
if x==num:
count+=1
if count==5:
self.winningMessage(num)
else:
count=0
#Vertical Test
for i in range(11):
for x in range(11):
if matrix[x][i]==num:
count+=1
if count==5:
self.winningMessage(num)
else:
count=0
def winningMessage(self,num):
if num==1:
messagebox.showinfo("WINNING","BLACK WINS!!!!")
else:
messagebox.showinfo("WINNING","WHITE WINS!!!!")
def drawCircle(self,event):
self.getCordinate(event)
x=int((event.x)/50)
y=int((event.y)/50)
if x>=0 and y>=0 and x<=10 and y<=10:
global stoneColor,memory,matrix
if matrix[y][x]==0:
if stoneColor==0:
self.__gameBoard.create_oval((x*50+25)-23,(y*50+25)-23,(x*50+25)+23,(y*50+25)+23,fill='black')
stoneColor+=1
matrix[y][x]=1
#print(matrix)
#print()
self.validCross(1)
self.diagonalR(x,y,1)
self.diagonalL(x,y,1)
else:
self.__gameBoard.create_oval((x*50+25)-23,(y*50+25)-23,(x*50+25)+23,(y*50+25)+23,fill='white')
stoneColor-=1
matrix[y][x]=2
self.validCross(2)
self.diagonalR(x,y,2)
self.diagonalL(x,y,2)
#print(matrix)
#print()
else:
self.errorMessage()
#raise BadArgument()
def main():
koreanGame()
main()
이정도 짜놓고선 이런 걸 질문하는 이유가... 본인이
이정도 짜놓고선 이런 걸 질문하는 이유가... 본인이 짠 거 맞나요?
댓글 달기