public void TestCopyTo_DestinationObjectNotValid_TrownException() { int row = 0; int column = 0; PlayerPosition target = new PlayerPosition(row, column); PlayerPosition destinationPlayerPosition = null; target.CopyTo(destinationPlayerPosition); }
public void TestInitializeLabyrinthMapInitial_CreateGame7x7_LabyrinthIsSolvable() { Labyrinth_Accessor target = new Labyrinth_Accessor(7, 7, 3, 3); target.InitializeLabyrinthMap(); PlayerPosition playerInitialPosition = new PlayerPosition(3,3); bool isLabirinthSolvable = GameUtilities_Accessor.IsLabyrinthSolvable(target.labyrinthMap, playerInitialPosition); Assert.IsTrue(isLabirinthSolvable); }
public void CopyTo(PlayerPosition destinationPlayerPosition) { if (destinationPlayerPosition == null) { throw new ArgumentNullException("The destination item must be a valid object"); } destinationPlayerPosition.Row = this.Row; destinationPlayerPosition.Column = this.Column; }
public void TestCheckPlayerPositionValidityInLabyrinthMap_PlayerPositionOutOfRange_ThrownException() { int[,] labyrinthMap = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, }; PlayerPosition playerStartPosition = new PlayerPosition(3, 3); GameUtilities_Accessor.CheckPlayerPositionValidityInLabyrinthMap(labyrinthMap, playerStartPosition); }
public void TestIsLabyrinthSolvable_LabitynthWithManyExits_ReturnsTrue() { int[,] labyrinthMap = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, }; PlayerPosition playerStartPosition = new PlayerPosition(1, 1); bool isLabyrinthSolvable = GameUtilities.IsLabyrinthSolvable(labyrinthMap, playerStartPosition); Assert.IsTrue(isLabyrinthSolvable); }
public void TestIsLabyrinthSolvable_LabitynthIsNotSolvable_ReturnsFalse() { int[,] labyrinthMap = new int[,] { { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, }; PlayerPosition playerStartPosition = new PlayerPosition(1, 1); bool isLabyrinthSolvable = GameUtilities.IsLabyrinthSolvable(labyrinthMap, playerStartPosition); Assert.IsFalse(isLabyrinthSolvable); }
/// <summary> /// Verifies that given labyrinth map has at leas one possible solution /// </summary> /// <param name="labyrinthMap">Labyrinth map to check</param> /// <param name="playerPositionRow">Player's starting row position</param> /// <param name="playerPositionColumn">Player's starting column position</param> /// <returns>Returns true if there is at least one solution and false otherwise</returns> public static bool IsLabyrinthSolvable( int[,] labyrinthMap, PlayerPosition playerStartPosition) { CheckLabyrinthMapValidity(labyrinthMap); CheckPlayerPositionValidityInLabyrinthMap(labyrinthMap, playerStartPosition); int[,] labyrinthMapTestable = (int[,])labyrinthMap.Clone(); int playerPositionRow = playerStartPosition.Row; int playerPositionColumn = playerStartPosition.Column; bool isSolvable = LabyrintExitExists(labyrinthMapTestable, playerPositionRow, playerPositionColumn); return isSolvable; }
/// <summary> /// Initialises the labyrinth game /// setting information about the board size and the initial player position /// </summary> /// <param name="rows">Number of labyrinth rows</param> /// <param name="columns">Number of labyrinth columns</param> /// <param name="playerInitialPositionRow">Initial row position of the player</param> /// <param name="playerInitialPositionColumn">Initial column position of the player</param> public Labyrinth(int rows, int columns, int playerInitialPositionRow, int playerInitialPositionColumn) { this.VerifyLabyrinthSizeParameter(rows, columns); this.VerifyPlayerInitialPositionParameters(rows, columns, playerInitialPositionRow, playerInitialPositionColumn); this.labyrinthMap = new int[rows, columns]; this.playerCurrentPosition = new PlayerPosition(playerInitialPositionRow, playerInitialPositionColumn); this.playerInitialPosition = new PlayerPosition(playerInitialPositionRow, playerInitialPositionColumn); this.InitializeUserCommadsParser(); this.InitializeUserActionList(); this.StartNewGame(); }
/// <summary> /// Checks if the given player position is inside the labyrinth map /// </summary> /// <param name="labyrinthMap">Labyrinth map</param> /// <param name="playerPosition">Player's position</param> /// <returns>Returns true if the position is inside the labyrinth. /// False - otherwise</returns> public static bool IsPlayerPositionInLabyrinthRange(int[,] labyrinthMap, PlayerPosition playerPosition) { CheckLabyrinthMapValidity(labyrinthMap); int labyrinthLastRowPosition = labyrinthMap.GetLength(0) - 1; int labyrinthLastColumnPosition = labyrinthMap.GetLength(1) - 1; bool isPlayerRowPositionInRange = playerPosition.Row >= 0 && playerPosition.Row <= labyrinthLastRowPosition; bool isPlayerColumnPositionInRange = playerPosition.Column >= 0 && playerPosition.Column <= labyrinthLastColumnPosition; bool isPlayerPositionInLabyrinthRange = isPlayerRowPositionInRange && isPlayerColumnPositionInRange; return isPlayerPositionInLabyrinthRange; }
/// <summary> /// Checks if the given player position is at the labyrinth's border /// </summary> /// <param name="labyrinthMap">Labyrinth map</param> /// <param name="playerPosition">Player's position</param> /// <returns>Returns true if the position is at one of the the labyrinth's borders. ///False - otherwise</returns> public static bool IsPlayerPositionAtLabyrintBorder(int[,] labyrinthMap, PlayerPosition playerPosition) { CheckLabyrinthMapValidity(labyrinthMap); int labyrinthLastRowPosition = labyrinthMap.GetLength(0) - 1; int labyrinthLastColumnPosition = labyrinthMap.GetLength(1) - 1; bool isPlayerRowPositionAtBorder = playerPosition.Row == 0 || playerPosition.Row == labyrinthLastRowPosition; bool isPlayerColumnPositionAtBorder = playerPosition.Column == 0 || playerPosition.Column == labyrinthLastColumnPosition; bool isPlayerPositionAtLabyrintBorder = isPlayerRowPositionAtBorder || isPlayerColumnPositionAtBorder; return isPlayerPositionAtLabyrintBorder; }
private bool IsPlayerPositionChanged(PlayerPosition playerPreviousPosition) { bool isPlayerPositionChanged = this.playerCurrentPosition.Row != playerPreviousPosition.Row || this.playerCurrentPosition.Column != playerPreviousPosition.Column; return isPlayerPositionChanged; }
public void TestIsPlayerPositionAtLabyrintBorder_PlayerPositionNotAtBorder_ReturnsFalse() { int[,] labyrinthMap = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, }; PlayerPosition playerStartPosition = new PlayerPosition(1, 1); bool isPlayerPositionAtRange = GameUtilities.IsPlayerPositionAtLabyrintBorder(labyrinthMap, playerStartPosition); Assert.IsFalse(isPlayerPositionAtRange); }
private static void CheckPlayerPositionValidityInLabyrinthMap(int[,] labyrinthMap, PlayerPosition playerStartPosition) { if (playerStartPosition == null) { throw new ArgumentNullException("Player position should a valid object."); } if (!IsPlayerPositionInLabyrinthRange(labyrinthMap, playerStartPosition)) { throw new ArgumentOutOfRangeException( "Player position should be within the labyrinth range."); } }
//The method works with integer positions instead of PlayerPosition object in order //to prevent stack overflow in case of big labyrinth maps private static bool LabyrintExitExists(int[,] labyrinthMap, int playerPositionRow, int playerPositionColumn) { PlayerPosition playerPosition = new PlayerPosition(playerPositionRow, playerPositionColumn); bool isPlayerPositionInLabyrinthRange = IsPlayerPositionInLabyrinthRange(labyrinthMap, playerPosition); bool isLabyrinthCellFree = labyrinthMap[playerPositionColumn, playerPositionRow] == 0; if (!isPlayerPositionInLabyrinthRange || !isLabyrinthCellFree) { bool labyrintExitExists = false; return labyrintExitExists; } else { bool isPlayerPositionAtLabyrinthBorder = IsPlayerPositionAtLabyrintBorder(labyrinthMap, playerPosition); if (isPlayerPositionAtLabyrinthBorder) { BlockVisitedLabyrinthCell(labyrinthMap, playerPositionColumn, playerPositionRow); bool labyrintExitExists = true; return labyrintExitExists; } else { BlockVisitedLabyrinthCell(labyrinthMap, playerPositionColumn, playerPositionRow); bool labyrintExitExistsInLeft = LabyrintExitExists( labyrinthMap, playerPositionRow - 1, playerPositionColumn); bool labyrintExitExistsInRight = LabyrintExitExists( labyrinthMap, playerPositionRow + 1, playerPositionColumn); bool labyrintExitExistsInUp = LabyrintExitExists( labyrinthMap, playerPositionRow, playerPositionColumn - 1); bool labyrintExitExistsInDown = LabyrintExitExists( labyrinthMap, playerPositionRow, playerPositionColumn + 1); bool labyrintExitExists = labyrintExitExistsInLeft || labyrintExitExistsInRight || labyrintExitExistsInUp || labyrintExitExistsInDown; return labyrintExitExists; } } }
/// <summary> ///Plays the game using the user's commands /// </summary> public void Play() { do { PlayerPosition playerPreviousPosition = new PlayerPosition( this.playerCurrentPosition.Row, this.playerCurrentPosition.Column); string enterMoveMessage = this.resourcesList.GetResourceValue("EnterMoveMessage"); Console.Write(enterMoveMessage); string userInput = Console.ReadLine(); try { UserCommand userCommand = this.ParseUserCommand(userInput); this.ExecuteUserCommand(userCommand); } catch (InvalidCommandException) { this.PrintInvalidCommandMessage(); continue; } if (this.IsPlayerPositionChanged(playerPreviousPosition)) { this.UpdateLabyrinthAfterMovement(); } } while (this.isGameActive); }
public void TestIsPlayerPositionInLabyrinthRange_PlayerPositionAtTopBorder_ReturnsTrue() { int[,] labyrinthMap = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, }; PlayerPosition playerStartPosition = new PlayerPosition(1, 0); bool isPlayerPositionInRange = GameUtilities.IsPlayerPositionInLabyrinthRange(labyrinthMap, playerStartPosition); Assert.IsTrue(isPlayerPositionInRange); }
private void MoveUp() { PlayerPosition playerNextPosition = new PlayerPosition(0, 0); this.playerCurrentPosition.CopyTo(playerNextPosition); playerNextPosition.MoveUp(); if (this.IsValidMovement(playerNextPosition)) { this.playerCurrentPosition.MoveUp(); } else { throw new InvalidCommandException("Invalid movement"); } }
private bool IsValidMovement(PlayerPosition playerNextPosition) { bool isPlayerPositionInRange = GameUtilities.IsPlayerPositionInLabyrinthRange( this.labyrinthMap, playerNextPosition); bool isValidMove = (isPlayerPositionInRange && this.labyrinthMap[playerNextPosition.Row, playerNextPosition.Column] == 0); return isValidMove; }
public void TestIsPlayerPositionInLabyrinthRange_PlayerPositionOutRange_ReturnsFalse() { int[,] labyrinthMap = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, }; PlayerPosition playerStartPosition = new PlayerPosition(3, 3); bool isPlayerPositionInRange = GameUtilities.IsPlayerPositionInLabyrinthRange(labyrinthMap, playerStartPosition); Assert.IsFalse(isPlayerPositionInRange); }