Pages

Wednesday, March 22, 2017

Algo Code Ep 2 - Setting Up Part 2

Hello readers!

This is the second episode of Algo Code. Let's continue to startup Algo Code.

We can test Algo Code using Program Test.

In Program Test, copy this and save it as Test 1 algorithm.txt .
_________________________________________________________________________________
START
  SET start=false
  SET n=0
  REPEAT
    IF start=true AND n<100
      ADD the value of n with 1
    END IF
  UNTIL start=false
  FOREVER
    CLEAR canvas
    IF n=100
      SET n=0
      SET start=false
    END IF
    SHOW n
  END FOREVER
END
_________________________________________________________________________________
This is the algorithm for Test 1.

Next, this is Test 1 code.
_________________________________________________________________________________
var start=false;
var n=0;

update=function() {
cnvscls();
text(n,60,60);
if(start==true && n<100) {
n+=1;
}
if(n==100) {
n=0;
start=false;
conscls();
}
}

setInterval(update,15);
_________________________________________________________________________________

It is the algorithm for Test 1.

Copy and paste Test 1 into MAIN.js and run app.html . Open console. Oh  yeah! Algo Code is best to be used with Google Chrome. Anyway, in console, type 'start=true' (without the quotes). The 0 on the canvas should increase by 1 until it reaches 99 and the console will clear it self.

For Test 2, save this as Test 2 algorithm.
_________________________________________________________________________________
START
  SET a=0
  SET b=0
  SET c=0
FOREVER
    CLEAR canvas
    IF pressingUp=true
      a=a+1
    END IF

    IF pressingDown=true
      a=a-1
    END IF

    IF pressingLeft=true
      b=b-1
    END IF

    IF pressingRight=true
      b=b+1
    END IF

    SHOW a AND b
    SHOW '+'
    SET c=a+b
    SHOW '='
    SHOW c
  END FOREVER
END
_________________________________________________________________________________

Next, save this code as Test 2 code.
_________________________________________________________________________________
var a=0;
var b=0;
var c=0;

update=function() {
if(pressingUp==true) {
a+=1;
}
if(pressingDown==true) {
a-=1;
}
if(pressingLeft==true) {
b-=1;
}
if(pressingRight==true) {
b+=1;
}

text(a,80,110);
text('+',150,110);
text(b,240,110);
c=a+b;
text('=',150,180);
text(c,150,260);
}

setInterval(update,15);
_________________________________________________________________________________

Like earlier, run app.html . This time you do not need to use console. Just use the arrow keys. The Up arrow key will increase the number on the left and the Down arrow key will decrease it. The Left arrow key will decrease the number on the right and the Right arrow key will increase the number on the right.

This is how to set up Algo Code.

Thanks for eating.

Continue in Ep 3.

22-03-2017

No comments:

Post a Comment