/// Author: Max Hamulyak /// Date: 24-06-2015 /// <summary> /// Given a token, this method builds a <see cref="ConcreteInstruction"/> /// </summary> /// <returns>The logic instruction from token.</returns> /// <param name="token">Token.</param> private ICondition ParseLogicInstructionFromToken(Token token) { ICondition condition; if (token.Value.StartsWith ("at('")) { int quoteIndex = token.Value.IndexOf ("'"); String desiredObject = token.Value.Substring (quoteIndex + 1); desiredObject = desiredObject.TrimEnd ("')".ToCharArray ()); condition = new ConcreteInstruction (ECanInstructions.At, desiredObject); } else { switch (token.Value) { case "left": condition = new ConcreteInstruction (ECanInstructions.Left); break; case "right": condition = new ConcreteInstruction (ECanInstructions.Right); break; case "forward": condition = new ConcreteInstruction (ECanInstructions.Forward); break; case "backward": condition = new ConcreteInstruction (ECanInstructions.Backward); break; default: condition = null; break; } } return condition; }
public void whileLoop3() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); Solver concreteInstruction1 = new ConcreteInstruction (ECanInstructions.Forward); //A Solver concreteInstruction2 = new ConcreteInstruction (ECanInstructions.Forward); //A Solver notInstruction = new ConditionCombo (concreteInstruction1, ELogicOperators.Not); //!A Solver combo = new ConditionCombo (notInstruction, concreteInstruction2, ELogicOperators.And); // !A && A ICodeBlock command = new TurnRight (); WhileLoop whileLoop = new WhileLoop (combo); whileLoop.addChild (command); whileLoop.execute (null); EOrientation actual = robot.orientationEnum; EOrientation expected = EOrientation.East; Assert.AreEqual (expected, actual); }
public void whileLoop4() { EOrientation orientation = EOrientation.South; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); robot.xPosition = 1; Solver canForward = new ConcreteInstruction (ECanInstructions.Forward); ICodeBlock command = new Forward(); WhileLoop whileLoop = new WhileLoop (canForward); whileLoop.addChild (command); MainCode mainCode = new MainCode (); mainCode.addChild (whileLoop); mainCode.execute (); int actual = robot.yPosition; int expected = 2; Assert.AreEqual (expected, actual); }
public void whileLoop2() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); Solver conditions = new ConcreteInstruction (ECanInstructions.Backward); WhileLoop whileLoop = new WhileLoop (conditions); bool actual = whileLoop.execute (null); bool expected = true; Assert.AreEqual (expected, actual); }
public void whileLoop1() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); robot.xPosition = 1; robot.yPosition = 1; Solver condition1 = new ConcreteInstruction (ECanInstructions.Right); Solver condition2 = new ConcreteInstruction (ECanInstructions.Forward); Solver combo = new ConditionCombo (condition1, condition2, ELogicOperators.Or); WhileLoop whileLoop = new WhileLoop (combo); bool actual = whileLoop.execute (null); bool expected = false; Assert.AreEqual (expected, actual); }
public void SolverOr2() { Solver leftSolver = new ConcreteInstruction (ECanInstructions.Backward); Solver rightSolver = new ConcreteInstruction (ECanInstructions.Backward); Solver combo = new ConditionCombo (leftSolver, rightSolver, ELogicOperators.Or); EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); bool actual = combo.solve (null); bool expected = false; Assert.AreEqual (expected, actual); }
public void SolverBasic() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); Solver concreteInstruction = new ConcreteInstruction (ECanInstructions.Forward); bool actual = concreteInstruction.solve (null); bool expected = true; Assert.AreEqual (expected, actual); }
public void ifStatement2() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); robot.xPosition = 0; robot.yPosition = 0; Solver conditions = new ConcreteInstruction (ECanInstructions.Backward); ICodeBlock command = new TurnRight (); IfStatement ifStatement = new IfStatement (conditions); ifStatement.ElseChildren.Add (command); ifStatement.execute (null); EOrientation actual = robot.orientationEnum; EOrientation expected = EOrientation.South; Assert.AreEqual (expected, actual); }