summaryrefslogtreecommitdiffstats
path: root/Matrix_9x14/Ant/Ant.cpp
blob: 0fb7f135885d3ec8f3d2ca9e1c688a8e0aef8e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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);
}