/* START MAIN.INO */
/******************************************/
/* START CONFIG */
/* END CONFIG */
/******************************************/
/* START CODE */
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(10, 9);
int grid[17][17] = {{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
// 221 x 221
// 13 x 13
// 17 x 17
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
int done = 0;
int x = 0;
int y = 0;
for (int i = 0; done != 1; i += 0) {
int fruitx = rand(0, 17);
int fruity = rand(0, 17);
if (grid[fruity][fruitx] != 1 && grid[fruity][fruitx] != 2) {
x = fruitx;
y = fruity;
done = 1;
}
}
grid[y][x] = 1;
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(2, INPUT_PULLUP);
}
int pv = 0;
int ph = 1;
int headmove = 0;
void loop() {
int h = analogRead(A1) < 300 ? 1 : analogRead(A1) > 700 ? -1 : 0;
int v = analogRead(A0) < 300 ? 1 : analogRead(A0) > 700 ? -1 : 0;
int d = digitalRead(2) == 1 ? 0 : 1;
headmove = 0;
if (h != 0 && v == 0) {
if (h == 1 && ph != -1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((x + 1) > 17) {
} else if (headmove == 0) {
headmove = 1;
grid[y][x + 1] = grid[y][x];
grid[y][x] = 0;
pv = 0;
ph = 1;
}
}
}
}
}
} else if (h == -1 && ph != 1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((x - 1) < 0) {
} else if (headmove == 0) {
headmove = 1;
grid[y][x - 1] = grid[y][x];
grid[y][x] = 0;
pv = 0;
ph = -1;
}
}
}
}
}
}
} else if (h == 0 && v != 0) {
if (v == 1 && pv != -1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((y + 1) > 17) {
} else if (headmove == 0) {
headmove = 1;
grid[y + 1][x] = grid[y][x];
grid[y][x] = 0;
pv = 1;
ph = 0;
}
}
}
}
}
} else if (v == -1 && pv != 1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((y - 1) < 0) {
} else if (headmove == 0) {
headmove = 1;
grid[y - 1][x] = grid[y][x];
grid[y][x] = 0;
pv = -1;
ph = 0;
}
}
}
}
}
}
} else {
if (ph != 0 && pv == 0) {
if (ph == 1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((x + 1) > 17) {
} else if (headmove == 0) {
headmove = 1;
grid[y][x + 1] = grid[y][x];
grid[y][x] = 0;
}
}
}
}
}
} else if (ph == -1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((x - 1) < 0) {
} else if (headmove == 0) {
headmove = 1;
grid[y][x - 1] = grid[y][x];
grid[y][x] = 0;
}
}
}
}
}
}
} else if (ph == 0 && pv != 0) {
if (pv == 1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((y + 1) > 17) {
} else if (headmove == 0) {
headmove = 1;
grid[y + 1][x] = grid[y][x];
grid[y][x] = 0;
}
}
}
}
}
} else if (pv == -1) {
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
if (grid[y][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x + 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x - 1] == (grid[y][x] + 1)) {
} else if (grid[y + 1][x] == (grid[y][x] + 1)) {
} else if (grid[y - 1][x] == (grid[y][x] + 1)) {
} else {
if ((y - 1) < 0) {
} else if (headmove == 0) {
headmove = 1;
grid[y - 1][x] = grid[y][x];
grid[y][x] = 0;
}
}
}
}
}
}
}
}
for (int y = 0; y < 17; y += 1) {
for (int x = 0; x < 17; x += 1) {
if (grid[y][x] > 1) {
tft.fillRect((x * 13), (y * 13), 13, 13, 0x07e0);
} else if (grid[y][x] == 1) {
tft.fillRect((x * 13), (y * 13), 13, 13, 0xf800);
} else {
tft.fillRect((x * 13), (y * 13), 13, 13, 0x0000);
}
}
}
delay(100);
}
/* END CODE */
/******************************************/
/* END MAIN.INO */