private bool IsOutside(int x, int y, JellyTank objJellyTank) { bool isOut = false; if (x > objJellyTank.MaxX || x < 0 || y > objJellyTank.MaxY || y < 0) { isOut = true; } return(isOut); }
private void MoveFoward(JellyTank objJellyTank) { var nextPosition = NextPosition(); if (IsOutside(nextPosition.X, nextPosition.Y, objJellyTank) && !objJellyTank.IsScent(X, Y)) { this.IsLost = true; objJellyTank.AddScentedCoordinates(new Coordinate(X, Y)); } else { X = nextPosition.X; Y = nextPosition.Y; } }
public void RunInstruction(Instruction instruction, JellyTank objJellyTank) { if (!IsLost) { switch (instruction) { case Instruction.forward: MoveFoward(objJellyTank); break; case Instruction.right: MoveRight(); break; case Instruction.left: MoveLeft(); break; } } }
public List <Jelly> ExtractInputAndGenerateResult(string inputValue) { List <Jelly> lstJelly = new List <Jelly>(); var inputArr = inputValue.Split( new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries ).ToArray(); int range = Convert.ToInt32(inputArr[0]); int maxX = range / 10; int maxY = range % 10; for (int i = 1; i < inputArr.Length; i++) { //jellyfish loc and orientation //considering only one row right now. string jellyFishPositionDirectionAndInstruction = inputArr[i]; if (!string.IsNullOrEmpty(jellyFishPositionDirectionAndInstruction)) { string jellyFishPosition = jellyFishPositionDirectionAndInstruction.Split()[0]; string jellyFishCoordinate = new string(jellyFishPosition.Where(Char.IsDigit).ToArray()); string jellyFishOrientation = jellyFishPosition.Last().ToString(); string jellyFishInstruction = jellyFishPositionDirectionAndInstruction.Split()[1]; int jellyFishCoordinateX = Convert.ToInt32(jellyFishCoordinate) / 10; int jellyFishCoordinateY = Convert.ToInt32(jellyFishCoordinate) % 10; //Create Jelly tank object var objJellyTank = new JellyTank(maxX, maxY); //Create Jelly object var objJelly = new Jelly(jellyFishCoordinateX, jellyFishCoordinateY, jellyFishOrientation); char[] instructionArr = jellyFishInstruction.ToCharArray(); foreach (char inst in instructionArr) { objJelly.RunInstruction(GetInstruction(inst.ToString()), objJellyTank); } lstJelly.Add(objJelly); } } return(lstJelly); }