[질문] 한 파일의 코드를 여러개로 나누는 법 ,,,



  • 안녕하세요 파이썬 초심자 입니다 , 프로젝트로 오목을 만들어 봤는데요 , 모든 코드가 한파일에 있어서요, 제가 하고 싶은건 이 파일에서 승리 인지 체크하는 부분인 def diagonalL 부터 def validCross 를 한 클래스로 묶어서 그 클래스를 다른 파일로 저장해 import 로 불러오고 싶은데요 , 나눠봤는데 자꾸 오류가 나서요, 고수님들 부탁드립니다.
    from tkinter import *
    from tkinter import messagebox

    x1=0
    y1=0
    stoneColor=0
    memory={}
    w,h=11,11
    matrix=[[0 for x in range(w)]for y in range(h)]

    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('<Button-1>',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()


답글을 작성하기 위해 로그인
 

Rasplay Forum 사이트로의 연결이 끊어졌습니다. 다시 연결을 시도하는동안 잠시만 기다려 주십시오.