From 88c86f6cf634f5bf7ff797f0e54f486445ab2b95 Mon Sep 17 00:00:00 2001 From: Alexander Kauerz Date: Fri, 27 Apr 2012 17:57:04 +0200 Subject: Langtons Ant impementiert. --- Matrix_9x14/Ant/Ant.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Matrix_9x14/Ant/Ant.cpp (limited to 'Matrix_9x14/Ant/Ant.cpp') 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 +#include //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); +} + + + -- cgit v1.2.3