Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MagicSquare/재동: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0002 pages from live compare)
 
Line 1: Line 1:
== 마방진 ==
== 마방진 ==
=== 고치기 전 소스 ===
=== 고치기 전 소스 ===
~cpp MagicSquare.py
<code>MagicSquare.py</code>
  import unittest
  import unittest
   
   
Line 13: Line 13:
          
          
     def testInitializeBoard(self):
     def testInitializeBoard(self):
         board = [[-1,-1,-1,-1],[0,0,0,-1],[0,0,0,-1],[0,0,0,-1]]
         board = &#91;&#91;-1,-1,-1,-1&#93;,&#91;0,0,0,-1&#93;,&#91;0,0,0,-1&#93;,&#91;0,0,0,-1&#93;&#93;
         self.assertEquals(board,self.magicSquare.getBoard())
         self.assertEquals(board,self.magicSquare.getBoard())
          
          
     def testBoard_3by3(self):
     def testBoard_3by3(self):
         self.magicSquare.calculBoard()
         self.magicSquare.calculBoard()
         board = [[-1,-1,-1,-1],[8,1,6,-1],[3,5,7,-1],[4,9,2,-1]]
         board = &#91;&#91;-1,-1,-1,-1&#93;,&#91;8,1,6,-1&#93;,&#91;3,5,7,-1&#93;,&#91;4,9,2,-1&#93;&#93;
         self.assertEquals(board,self.magicSquare.getBoard())
         self.assertEquals(board,self.magicSquare.getBoard())
   
   
Line 24: Line 24:
         self.magicSquare.setBoard(5)
         self.magicSquare.setBoard(5)
         self.magicSquare.calculBoard()
         self.magicSquare.calculBoard()
         board = [[-1,-1,-1,-1,-1,-1],[17,24,1,8,15,-1],[23,5,7,14,16,-1],\
         board = &#91;&#91;-1,-1,-1,-1,-1,-1&#93;,&#91;17,24,1,8,15,-1&#93;,&#91;23,5,7,14,16,-1&#93;,\
                   [4,6,13,20,22,-1],[10,12,19,21,3,-1],[11,18,25,2,9,-1]]
                   &#91;4,6,13,20,22,-1&#93;,&#91;10,12,19,21,3,-1&#93;,&#91;11,18,25,2,9,-1&#93;&#93;
         self.assertEquals(board,self.magicSquare.getBoard())
         self.assertEquals(board,self.magicSquare.getBoard())
   
   
