한 파일에 모든코드를 여러가지 classes 로 나누는법?

gseol1의 이미지

제가 오목 게임을 만들어 봤는데,,, 이걸 여러개의 파일로 나눠야 하는데 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()

팬더12의 이미지

이정도 짜놓고선 이런 걸 질문하는 이유가... 본인이 짠 거 맞나요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.