def testToText_ConvertsArrayToText_smallBoxMap(self): s = "#####\n" s += "# #\n" s += "#####\n" mObj = gameMap.GameMap() mObj.mArr = [[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]] self.assertEqual(s, mObj.toText())
def testExpandMap_appendsThirdDimensionHorizontally_Bug(self): horizontal = 1 vertical = 0 mObj = gameMap.GameMap() mObj.expandMap(1, 1, True, True) mObj.expandMap(horizontal, vertical, True, True) self.assertEqual(True, type(mObj.getLocation(2, 1)) is types.NoneType)
def testToText_ConvertsArrayToTextWithPlayerPosition(self): s = "#####\n" s += "# * #\n" s += "#####\n" mObj = gameMap.GameMap() mObj.mArr = [[1, 1, 1, 1, 1], [1, 0, 2, 0, 1], [1, 1, 1, 1, 1]] self.assertEqual(s, mObj.toText())
def setUp(self): self.brainClass = dullBrain.DullBrain self.mapFile = "maps/test-room1-box.txt" self.mObj = gameMap.GameMap() self.mObj.loadMapFile(self.mapFile) self.pos = c.Coordinate(3, 3) self.p = player.Player(self.brainClass, self.mObj, self.pos)
def testGameMap_ConvertsTextToArray_BoxMap(self): a = [[1] * 16] for i in range(0, 5): a.append([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) a.append([1] * 16) mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room1-box.txt") self.assertEqual(a, mObj.getMapArray())
def testToText_ConvertsArrayToTextWithUnknownLocationFields(self): s = "#####\n" s += "#???#\n" s += "#####\n" mObj = gameMap.GameMap() mObj.mArr = [[1, 1, 1, 1, 1], [1, None, None, None, 1], [1, 1, 1, 1, 1]] self.assertEqual(s, mObj.toText())
def testExpandMap_appendsMapVerticallyByOne(self): horizontal = 0 vertical = 1 expectedLenY = 1 mObj = gameMap.GameMap() mObj.expandMap(horizontal, vertical, True, True) self.assertEqual(expectedLenY, mObj.getHeight()) self.assertEqual(True, type(mObj.mArr[0]) is list)
def testExpandMap_appendsMapHorizontallyByEight(self): horizontal = 8 vertical = 4 expectedLenX = 8 mObj = gameMap.GameMap() mObj.expandMap(horizontal, vertical, True, True) for i in range(0, mObj.getHeight()): self.assertEqual(expectedLenX, len(mObj.mArr[i]))
def fakeDrawSimMap(): global drawSimMapCalled drawSimMapCalled = True # NOTE Make sure this map is the same as used in setup # of this test case mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room1-box.txt") return mObj.toText()
def testExpandMap_appendsMapVertically_Bug(self): horizontal = 0 vertical = 1 expectedLenX = 4 appendY = True mObj = gameMap.GameMap() mObj.expandMap(4, 1, True, True) mObj.expandMap(horizontal, vertical, True, True) self.assertEqual(expectedLenX, len(mObj.mArr[1]))
def testExpandMap_prependsMapVerticallyWithLenOfPreviousX(self): horizontal = 0 vertical = 1 expectedLenX = 4 appendY = False mObj = gameMap.GameMap() mObj.expandMap(4, 1, True, True) mObj.expandMap(horizontal, vertical, appendY, True) self.assertEqual(expectedLenX, len(mObj.mArr[0]))
def testExpandMap_appendsMapVerticallyByFour(self): horizontal = 0 vertical = 4 expectedLenY = 4 mObj = gameMap.GameMap() mObj.expandMap(horizontal, vertical, True, True) self.assertEqual(expectedLenY, mObj.getHeight()) for i in range(0, vertical): self.assertEqual(True, type(mObj.mArr[i]) is list)
def testStep_setsExitCode_onMapMatch(self): global playerGetMapValue global playerIsFinishedValue playerIsFinishedValue = True playerGetMapValue = gameMap.GameMap() playerGetMapValue.loadMapFile(self.gameMapFile) self.s.player.isFinished = fakePlayerIsFinished self.s.player.getPlayerMap = fakePlayerGetMap self.s.run() self.assertEqual(sim.Sim.EXITCODE_MAPMATCH, self.s.getExitCode())
def testGameMap_ConvertsArrayToTextWithPlayerPositionAndOrientation_Left( self): player_left = "<" player_left_value = 23 expectTxt = "#####\n" expectTxt += "# %c #\n" % player_left expectTxt += "#####\n" mObj = gameMap.GameMap() mObj.mArr = [[1, 1, 1, 1, 1], [1, 0, player_left_value, 0, 1], [1, 1, 1, 1, 1]] self.assertEqual(expectTxt, mObj.toText())
def testToText_ConvertsArrayToTextWithPlayerPositionAndOrientation_Down( self): player_down = "V" player_down_value = 22 expectTxt = "#####\n" expectTxt += "# %c #\n" % player_down expectTxt += "#####\n" mObj = gameMap.GameMap() mObj.mArr = [[1, 1, 1, 1, 1], [1, 0, player_down_value, 0, 1], [1, 1, 1, 1, 1]] self.assertEqual(expectTxt, mObj.toText())
def testExpandMap_prependsMapVerticallyByFour(self): horizontal = 0 vertical = 4 expectedLenY = 5 appendY = False mObj = gameMap.GameMap() mObj.expandMap(1, 1, True, True) mObj.setLocation(1, 1, 7) mObj.expandMap(horizontal, vertical, appendY, True) self.assertEqual(expectedLenY, mObj.getHeight()) self.assertEqual(7, mObj.getLocation(1, 5))
def testExpandMap_prependsMapHorizontallyByEight(self): horizontal = 8 vertical = 4 expectedLenX = 9 appendX = False mObj = gameMap.GameMap() mObj.expandMap(1, 1, True, True) mObj.setLocation(1, 1, 7) mObj.expandMap(horizontal, vertical, True, appendX) self.assertEqual(5, mObj.getHeight()) for i in range(0, mObj.getHeight()): self.assertEqual(expectedLenX, len(mObj.mArr[i])) self.assertEqual(7, mObj.getLocation(9, 1))
def testDrawPlayerMap_drawsGameMapWithPlayerPositionAndOrientation(self): global playerGetMapValue setLoc = gameMap.GameMap.setLocationInArray ori = self.s.player.getOrientation() playerSymbol = gameMap.GameMap.PLAYER_POSITION_VALUE_ARR pos = c.Coordinate(2, 2) simMapObj = self.s.getSimMap() simMapArray = simMapObj.getMapArray() setLoc(simMapArray, pos.y, pos.x, playerSymbol[ori]) playerGetMapValue = gameMap.GameMap() playerGetMapValue.mArr = simMapArray txtMap = gameMap.GameMap.arrayToText(simMapArray) self.s.player.getPlayerMap = fakePlayerGetMap self.assertEqual(txtMap, self.s.drawPlayerMap())
def testGameMap_ConvertsTextToArray_LMap(self): a = [[1] * 16] for i in range(0, 2): a.append([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) a.append([ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]) for i in range(0, 2): a.append([ 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, 1 ]) a.append([1] * 28) mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room2-l-shape.txt") self.assertEqual(a, mObj.getMapArray())
def testWriteMapFile_writesStringIntoFile(self): dirname = "testWriteMapFile" filename = "test.txt" filepath = os.path.join(dirname, filename) os.mkdir(dirname) mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room2-l-shape.txt") expected = mObj.toText() mObj.writeMapFile(filepath) actual = None if os.path.exists(filepath): actual = open(filepath, "r").read() try: self.assertEqual(expected, actual) finally: if os.path.exists(filepath): os.unlink(filepath) os.rmdir(dirname)
def testToText_paddsUnevenMapWhenSecondArgIsTrue_case2(self): s = "############# \n" s += "# # \n" s += "# # \n" s += "# ########\n" s += "# #\n" s += "# #\n" s += "####################\n" mObj = gameMap.GameMap() mObj.mArr = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] self.assertEqual(s, mObj.toText(True))
def testGetNonCollisionFields_forLShapeMap(self): self.maxDiff = None mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room2-l-shape.txt") fields = [] mapArray = mObj.getMapArray() maxY = mObj.getHeight() for y in range(1, maxY + 1): if y == 1 or y == maxY: continue maxX = len(mapArray[y - 1]) for x in range(1, maxX + 1): if x == 1 or x == maxX: continue # verify that the map location is really a non-collision field if mObj.getLocation(x, y) == 1: continue fields.append(c.Coordinate(x, y)) self.assertEqual(fields, mObj.getNonCollisionFields())
def testGetNonCollisionFields_forBoxMap(self): mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room1-box.txt") # construct the array of non-collision fields (that matches test-room1-box.txt) # calculate all coordinates while skipping all collision fields (result is all non-collision fields) # - skip line with y=1 completely # - skip line with y=7 comletely # - subtract first and last x coordinate in every line in between line y=1 and y=7 fields = [] mapArray = mObj.getMapArray() maxY = mObj.getHeight() for y in range(1, maxY + 1): if y == 1 or y == maxY: continue maxX = len(mapArray[y - 1]) for x in range(1, maxX + 1): if x == 1 or x == maxX: continue # verify that the map location is really a non-collision field if mObj.getLocation(x, y) == 1: continue fields.append(c.Coordinate(x, y)) self.assertEqual(fields, mObj.getNonCollisionFields())
def testWithinMap_returnsFalseWhenCoordinatesNotWithinMapArray(self): mObj = gameMap.GameMap() mObj.expandMap(3, 3, True, True) self.assertEqual(False, mObj.withinMap(4, 5))
def testWithinMap_returnsTrueWhenCoordinatesWithinMapArray(self): mObj = gameMap.GameMap() mObj.expandMap(3, 3, True, True) self.assertEqual(True, mObj.withinMap(2, 1))
def init_gameMap(self, gameMapFile): self.gameMap = gameMap.GameMap() self.gameMap.loadMapFile(self.gameMapFile)
def testWithinMap_returnsFalseWhenCoordinateIsMaxMap(self): mObj = gameMap.GameMap() mObj.expandMap(3, 3, True, True) self.assertEqual(True, mObj.withinMap(3, 3))
def testGetHeight_returnsMaxHeightOfMapArray(self): mObj = gameMap.GameMap() mObj.loadMapFile("maps/test-room2-l-shape.txt") self.assertEqual(len(self.mObj.mArr), mObj.getHeight())
def __init__(self, inputs, outputs): self.inputs = None self.outputs = None y = lambda element: element.startswith("is") and element.endswith("Collision") arg = "inputs" if not isinstance(inputs, dict): raise NotADictException(arg) arg = "outputs" if not isinstance(outputs, dict): raise NotADictException(arg) arg = "inputs" if len(inputs) == 0: raise IsEmptyException(arg) arg = "outputs" if len(outputs) == 0: raise IsEmptyException(arg) # test keys in inputs argument exists arg = "inputs" field = "isCollision" if not self._isInList(inputs.keys(), y): raise IsNotAKeyException("%s: %s" % (arg, field)) field = "getOrientation" if not field in inputs.keys(): raise IsNotAKeyException("%s: %s" % (arg, field)) field = "getMovementDirection" if not field in inputs.keys(): raise IsNotAKeyException("%s: %s" % (arg, field)) # test keys in outputs argument exist arg = "outputs" field = "setOrientation" if not field in outputs.keys(): raise IsNotAKeyException("%s: %s" % (arg, field)) field = "setMovementDirection" if not field in outputs.keys(): raise IsNotAKeyException("%s: %s" % (arg, field)) field = "move" if not field in outputs.keys(): raise IsNotAKeyException("%s: %s" % (arg, field)) # test values of inputs argument dict are callable arg = "inputs" field = "isCollision" if not self._isCallable(inputs, y): raise NotAFunctionException("%s: %s" % (arg, field)) field = "getOrientation" if not callable(inputs[field]): raise NotAFunctionException("%s: %s" % (arg, field)) field = "getMovementDirection" if not callable(inputs[field]): raise NotAFunctionException("%s: %s" % (arg, field)) # test values of outputs argument dict are callable arg = "outputs" field = "setOrientation" if not callable(outputs[field]): raise NotAFunctionException("%s: %s" % (arg, field)) field = "setMovementDirection" if not callable(outputs[field]): raise NotAFunctionException("%s: %s" % (arg, field)) field = "move" if not callable(outputs[field]): raise NotAFunctionException("%s: %s" % (arg, field)) self.inputs = inputs self.outputs = outputs self.mObj = gameMap.GameMap() self.mObj.mArr.append([0]) self.startPos = c.Coordinate(1, 1) self.pos = c.Coordinate(1, 1) self.lastPos = None self.firstCollision = None self.stepLog = [] self.finished = False
def testWithinMap_returnsFalseWhenCalledWithZero(self): mObj = gameMap.GameMap() mObj.expandMap(3, 3, False, True) self.assertEqual(False, mObj.withinMap(2, 0))