Line 39: Line 39:
      
      
     def createBoard(self):
     def createBoard(self):
         self.board = []
         self.board = &#91;&#93;
         for i in range(self.boardLength+1):
         for i in range(self.boardLength+1):
             self.board.append([])
             self.board.append(&#91;&#93;)
             for j in range(self.boardLength+1):
             for j in range(self.boardLength+1):
                     self.board[i].append(0)
                     self.board&#91;i&#93;.append(0)
   
   
     def initializeBoard(self):
     def initializeBoard(self):
         for i in range(self.boardLength+1):
         for i in range(self.boardLength+1):
             for j in range(self.boardLength+1):
             for j in range(self.boardLength+1):
                 self.board[i][j] = 0
                 self.board&#91;i&#93;&#91;j&#93; = 0
                 self.board[i][self.boardLength] = -1
                 self.board&#91;i&#93;&#91;self.boardLength&#93; = -1
             self.board[0][i] = -1
             self.board&#91;0&#93;&#91;i&#93; = -1
                                  
                                  
     def getBoard(self):
     def getBoard(self):
Line 69: Line 69:
             elif col &gt; self.boardLength-1:
             elif col &gt; self.boardLength-1:
                 col = 0
                 col = 0
             elif self.board[row][col] != 0:
             elif self.board&#91;row&#93;&#91;col&#93; != 0:
                 row += 2
                 row += 2
                 col -= 1
                 col -= 1
Line 76: Line 76:
          
          
     def moveBoard(self,row,col):
     def moveBoard(self,row,col):
         self.board[row][col] = self.count
         self.board&#91;row&#93;&#91;col&#93; = self.count
         self.count+=1
         self.count+=1
          
          
Line 82: Line 82:
         for i in range(1,self.boardLength+1):
         for i in range(1,self.boardLength+1):
             for j in range(self.boardLength):
             for j in range(self.boardLength):
                 print self.board[i][j],"\t",
                 print self.board&#91;i&#93;&#91;j&#93;,"\t",
             print
             print
              
              
Line 96: Line 96:
         print 'Input must be odd number'
         print 'Input must be odd number'
=== 고친 후의 소스 ===
=== 고친 후의 소스 ===
~cpp MagicSquare_2.py
<code>MagicSquare_2.py</code>
  import unittest
  import unittest
   
   
Line 108: Line 108:
          
          
     def testInitializeBoard(self):
     def testInitializeBoard(self):
         expectBoard = [[0,0,0],[0,0,0],[0,0,0]]
         expectBoard = &#91;&#91;0,0,0&#93;,&#91;0,0,0&#93;,&#91;0,0,0&#93;&#93;
         self.assertEquals(expectBoard,self.magicSquare.getBoard())
         self.assertEquals(expectBoard,self.magicSquare.getBoard())
      
      
     def testBoard_3by3(self):
     def testBoard_3by3(self):
         expectBoard = [[8,1,6],[3,5,7],[4,9,2]]
         expectBoard = &#91;&#91;8,1,6&#93;,&#91;3,5,7&#93;,&#91;4,9,2&#93;&#93;
         self.magicSquare.moveBoard()
         self.magicSquare.moveBoard()
         self.assertEquals(expectBoard,self.magicSquare.getBoard())
         self.assertEquals(expectBoard,self.magicSquare.getBoard())
   
   
     def testBoard_5by5(self):
     def testBoard_5by5(self):
         expectBoard = [[17,24,1,8,15],[23,5,7,14,16],[4,6,13,20,22],[10,12,19,21,3],[11,18,25,2,9]]
         expectBoard = &#91;&#91;17,24,1,8,15&#93;,&#91;23,5,7,14,16&#93;,&#91;4,6,13,20,22&#93;,&#91;10,12,19,21,3&#93;,&#91;11,18,25,2,9&#93;&#93;
         self.magicSquare.setBoard(5)
         self.magicSquare.setBoard(5)
         self.magicSquare.moveBoard()
         self.magicSquare.moveBoard()
Line 124: Line 124:
  class CounterTestCase(unittest.TestCase):
  class CounterTestCase(unittest.TestCase):
     def setUp(self):
     def setUp(self):
         expectBoard = [[0,0,0],[0,0,0],[0,0,0]]
         expectBoard = &#91;&#91;0,0,0&#93;,&#91;0,0,0&#93;,&#91;0,0,0&#93;&#93;
         self.counter = Counter()
         self.counter = Counter()
         boardLength = 3
         boardLength = 3
Line 133: Line 133:
          
          
     def testSetBoard(self):
     def testSetBoard(self):
         expectBoard = [[0,0,0],[0,0,0],[0,0,0]]
         expectBoard = &#91;&#91;0,0,0&#93;,&#91;0,0,0&#93;,&#91;0,0,0&#93;&#93;
         self.assertEquals(expectBoard,self.counter.getBoard())
         self.assertEquals(expectBoard,self.counter.getBoard())
          
          
     def testWriteFirstCell(self):
     def testWriteFirstCell(self):
         expectBoard = [[0,1,0],[0,0,0],[0,0,0]]
         expectBoard = &#91;&#91;0,1,0&#93;,&#91;0,0,0&#93;,&#91;0,0,0&#93;&#93;
         self.counter.writeFirstCell()
         self.counter.writeFirstCell()
         self.assertEquals(expectBoard,self.counter.getBoard())
         self.assertEquals(expectBoard,self.counter.getBoard())
          
          
     def testWriteNextCell(self):
     def testWriteNextCell(self):
         expectBoard = [[0,1,0],[3,0,0],[4,0,2]]
         expectBoard = &#91;&#91;0,1,0&#93;,&#91;3,0,0&#93;,&#91;4,0,2&#93;&#93;
         self.counter.writeFirstCell()
         self.counter.writeFirstCell()
         for i in range(3):
         for i in range(3):
Line 181: Line 181:
      
      
     def isObstacle(self):
     def isObstacle(self):
         return self.board[self.row][self.col] != 0
         return self.board&#91;self.row&#93;&#91;self.col&#93; != 0
      
      
     def moveUpRight(self):
     def moveUpRight(self):
Line 198: Line 198:
      
      
     def writeCell(self):
     def writeCell(self):
         self.board[self.row][self.col] = self.count
         self.board&#91;self.row&#93;&#91;self.col&#93; = self.count
         self.count += 1
         self.count += 1
          
          
Line 215: Line 215:
          
          
     def createBoard(self):
     def createBoard(self):
         self.board=[[None for col in range(self.boardLength)] for row in range(self.boardLength)]
         self.board=&#91;&#91;None for col in range(self.boardLength)&#93; for row in range(self.boardLength)&#93;
          
          
     def initializeBoard(self):
     def initializeBoard(self):
         for row in range(self.boardLength):
         for row in range(self.boardLength):
             for col in range(self.boardLength):
             for col in range(self.boardLength):
                 self.board[row][col] = 0
                 self.board&#91;row&#93;&#91;col&#93; = 0
              
              
     def getBoard(self):
     def getBoard(self):
Line 235: Line 235:
         for row in range(self.boardLength):
         for row in range(self.boardLength):
             for col in range(self.boardLength):
             for col in range(self.boardLength):
                 print self.board[row][col],"\t",
                 print self.board&#91;row&#93;&#91;col&#93;,"\t",
             print
             print
              
              
Line 250: Line 250:
----
----
[[신재동/PracticeByTDD]]
[[신재동/PracticeByTDD]]

Latest revision as of 00:16, 27 March 2026

마방진

고치기 전 소스

MagicSquare.py

import unittest

class MagicSquareTestCase(unittest.TestCase):
    def setUp(self):
        self.magicSquare = MagicSquare()
        self.magicSquare.setBoard(3)
        
    def testCreation(self):
        self.assert_(self.magicSquare)
        
    def testInitializeBoard(self):
        board = [[-1,-1,-1,-1],[0,0,0,-1],[0,0,0,-1],[0,0,0,-1]]
        self.assertEquals(board,self.magicSquare.getBoard())
        
    def testBoard_3by3(self):
        self.magicSquare.calculBoard()
        board = [[-1,-1,-1,-1],[8,1,6,-1],[3,5,7,-1],[4,9,2,-1]]
        self.assertEquals(board,self.magicSquare.getBoard())

    def testBoard_5by5(self):
        self.magicSquare.setBoard(5)
        self.magicSquare.calculBoard()
        board = [[-1,-1,-1,-1,-1,-1],[17,24,1,8,15,-1],[23,5,7,14,16,-1],\
                 [4,6,13,20,22,-1],[10,12,19,21,3,-1],[11,18,25,2,9,-1]]
        self.assertEquals(board,self.magicSquare.getBoard())

class MagicSquare:
    def __init__(self):
        self.boardLength = 0
        self.count = 1
        
    def setBoard(self,arg):
        self.boardLength = arg
        self.createBoard()
        self.initializeBoard()
    
    def createBoard(self):
        self.board = []
        for i in range(self.boardLength+1):
            self.board.append([])
            for j in range(self.boardLength+1):
                    self.board[i].append(0)

    def initializeBoard(self):
        for i in range(self.boardLength+1):
            for j in range(self.boardLength+1):
                self.board[i][j] = 0
                self.board[i][self.boardLength] = -1
            self.board[0][i] = -1
                                
    def getBoard(self):
        return self.board
    
    def calculBoard(self):
        row = 1
        col = self.boardLength / 2
        self.moveBoard(row,col)
        while self.count != self.boardLength*self.boardLength+1:
            row -= 1
            col += 1
            if row < 1 and col > self.boardLength-1:
                row += 2
                col -= 1
            elif row < 1:
                row = self.boardLength
            elif col > self.boardLength-1:
                col = 0
            elif self.board[row][col] != 0:
                row += 2
                col -= 1
            
            self.moveBoard(row,col)
        
    def moveBoard(self,row,col):
        self.board[row][col] = self.count
        self.count+=1
        
    def printBoard(self):
        for i in range(1,self.boardLength+1):
            for j in range(self.boardLength):
                print self.board[i][j],"\t",
            print
            
if __name__ == "__main__":
    #unittest.main()
    magicSquare = MagicSquare()
    input = int(raw_input('Input(odd number): '))
    if input % 2 == 1:
        magicSquare.setBoard(input)
        magicSquare.calculBoard()
        magicSquare.printBoard()
    else:
        print 'Input must be odd number'

고친 후의 소스

MagicSquare_2.py

import unittest

class MagicSquareTestCase(unittest.TestCase):
    def setUp(self):
        self.magicSquare = MagicSquare()
        self.magicSquare.setBoard(3)
            
    def testCreateMagicSquare(self):
        self.assert_(self.magicSquare)
        
    def testInitializeBoard(self):
        expectBoard = [[0,0,0],[0,0,0],[0,0,0]]
        self.assertEquals(expectBoard,self.magicSquare.getBoard())
    
    def testBoard_3by3(self):
        expectBoard = [[8,1,6],[3,5,7],[4,9,2]]
        self.magicSquare.moveBoard()
        self.assertEquals(expectBoard,self.magicSquare.getBoard())

    def testBoard_5by5(self):
        expectBoard = [[17,24,1,8,15],[23,5,7,14,16],[4,6,13,20,22],[10,12,19,21,3],[11,18,25,2,9]]
        self.magicSquare.setBoard(5)
        self.magicSquare.moveBoard()
        self.assertEquals(expectBoard,self.magicSquare.getBoard())

class CounterTestCase(unittest.TestCase):
    def setUp(self):
        expectBoard = [[0,0,0],[0,0,0],[0,0,0]]
        self.counter = Counter()
        boardLength = 3
        self.counter.setBoard(expectBoard,boardLength)
    
    def testCreateCounter(self):
        self.assert_(self.counter)
        
    def testSetBoard(self):
        expectBoard = [[0,0,0],[0,0,0],[0,0,0]]
        self.assertEquals(expectBoard,self.counter.getBoard())
        
    def testWriteFirstCell(self):
        expectBoard = [[0,1,0],[0,0,0],[0,0,0]]
        self.counter.writeFirstCell()
        self.assertEquals(expectBoard,self.counter.getBoard())
        
    def testWriteNextCell(self):
        expectBoard = [[0,1,0],[3,0,0],[4,0,2]]
        self.counter.writeFirstCell()
        for i in range(3):
            self.counter.writeNextCell()
        self.assertEquals(expectBoard,self.counter.getBoard())

class Counter:
    def setBoard(self,board,boardLength):
        self.board = board
        self.boardLength =boardLength
        self.count = 1
        
    def writeFirstCell(self):
        self.row = 0
        self.col = self.boardLength / 2
        self.writeCell()
        
    def writeNextCell(self):
        self.moveUpRight()
        if self.isOutOfUpAndRight():
            self.moveDown()    
        elif self.isOutOfUp():
            self.movePassUp()    
        elif self.isOutOfRight():
            self.movePassRight()
        elif self.isObstacle():
            self.moveDown()
        self.writeCell()
        
    def isOutOfUpAndRight(self):
        return self.row < 0 and self.col > self.boardLength - 1
    
    def isOutOfUp(self):
        return self.row < 0
    
    def isOutOfRight(self):
        return self.col > self.boardLength - 1
    
    def isObstacle(self):
        return self.board[self.row][self.col] != 0
    
    def moveUpRight(self):
        self.row -= 1
        self.col += 1
        
    def moveDown(self):
        self.row += 2
        self.col -= 1
        
    def movePassUp(self):
        self.row = self.boardLength - 1
        
    def movePassRight(self):
        self.col = 0
    
    def writeCell(self):
        self.board[self.row][self.col] = self.count
        self.count += 1
        
    def getBoard(self):
        return self.board
            
class MagicSquare:
    def __init__(self):
        self.counter = Counter()
        self.boardLength = 0
        
    def setBoard(self,arg):
        self.boardLength = arg
        self.createBoard()
        self.initializeBoard()
        
    def createBoard(self):
        self.board=[[None for col in range(self.boardLength)] for row in range(self.boardLength)]
        
    def initializeBoard(self):
        for row in range(self.boardLength):
            for col in range(self.boardLength):
                self.board[row][col] = 0
            
    def getBoard(self):
        return self.board
    
    def moveBoard(self):
        self.counter.setBoard(self.board,self.boardLength)
        self.counter.writeFirstCell()
        for i in range(self.boardLength*self.boardLength - 1):
            self.counter.writeNextCell()
        self.board = self.counter.getBoard()

    def printBoard(self):
        for row in range(self.boardLength):
            for col in range(self.boardLength):
                print self.board[row][col],"\t",
            print
            
if __name__ == "__main__":
    unittest.main()
    magicSquare = MagicSquare()
    input = int(raw_input('Input(odd number): '))
    if input % 2 == 1:
        magicSquare.setBoard(input)
        magicSquare.moveBoard()
        magicSquare.printBoard()
    else:
        print 'Input must be odd number'

신재동/PracticeByTDD