== RandomWalk2 == === 기본적 요구사항만 만족 버전 === {{{~cpp RandomWalk2.py}}} {{{~cpp import unittest, random, os.path class ReaderTestCase(unittest.TestCase): def setUp(self): self.reader = Reader() self.reader.readTXT('case_0.txt') def testCreateReaderClass(self): self.assert_(self.reader) def testReadTXT(self): self.assertEquals('5 5\n',self.reader.getData()[0]) self.assertEquals('22224444346\n',self.reader.getData()[2]) def testBoardLength(self): self.assertEquals((5,5),self.reader.getBoardLength()) def testStartPoint(self): self.assertEquals((0,0),self.reader.getStartPoint()) def testRoachPath(self): expectPath = [2,2,2,2,4,4,4,4,3,4,6] self.assertEquals(expectPath,self.reader.getPath()) class BoardTestCase(unittest.TestCase): def setUp(self): self.board = Board() def testCreateBoardClass(self): self.assert_(self.board) def testSetBoard(self): expectBoard = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]] self.board.readTXT('case_0.txt') self.board.initializeBoard() self.assertEquals(expectBoard,self.board.getBoard()) class MoveRoachTestCase(unittest.TestCase): def setUp(self): TXT = 'case_0.txt' self.board = Board() self.board.readTXT(TXT) self.board.initializeBoard() self.moveRoach = MoveRoach() self.moveRoach.readTXT(TXT) self.moveRoach.setBoard(self.board.getBoard()) def testCreateMoveRoachCalss(self): self.assert_(self.moveRoach) def testMoveRoach(self): expectBoard = [[2,1,1,1,1],[1,0,0,0,2],[0,0,0,0,1],[0,0,0,0,1],[0,0,0,0,1]] self.moveRoach.move() self.board.setBoard(self.moveRoach.getBoard()) self.assertEquals(expectBoard,self.board.getBoard()) def testCount(self): self.moveRoach.move() self.assertEquals(11,self.moveRoach.getCount()) class AllCaseTestCase(unittest.TestCase): def setUp(self): self.board = Board() self.moveRoach = MoveRoach() self.expectBoard = [[[2,1,1,1,1],[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]],\ [[5,0,0,0,0],[4,0,0,0,0],[4,0,0,0,0],[4,0,0,0,0],[4,0,0,0,0]],\ [[1,1,1],[1,1,1],[1,1,1]]] def testAllCase(self): for i in range(3): TXT = 'case_' + str(i+1) +'.txt' self.board.readTXT(TXT) self.board.initializeBoard() self.moveRoach.readTXT(TXT) self.moveRoach.setBoard(self.board.getBoard()) self.moveRoach.move() self.board.setBoard(self.moveRoach.getBoard()) self.assertEquals(self.expectBoard[i],self.board.getBoard()) class Reader: def readTXT(self,TXT): file = open(os.path.join(TXT)) self.data = file.readlines() def getData(self): return self.data def getBoardLength(self): rowLength = int(self.data[0].split()[0]) colLength = int(self.data[0].split()[1]) return rowLength,colLength def getStartPoint(self): row = int(self.data[1].split()[0]) col = int(self.data[1].split()[1]) return row,col def getPath(self): path = [] i = 0 while self.data[2][i] != '\n': path.append(int(self.data[2][i])) i += 1 return path class Board: def __init__(self): self.reader = Reader() def readTXT(self,TXT): self.reader.readTXT(TXT) def initializeBoard(self): self.row, self.col = self.reader.getBoardLength() self.board = [[0 for i in range(self.col)] for j in range(self.row)] def setBoard(self,board): self.board = board def getBoard(self): return self.board def printBoard(self): for row in range(self.row): for col in range(self.col): print self.board[row][col],' ', print print class MoveRoach: def __init__(self): self.dirNumber = {0:(-1,0),1:(-1,1),2:(0,1),3:(1,1),4:(1,0),5:(1,-1),6:(0,-1),7:(-1,-1)} self.rdr = Reader() self.brd = Board() self.count = 0 def readTXT(self,TXT): self.rdr.readTXT(TXT) self.brd.readTXT(TXT) self.brd.initializeBoard() def setBoard(self,board): self.board = board def getBoard(self): return self.board def setStartPoint(self): self.row, self.col = self.rdr.getStartPoint() self.board[self.row][self.col] += 1 def move(self): self.setStartPoint() self.path = self.rdr.getPath() for i in range(len(self.path)): move_row, move_col = self.dirNumber[self.path[i]] self.row += move_row self.col += move_col self.checkOutOfBoard() self.board[self.row][self.col] += 1 self.count += 1 if self.isMoveAllCell(): break def isMoveAllCell(self): cellCount = 0 for i in range(self.rowLength): for j in range(self.colLength): if self.board[i][j] != 0: cellCount += 1 if cellCount != self.rowLength * self.colLength: return False else: return True def checkOutOfBoard(self): self.rowLength, self.colLength = self.rdr.getBoardLength() if self.row < 0: self.row = self.rowLength - 1 if self.col < 0: self.col = self.colLength - 1 if self.row >= self.rowLength: self.row = 0 if self.col >= self.colLength: self.col = 0 def getCount(self): return self.count def printCount(self): print self.count,'\n\n' if __name__ == '__main__': #unittest.main() TXT = 'input.TXT' board = Board() moveRoach = MoveRoach() board.readTXT(TXT) board.initializeBoard() moveRoach.readTXT(TXT) moveRoach.setBoard(board.getBoard()) moveRoach.move() board.setBoard(moveRoach.getBoard()) moveRoach.printCount() board.printBoard() }}} === 요구사항1 만족 버전 === {{{~cpp import unittest, random, os.path class ReaderTestCase(unittest.TestCase): def setUp(self): self.rdr = Reader() self.rdr.readTXT('case2_0.txt') def testCreaterdrClass(self): self.assert_(self.rdr) def testReadTXT(self): self.assertEquals('10 10\n',self.rdr.getData()[0]) self.assertEquals('22222444445\n',self.rdr.getData()[2]) self.assertEquals('121212645372\n',self.rdr.getData()[4]) def testbrdLength(self): self.assertEquals((10,10),self.rdr.getBoardLength()) def testStartPoint(self): self.assertEquals((0,0,3,7),self.rdr.getStartPoint()) def testRoachPath(self): expectPath = [[2,2,2,2,2,4,4,4,4,4,5],[1,2,1,2,1,2,6,4,5,3,7,2]] self.assertEquals(expectPath,self.rdr.getPath()) class BoardTestCase(unittest.TestCase): def setUp(self): self.brd = Board() def testCreateBoardClass(self): self.assert_(self.brd) def testSetBoard(self): expectboard = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]] self.brd.readTXT('case2_0.txt') self.brd.initializeBoard() self.assertEquals(expectboard,self.brd.getBoard()) class MoveRoachTestCase(unittest.TestCase): def setUp(self): TXT = 'case2_0.txt' self.brd = Board() self.brd.readTXT(TXT) self.brd.initializeBoard() self.moveRoach = MoveRoach() self.moveRoach.readTXT(TXT) self.moveRoach.setBoard(self.brd.getBoard()) def testCreateMoveRoachClass(self): self.assert_(self.moveRoach) def testMoveRoach(self): expectBoard = [[1,1,3,2,1,1,0,0,0,0],\ [1,1,1,0,0,1,0,0,0,0],\ [0,2,1,0,0,1,0,0,1,1],\ [0,0,1,0,0,1,0,1,0,0],\ [0,0,0,0,0,1,0,0,0,0],\ [0,0,0,0,0,1,0,0,0,0],\ [0,0,0,0,1,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0]] self.moveRoach.move() self.brd.setBoard(self.moveRoach.getBoard()) self.assertEquals(expectBoard,self.brd.getBoard()) def testCount(self): self.moveRoach.move() self.assertEquals((11,12),self.moveRoach.getCount()) class Reader: def readTXT(self,TXT): file = open(os.path.join(TXT)) self.data = file.readlines() def getData(self): return self.data def getBoardLength(self): rowLength = int(self.data[0].split()[0]) colLength = int(self.data[0].split()[1]) return rowLength,colLength def getStartPoint(self): row = [0,0] col = [0,0] for who in range(2): row[who] = int(self.data[who*2+1].split()[0]) col[who] = int(self.data[who*2+1].split()[1]) return row[0],col[0],row[1],col[1] def getPath(self): path = [[],[]] for who in range(2): i = 0 while self.data[who*2+2][i] != '\n': path[who].append(int(self.data[who*2+2][i])) i += 1 return path class Board: def __init__(self): self.rdr = Reader() def readTXT(self,TXT): self.rdr.readTXT(TXT) def initializeBoard(self): self.row, self.col = self.rdr.getBoardLength() self.board = [[0 for i in range(self.col)] for j in range(self.row)] def setBoard(self,board): self.board = board def getBoard(self): return self.board def printBoard(self): for row in range(self.row): for col in range(self.col): print self.board[row][col],' ', print print class MoveRoach: def __init__(self): self.dirNumber = {0:(-1,0),1:(-1,1),2:(0,1),3:(1,1),4:(1,0),5:(1,-1),6:(0,-1),7:(-1,-1)} self.rdr = Reader() self.brd = Board() self.count = [0,0] self.row = [0,0] self.col = [0,0] self.path = [0,0] def readTXT(self,TXT): self.rdr.readTXT(TXT) self.brd.readTXT(TXT) self.brd.initializeBoard() def setBoard(self,board): self.board = board def getBoard(self): return self.board def setStartPoint(self): self.row[0], self.col[0], self.row[1], self.col[1] = self.rdr.getStartPoint() for who in range(2): self.board[self.row[who]][self.col[who]] += 1 def move(self): self.setStartPoint() for who in range(2): self.path[who] = self.rdr.getPath()[who] self.time = self.getTime() for i in range(self.time): for who in range(2): if self.count[who] < len(self.path[who]): move_row, move_col = self.dirNumber[self.path[who][i]] self.row[who] += move_row self.col[who] += move_col self.checkOutOfbrd() self.board[self.row[who]][self.col[who]] += 1 self.count[who] += 1 if self.isMoveAllCell(): break def getTime(self): if len(self.path) > len(self.path[0]): return len(self.path[0]) else: return len(self.path[1]) def isMoveAllCell(self): cellCount = 0 for i in range(self.rowLength): for j in range(self.colLength): if self.board[i][j] != 0: cellCount += 1 if cellCount != self.rowLength * self.colLength: return False else: return True def checkOutOfbrd(self): self.rowLength, self.colLength = self.rdr.getBoardLength() for i in range(len(self.row)): if self.row[i] < 0: self.row[i] = self.rowLength - 1 if self.row[i] >= self.rowLength: self.row[i] = 0 for j in range(len(self.col)): if self.col[j] < 0: self.col[j] = self.colLength - 1 if self.col[j] >= self.colLength: self.col[j] = 0 def getCount(self): return self.count[0],self.count[1] def printCount(self): for i in range(len(self.count)): print self.count[i],'\n' if __name__ == '__main__': #unittest.main() TXT = 'input_2.TXT' brd = Board() moveRoach = MoveRoach() brd.readTXT(TXT) brd.initializeBoard() moveRoach.readTXT(TXT) moveRoach.setBoard(brd.getBoard()) moveRoach.move() brd.setBoard(moveRoach.getBoard()) moveRoach.printCount() brd.printBoard() }}} === 요구사항2 만족 버전 === {{{~cpp import unittest, os.path class ReaderTestCase(unittest.TestCase): def setUp(self): self.rdr = Reader() self.rdr.readTXT('case3_0.txt') def testCreaterdrClass(self): self.assert_(self.rdr) def testReadTXT(self): self.assertEquals('10 10\n',self.rdr.getData()[0]) self.assertEquals('57575757575757575757\n',self.rdr.getData()[6]) def testbrdLength(self): self.assertEquals((10,10),self.rdr.getBoardLength()) def testStartPointRow(self): self.assertEquals([0,3,2,8],self.rdr.getStartPointRow()) def testStartPointCol(self): self.assertEquals([0,7,5,8],self.rdr.getStartPointCol()) def testRoachPath(self): expectPath = [[2,2,2,2,2,4,4,4,4,4,5],[1,2,1,2,1,2,6,4,5,3,7,2],\ [5,7,5,7,5,7,5,7,5,7,5,7,5,7,5,7,5,7,5,7],[6,6,3]] self.assertEquals(expectPath,self.rdr.getPath()) def testCountRoachNumber(self): expectRoachNumber = 4 self.assertEquals(expectRoachNumber,self.rdr.getRoachNumber()) class BoardTestCase(unittest.TestCase): def setUp(self): self.brd = Board() def testCreateBoardClass(self): self.assert_(self.brd) def testInitializeBoard(self): expectboard = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]] self.brd.readTXT('case3_0.txt') self.brd.initializeBoard() self.assertEquals(expectboard,self.brd.getBoard()) class MoveRoachTestCase(unittest.TestCase): def setUp(self): TXT = 'case3_0.txt' self.brd = Board() self.brd.readTXT(TXT) self.brd.initializeBoard() self.moveRoach = MoveRoach() self.moveRoach.readTXT(TXT) self.moveRoach.setBoard(self.brd.getBoard()) def testCreateMoveRoachClass(self): self.assert_(self.moveRoach) def testStartPoint(self): self.moveRoach.setStartPoint() self.assertEquals(1,self.moveRoach.getBoard()[0][0]) self.assertEquals(1,self.moveRoach.getBoard()[3][7]) def testGetTime(self): expectTime = 20 self.moveRoach.setPath() self.assertEquals(expectTime,self.moveRoach.getTime()) def testIsMoveAllCell(self): board = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]] self.moveRoach.setBoard(board) self.assertEquals(True,self.moveRoach.isMoveAllCell()) def testMoveRoach(self): expectBoard = [[1,1,3,2,1,1,0,0,0,0],[1,1,1,0,0,1,0,0,0,0],\ [0,4,1,2,0,4,0,2,1,3],[2,0,3,0,2,1,2,1,2,0],\ [0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,1,0,0,0,0],\ [0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],\ [0,0,0,0,0,0,1,1,1,0],[0,0,0,0,0,0,0,1,0,0]] self.moveRoach.move() self.assertEquals(expectBoard,self.moveRoach.getBoard()) def testGetCount(self): expectCount = [11, 12, 20, 3] self.moveRoach.move() self.assertEquals(expectCount,self.moveRoach.getCount()) class Reader: def readTXT(self,TXT): file = open(os.path.join(TXT)) self.data = file.readlines() def getData(self): return self.data def getBoardLength(self): rowLength = int(self.data[0].split()[0]) colLength = int(self.data[0].split()[1]) return rowLength,colLength def getStartPointRow(self): row = [0 for i in range(self.getRoachNumber())] for who in range(self.roachNumber): row[who] = int(self.data[who*2+1].split()[0]) return row def getStartPointCol(self): col = [0 for i in range(self.getRoachNumber())] for who in range(self.roachNumber): col[who] = int(self.data[who*2+1].split()[1]) return col def getPath(self): path = [[] for i in range(self.getRoachNumber())] for who in range(self.roachNumber): i = 0 while self.data[who*2+2][i] != '\n': path[who].append(int(self.data[who*2+2][i])) i += 1 return path def calculRoachNumber(self): line = 0 while self.data[line] != '999\n': line += 1 self.roachNumber = line / 2 def getRoachNumber(self): self.calculRoachNumber() return self.roachNumber class Board: def __init__(self): self.rdr = Reader() def readTXT(self,TXT): self.rdr.readTXT(TXT) def initializeBoard(self): self.row, self.col = self.rdr.getBoardLength() self.board = [[0 for i in range(self.col)] for j in range(self.row)] def setBoard(self,board): self.board = board def getBoard(self): return self.board def printBoard(self): for row in range(self.row): for col in range(self.col): print self.board[row][col],',', print print class MoveRoach: def __init__(self): self.dirNumber = {0:(-1,0),1:(-1,1),2:(0,1),3:(1,1),4:(1,0),5:(1,-1),6:(0,-1),7:(-1,-1)} self.rdr = Reader() self.brd = Board() def readTXT(self,TXT): self.rdr.readTXT(TXT) self.brd.readTXT(TXT) self.brd.initializeBoard() def setBoard(self,board): self.board = board self.rowLength = len(board) self.colLength = len(board[0]) def getBoard(self): return self.board def setStartPoint(self): self.row = self.rdr.getStartPointRow() self.col = self.rdr.getStartPointCol() for who in range(self.rdr.getRoachNumber()): self.board[self.row[who]][self.col[who]] += 1 def setPath(self): self.path = [0 for i in range(self.rdr.getRoachNumber())] for who in range(self.rdr.getRoachNumber()): self.path[who] = self.rdr.getPath()[who] def setCount(self): self.count = [0 for i in range(self.rdr.getRoachNumber())] self.time = self.getTime() def move(self): self.setStartPoint() self.setPath() self.setCount() for i in range(self.time): for who in range(self.rdr.getRoachNumber()): if self.isRestPath(who): move_row, move_col = self.dirNumber[self.path[who][i]] self.row[who] += move_row self.col[who] += move_col self.checkOutOfBoard() self.board[self.row[who]][self.col[who]] += 1 self.count[who] += 1 if self.isMoveAllCell(): break def isRestPath(self,who): return self.count[who] < len(self.path[who]) def getTime(self): time = 0 for i in range(self.rdr.getRoachNumber()): time = max(time,len(self.path[i])) return time def isMoveAllCell(self): cellCount = 0 for i in range(self.rowLength): for j in range(self.colLength): if self.board[i][j] != 0: cellCount += 1 if cellCount < self.rowLength * self.colLength: return False else: return True def checkOutOfBoard(self): for i in range(self.rdr.getRoachNumber()): if self.row[i] < 0: self.row[i] = self.rowLength - 1 if self.row[i] >= self.rowLength: self.row[i] = 0 for j in range(self.rdr.getRoachNumber()): if self.col[j] < 0: self.col[j] = self.colLength - 1 if self.col[j] >= self.colLength: self.col[j] = 0 def getCount(self): return self.count def printCount(self): for i in range(len(self.count)): print self.count[i],'\n' if __name__ == '__main__': unittest.main() TXT = 'case3_0.TXT' brd = Board() moveRoach = MoveRoach() brd.readTXT(TXT) brd.initializeBoard() moveRoach.readTXT(TXT) moveRoach.setBoard(brd.getBoard()) moveRoach.move() brd.setBoard(moveRoach.getBoard()) moveRoach.printCount() brd.printBoard() }}} ---- ["신재동/PracticeByTDD"]