/// <summary> /// Checks if the MergeableUnit is in a position to merge (into a Colony if it /// is a cell, or into a Plant or Animal if it is a Colony). /// </summary> /// <param name="grid"> The grid of Units currently in the simulation </param> /// <param name="row"> The row of the grid that this MergeableUnit resides in. </param> /// <param name="col"> The column of the grid that this MergeableUnit resides in. </param> /// <returns> True if the MergeableUnit is the top left of a 2x2 square with other /// MergeableUnits of the same species and should merge, false otherwise. </returns> protected bool ShouldMerge(Unit[,] grid) { // Get the type of this unit -- must merge with units of the same species var curType = this.GetType(); // Get the location of the current unit int row = Location.r, col = Location.c; // if the cell is not in a space capable of forming a 2x2 square, it cannot merge if (!grid.InDimension(GridHelper.ROW, row + 1) || !grid.InDimension(GridHelper.COLUMN, col + 1)) { return(false); } // otherwise, if the surrounding 3 grids cells are of the same type, this unit should merge else if (grid[row, col + 1]?.GetType() == curType && grid[row + 1, col]?.GetType() == curType && grid[row + 1, col + 1]?.GetType() == curType) { return(true); } // otherwise, this unit does not meet the locational requirements to evolve else { return(false); } }
/// <summary> /// Checks if it is possible, given the grid and the Colony's location, for the Colony /// to split into 4 Cells in a 2x2 square in the given direction. /// </summary> /// <remarks> /// Author: Rudy Ariaz /// </remarks> /// <param name="grid">The grid in which the Colony is.</param> /// <param name="rowDirection">The vertical direction of splitting. +1 if the split /// should occur downwards, -1 if the split should occur upwards.</param> /// <param name="colDirection">The horizontal direction of splitting. +1 if the split /// should occur to the right, -1 if the split should occur to the left.</param> /// <returns>True if the split in the given direction is possible, false otherwise..</returns> private bool IsSplitPossible(Unit[,] grid, int rowDirection, int colDirection) { // Check for invalid arguments (the directions must have an absolute value of 1) if (Math.Abs(rowDirection) > 1 || Math.Abs(colDirection) > 1) { // Indicate that the split is not possible return(false); } // Otherwise, get the current row and column int row = Location.r, col = Location.c; // Check if the farthest newly created cell is still within the grid, in which // case the split is possible. return(grid.InDimension(GridHelper.ROW, row + rowDirection) && grid.InDimension(GridHelper.COLUMN, col + colDirection)); }
/// <summary> /// Checks whether a given location is within the gird /// </summary> /// <param name="grid">The unit grid</param> /// <param name="row">The row index of the location</param> /// <param name="col">The column index of the location</param> /// <returns> /// True if the location is within the grid /// False if the location is not within the grid</returns> public static bool InGridBounds(this Unit[,] grid, int row, int col) { return(grid.InDimension(ROW, row) && grid.InDimension(COLUMN, col)); }