/* Moves an Entity into the Grid if it's location is null. * * PRECONDITION: grid != null; * PRECONDITION: this.location == null; * PARAMETER: loc must be valid in the Grid * POSTCONDITION: grid.Get(location) == this; */ public void MoveFromSideline(int x, int y) { if (Grid.IsValid(x, y)) { Grid.Set(this, x, y); this.Location = new Location(x, y); } else { throw new ArgumentException("Given Location must be Valid!"); } }
/* Moves this Entity to another Location within the Grid. * Removes whatever was at its original position. * Returns the Entity that was originally occupying that Location, null if there was nothing there. * * PRECONDITION: Grid and Location != null; * PARAMETER: grid.IsValid(x, y) == true; * POSTCONDITION: grid.Get(x, y) == this; * POSTCONDITION: grid.Get(oldLoc) == null; */ public Entity MoveTo(int x, int y) { if (Grid.IsValid(x, y)) { Grid.Remove(Location); this.Location = new Location(x, y); return(Grid.Set(this, this.Location)); } else { throw new Exception("Location " + x + ", " + y + "was invalid!"); } }
/* Private helper that acts as a highly variable for loop. * Checks all Locations dictated by its loop values and returns the valid ones in a list. */ private List <Location> GetLocations(int initial, int final, int step, Grid g) { List <Location> locs = new List <Location>(); for (int i = initial; i <= final; i += step) { Location adj = this.GetAdjacentLocation(i); if (g.IsValid(adj)) { locs.Add(adj); } } return(locs); }