/// <summary> /// Check the node is visited or not /// </summary> /// <param name="coord"></param> /// <returns></returns> private bool CheckCoordNodeUnVisited(FarmFieldCoord coord) { if (coord.isVisited) { return(false); } coord.isVisited = true; return(true); }
/// <summary> /// Create total available land as per th provided X and Y max. values /// </summary> public void CreateTotalLand() { for (int i = 0; i < X_MAX; i++) { for (int j = 0; j < Y_MAX; j++) { FarmFieldCoord co = new FarmFieldCoord(i, j); totalLand[i, j] = co; } } }
/// <summary> /// Get the avialable fertile lands /// </summary> public void GetConnectedFertileLands() { for (int i = 0; i < X_MAX; i++) { for (int j = 0; j < Y_MAX; j++) { FarmFieldCoord coord = totalLand[i, j]; if (!coord.isVisited) { int fertileLand = FillFertileArea(i, j); if (fertileLand != 0) { fertileAreaMap.Add(fertileLand); } } } } }
/// <summary> /// Fill the fertile area from the start point of x and y /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns>return connected fertile land</returns> private int FillFertileArea(int x, int y) { int fertileCoord = 0; Queue <FarmFieldCoord> coordQueue = new Queue <FarmFieldCoord>(); coordQueue.Enqueue(totalLand[x, y]); while (coordQueue.Count > 0) { FarmFieldCoord coord = coordQueue.Dequeue(); if (CheckCoordNodeUnVisited(coord)) { if (coord.IsBarren) { continue; } fertileCoord++; if (coord.X > 0 && !totalLand[coord.X - 1, coord.Y].isVisited) { coordQueue.Enqueue(totalLand[coord.X - 1, coord.Y]); } if (coord.X < X_MAX - 1 && !totalLand[coord.X + 1, coord.Y].isVisited) { coordQueue.Enqueue(totalLand[coord.X + 1, coord.Y]); } if (coord.Y > 0 && !totalLand[coord.X, coord.Y - 1].isVisited) { coordQueue.Enqueue(totalLand[coord.X, coord.Y - 1]); } if (coord.Y < Y_MAX - 1 && !totalLand[coord.X, coord.Y + 1].isVisited) { coordQueue.Enqueue(totalLand[coord.X, coord.Y + 1]); } } } return(fertileCoord); }