~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'