== 마방진 == === 고치기 전 소스 === {{{~cpp MagicSquare.py}}} {{{~cpp 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' }}} === 고친 후의 소스 === {{{~cpp MagicSquare_2.py}}} {{{~cpp 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"]