/// <summary> /// HasValidExit determines if a GridPoint on the grid has a valid exit and /// returns true if it does. /// When the last square has been set, this method should return false. /// </summary> /// <param name="gridPoint">the GridPoint to check exits from</param> /// <returns>true if a valid exit exists, otherwise false</returns> private bool HasValidExit(GridPoint gridPoint) { //Test up GridPoint newPoint = new GridPoint(gridPoint.Row, gridPoint.Column); newPoint.Move(GridPoint.Direction.UP); if ((IsValid(newPoint)) && (UnionPossible(gridPoint, newPoint))) { return(true); } //Test down newPoint = new GridPoint(gridPoint.Row, gridPoint.Column); newPoint.Move(GridPoint.Direction.DOWN); if ((IsValid(newPoint)) && (UnionPossible(gridPoint, newPoint))) { return(true); } //Test left newPoint = new GridPoint(gridPoint.Row, gridPoint.Column); newPoint.Move(GridPoint.Direction.LEFT); if ((IsValid(newPoint)) && (UnionPossible(gridPoint, newPoint))) { return(true); } //Test right newPoint = new GridPoint(gridPoint.Row, gridPoint.Column); newPoint.Move(GridPoint.Direction.RIGHT); if ((IsValid(newPoint)) && (UnionPossible(gridPoint, newPoint))) { return(true); } return(false); }
/// <summary> /// ValidDirection determines if a randomly generated direction from the /// initial starting GridPoint is valid, and if the new GridPoint found there can /// be connected (union) with the initial GridPoint. /// </summary> /// <param name="randomNumber">a random number generated to choose direction</param> /// <param name="gridPoint">the GridPoint to choose a direction from</param> /// <returns>the new GridPoint object if it is valid, otherwise null</returns> private GridPoint ValidDirection(int randomNumber, GridPoint gridPoint) { //Initialises newPoint to the value of GridPoint GridPoint newPoint = new GridPoint(gridPoint.Row, gridPoint.Column); //Moves the new GridPoint in a direction, depending upon which random //direction is indicated if (randomNumber == 0) { newPoint.Move(GridPoint.Direction.UP); } else if (randomNumber == 1) { newPoint.Move(GridPoint.Direction.RIGHT); } else if (randomNumber == 2) { newPoint.Move(GridPoint.Direction.DOWN); } else if (randomNumber == 3) { newPoint.Move(GridPoint.Direction.LEFT); } else { Console.WriteLine("Error in random number"); return(null); } //If the new GridPoint is valid, and union is possible with the first GridPoint //return the new GridPoint if ((IsValid(newPoint)) && (UnionPossible(gridPoint, newPoint))) { return(newPoint); } return(null); }
/// <summary> /// GetDirection gets the direction to another point which is assumed to /// be at perpendicular offset from this object. /// </summary> /// <param name="target">the target GridPoint to compare with</param> /// <returns></returns> public GridPoint.Direction GetDirection(GridPoint target) { if (target.row < row) { return(Direction.UP); } else if (target.row > row) { return(Direction.DOWN); } else if (target.col < col) { return(Direction.LEFT); } else if (target.col > col) { return(Direction.RIGHT); } else { return(Direction.ERROR); } }
/// <summary> /// IsValid returns if a GridPoint is within the limits of the grid. /// </summary> /// <param name="p">a grid point</param> /// <returns>true if within the grid limits, false if not</returns> private bool IsValid(GridPoint p) { return(p.Row >= 0 && p.Row < maxRows && p.Column >= 0 && p.Column < maxColumns); }
/// <summary> /// GetSquareId gets the integer id of a grid square from the row and /// column of a GridPoint. /// </summary> /// <param name="p">a grid point object</param> /// <returns>an integer id representing square position</returns> private int GetSquareId(GridPoint p) { return(p.Row * maxColumns + p.Column); }