diff options
author | Alexander Kauerz <akauerz@gmail.com> | 2012-04-27 17:57:04 +0200 |
---|---|---|
committer | Alexander Kauerz <akauerz@gmail.com> | 2012-04-27 17:57:04 +0200 |
commit | 88c86f6cf634f5bf7ff797f0e54f486445ab2b95 (patch) | |
tree | 85503712992485168a3f3eeda1643051d2d22f01 /Matrix_9x14/Ant/Ant.cpp | |
parent | c35542703af7dc50241da217ce00f15e787ab979 (diff) | |
download | ledmatrix-master.tar ledmatrix-master.zip |
Diffstat (limited to 'Matrix_9x14/Ant/Ant.cpp')
-rw-r--r-- | Matrix_9x14/Ant/Ant.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/Matrix_9x14/Ant/Ant.cpp b/Matrix_9x14/Ant/Ant.cpp new file mode 100644 index 0000000..0fb7f13 --- /dev/null +++ b/Matrix_9x14/Ant/Ant.cpp @@ -0,0 +1,107 @@ +/* +Langtongs Ant +Eine Kooperationsarbeit von Kristof und Alexander +Version: 0.1 +*/ + +#include <Arduino.h> +#include <Charliplexing.h> //Imports the library, which needs to be + //Initialized in setup. + +#define DELAY 10 //Sets the time each generation is shown +#define SIZEX 14 //Sets the X axis size +#define SIZEY 9 //Sets the Y axis size +#define DIRECTIONS 4 //Defines the amount of dircetions possible +bool world[SIZEX][SIZEY][2]; //Creates a double buffer world +long density = 25; //Sets density % during seeding + +enum direction { + NORTH, + EAST, + SOUTH, + WEST +}; + +struct Ant { + int x; + int y; + direction orientation; +} ant; + +void step(){ + switch (ant.orientation) { + case NORTH: ant.y=(ant.y+1)%SIZEY; + break; + case EAST: ant.x=(ant.x+SIZEX-1)%SIZEX; + break; + case SOUTH: ant.y=(ant.y+SIZEY-1)%SIZEY; + break; + case WEST: ant.x=(ant.x+1)%SIZEX; + break; + } +} + +void turn(){ + ant.orientation=(direction)((world[ant.x][ant.y][0])? + (((int)ant.orientation)+DIRECTIONS-1)%DIRECTIONS : + (((int)ant.orientation)+1)%DIRECTIONS); +} + +void toggle(){ + world[ant.x][ant.y][0]=!world[ant.x][ant.y][0]; +} + + +void cycle(){ + step(); + turn(); + toggle(); +} + +//Re-seeds based off of RESEEDRATE +void seedWorld(){ + randomSeed(analogRead(5)); + for (int i = 0; i < SIZEX; i++) { + for (int j = 0; j < SIZEY; j++) { + if (random(100) < density) { + world[i][j][1] = 1; + } + } + } +} + +void setup() { + LedSign::Init(); //Initilizes the LoL Shield + ant.x = (SIZEX/2); + ant.y = (SIZEY/2); + ant.orientation = NORTH; + + randomSeed(analogRead(5)); + //Builds the world with an initial seed. + for (int i = 0; i < SIZEX; i++) { + for (int j = 0; j < SIZEY; j++) { + world[i][j][0] = 0; + world[i][j][1] = 0; + // world[i][j][0] = 0; + } + } +} + + + +void loop() { + + cycle(); + + for (int x = 0; x < SIZEX; x++) { + for (int y = 0; y < SIZEY; y++) { + LedSign::Set(x,y,world[x][y][0]); + } + } + + + delay(DELAY); +} + + + |