public void forLoop1() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); MainCode mainCode = new MainCode (); // Create (int i = 0; i < 5; i++) //Create i ConcreteVariable iVar = new ConcreteVariable (new Variable (0, EVariableType.Int)); DefineVariable iDefine = new DefineVariable (iVar); CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine); //create a variable thats always 5 Variable max = new Variable (5, EVariableType.Int); VariableSolver maxS = new ConcreteVariable (max); //Create i < 5 ValueSolver solver = new ValueSolver (iVar, maxS, EComparisonOperator.ValueLessThan); // Create i++ ConcreteVariable staticOneVar = new ConcreteVariable (new Variable (1, EVariableType.Int)); ConcreteVariable iGather = new ConcreteVariable ("i"); // Gathers i, this creates a reference to iVar VariableSolver iPlusOneSolver = new VariableCombo (iGather, staticOneVar, EMathOperator.Add); DefineVariable iPlusOneDefine = new DefineVariable (iPlusOneSolver); CmdDefineVariable iPlusOneSolverCommand = new CmdDefineVariable ("i", iPlusOneDefine); Composite forLoop = new ForLoop (solver, iDefineCommand, iPlusOneSolverCommand); mainCode.addChild (forLoop); forLoop.addChild (new TurnRight ()); mainCode.execute (); EOrientation actual = robot.orientationEnum; EOrientation expected = EOrientation.South; 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 forLoop2() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); MainCode mainCode = new MainCode (); // Create (int i = 1; i < 5; i*i) //Create i ConcreteVariable iVar = new ConcreteVariable (new Variable (1, EVariableType.Int)); DefineVariable iDefine = new DefineVariable (iVar); CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine); //create a variable thats always 2 Variable staticTwoVar = new Variable (2, EVariableType.Int); VariableSolver staticTwoVarS = new ConcreteVariable (staticTwoVar); // Create i*i ConcreteVariable iGather = new ConcreteVariable ("i"); // Gathers i, this creates a reference to iVar VariableSolver ixiSolver = new VariableCombo (iGather, staticTwoVarS, EMathOperator.Multiply); DefineVariable ixiDefine = new DefineVariable (ixiSolver); CmdDefineVariable ixiSolverCommand = new CmdDefineVariable ("i", ixiDefine); //create a variable thats always 5 Variable max = new Variable (5, EVariableType.Int); VariableSolver maxS = new ConcreteVariable (max); //Create i < 4 ValueSolver solver = new ValueSolver (iGather, maxS, EComparisonOperator.ValueLessThan); ForLoop forLoop = new ForLoop (solver, iDefineCommand, ixiSolverCommand); //forLoop.variables["i"] = new Variable(1, EVariableType.Int); mainCode.addChild (forLoop); forLoop.addChild (new TurnRight ()); mainCode.execute (); EOrientation actual = robot.orientationEnum; EOrientation expected = EOrientation.North; Assert.AreEqual (expected, actual); }
public void variableStringSubstract() { MainCode mainCode = new MainCode (); // create i = 10 ConcreteVariable iVar = new ConcreteVariable (new Variable ("10", EVariableType.String)); DefineVariable iDefine = new DefineVariable (iVar); CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine); // create x = 5; ConcreteVariable xVar = new ConcreteVariable (new Variable (5, EVariableType.Int)); DefineVariable xDefine = new DefineVariable (xVar); CmdDefineVariable xDefineCommand = new CmdDefineVariable ("x", xDefine); // Gather the variables ConcreteVariable iGather = new ConcreteVariable ("i"); ConcreteVariable xGather = new ConcreteVariable ("x"); // calculate and put the values in ii VariableSolver calculation = new VariableCombo (iGather, xGather, EMathOperator.Subtract); DefineVariable calcCommand = new DefineVariable (calculation); CmdDefineVariable executeCalculation = new CmdDefineVariable ("ii", calcCommand); mainCode.addChild (xDefineCommand); mainCode.addChild (iDefineCommand); mainCode.addChild (executeCalculation); mainCode.execute (); //ConcreteVariable iiGather = new ConcreteVariable ("ii"); }
public void variableStringAdd() { MainCode mainCode = new MainCode (); // create i = 10 ConcreteVariable iVar = new ConcreteVariable (new Variable ("10", EVariableType.String)); DefineVariable iDefine = new DefineVariable (iVar); CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine); // create x = 5; ConcreteVariable xVar = new ConcreteVariable (new Variable (5, EVariableType.Int)); DefineVariable xDefine = new DefineVariable (xVar); CmdDefineVariable xDefineCommand = new CmdDefineVariable ("x", xDefine); // Gather the variables ConcreteVariable iGather = new ConcreteVariable ("i"); ConcreteVariable xGather = new ConcreteVariable ("x"); // calculate and put the values in ii VariableSolver calculation = new VariableCombo (iGather, xGather, EMathOperator.Add); DefineVariable calcCommand = new DefineVariable (calculation); CmdDefineVariable executeCalculation = new CmdDefineVariable ("ii", calcCommand); mainCode.addChild (xDefineCommand); mainCode.addChild (iDefineCommand); mainCode.addChild (executeCalculation); mainCode.execute (); //ConcreteVariable iiGather = new ConcreteVariable ("ii"); string expected = "105"; string actual = mainCode.variables["ii"].Value; Assert.AreEqual (expected, actual); }
public void ExecuteCommand(List<ICodeBlock> command, string text, UILabel lblExecutedCode) { try { MainCode m = new MainCode(); foreach (var item in command) { m.addChild(item); } m.execute(null); lblExecutedCode.Text = "Code completed"; } catch (RobotException ex) { throw ex; } catch (MapException ex) { throw ex; } }
/// <summary> /// Executes the commands. /// </summary> /// <param name="command">Command.</param> /// <param name="text">Text.</param> /// <created>Stef Chappin</created> private static void ExecuteCommand(List<ICodeBlock> command, string text) { try { MainCode m = new MainCode(); foreach (var item in command) { m.addChild(item); } m.execute(null); } catch (RobotException excRobot) { throw excRobot; } catch (CodeParseException excCodeParse) { throw excCodeParse; } catch (SyntaxParseException excSyntax) { throw excSyntax; } catch (MapException excMap) { throw excMap; } catch (ArgumentOutOfRangeException excArgOOR) { throw excArgOOR; } }