Add random

master
rubenwardy 2016-02-21 21:33:58 +00:00
parent 17004c1885
commit 09ee4010c8
2 changed files with 23 additions and 0 deletions

6
examples/dice.rf Normal file
View File

@ -0,0 +1,6 @@
v
0
> 2. @
> .0 ? 1. @
> 3. @

View File

@ -2,6 +2,8 @@
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
class DirSR : public Subroutine class DirSR : public Subroutine
{ {
@ -220,6 +222,17 @@ public:
} }
}; };
class RandSR : public Subroutine
{
public:
virtual void run(Thread *th)
{
EDIR dir = (EDIR)(rand() % 4);
th->setDir(dir);
th->move();
}
};
class TemplateSR : public Subroutine class TemplateSR : public Subroutine
{ {
public: public:
@ -269,12 +282,15 @@ void VM::assignStandardSR(Thread *th)
c->operators['%'] = 13; c->operators['%'] = 13;
c->operators['!'] = 14; c->operators['!'] = 14;
c->operators['`'] = 15; c->operators['`'] = 15;
c->operators['?'] = 16;
// TODO: ? # [ ] \ $ & ~ g p M P R L // TODO: ? # [ ] \ $ & ~ g p M P R L
} }
void VM::init(Canvas *canvas) void VM::init(Canvas *canvas)
{ {
srand (time(NULL));
assert(threads.size() == 0); assert(threads.size() == 0);
assert(subroutines.size() == 0); assert(subroutines.size() == 0);
@ -298,6 +314,7 @@ void VM::init(Canvas *canvas)
loadSubroutine(new ModSR); loadSubroutine(new ModSR);
loadSubroutine(new NotSR); loadSubroutine(new NotSR);
loadSubroutine(new GtSR); loadSubroutine(new GtSR);
loadSubroutine(new RandSR);
assignStandardSR(th); assignStandardSR(th);
} }