const int ButtonUp = 2;
const int ButtonDown = 3;
const int ButtonLeft = 4;
const int ButtonRight = 5;
int pos[2];
int MyPosX = 1;
int MyPosY = 1;
int GoingUp = 0;
int GoingDown = 0;
int GoingLeft = 0;
int GoingRight = 0;
int lastGoingUp = 0;
int lastGoingDown = 0;
int lastGoingLeft = 0;
int lastGoingRight = 0;
int MX[43] = {3,3,4,4,4,5,5,6,7,8,10,10,11,11,11,12,12,12,12,13,13,13,13,14,14,14,14,17,18,18,19,19,19,19,19,19,19,20,20,20,20,20,20};
int MY[43] = {4,5,4,5,6,5,6,3,3,3,7,8,6,7,8,4,5,6,7,4,5,6,7,4,5,6,7,10,9,10,2,3,6,7,8,9,10,1,2,3,6,7,8};
int MNbr = 43;
int HWX[34] = {4,4,6,6,6,6,6,9,10,10,10,11,12,12,12,12,13,14,15,15,15,17,17,17,19,19,19,19,19,21,21,21,21,22};
int HWY[34] = {1,2,3,5,6,8,9,3,6,8,10,4,6,7,8,9,8,2,5,6,10,2,3,9,1,2,4,7,8,1,7,8,10,7};
int HWNbr = 34;
int VWX[61] = {1,1,1,2,2,2,3,3,4,4,5,5,5,6,6,6,6,7,7,7,8,8,9,9,9,9,10,10,11,11,11,12,12,12,12,13,13,13,13,14,14,14,15,15,15,16,16,17,17,17,18,18,19,19,19,20,20,20,21,22,22};
int VWY[61] = {3,4,6,3,4,6,4,9,4,9,3,4,9,3,4,6,9,4,6,9,3,9,3,4,6,9,3,6,4,6,9,3,4,6,9,3,4,6,9,2,4,6,4,6,9,4,9,4,6,9,4,6,4,6,9,4,6,9,6,4,8};
int VWNbr = 61;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ButtonUp, INPUT_PULLUP);
pinMode(ButtonDown, INPUT_PULLUP);
pinMode(ButtonLeft, INPUT_PULLUP);
pinMode(ButtonRight, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
GoingUp = digitalRead(ButtonUp);
GoingDown = digitalRead(ButtonDown);
GoingLeft = digitalRead(ButtonLeft);
GoingRight = digitalRead(ButtonRight);
if (GoingUp != lastGoingUp) {
if (GoingUp == LOW) {
MyPosY -= 1;
Serial.println("Up");
for (int VWi = 0; VWi < VWNbr; VWi++) {
if (MyPosX == VWX[VWi] && MyPosY == VWY[VWi]-1) {
MyPosY +=1;
Serial.print("aie le petit mur");
}
}
if (MyPosY < 1) {
MyPosY = 1;
}
Serial.print("(");
Serial.print(MyPosX);
Serial.print(",");
Serial.print(MyPosY);
Serial.println(")");
}
}
if (GoingDown != lastGoingDown) {
if (GoingDown == LOW) {
MyPosY += 1;
Serial.println("Down");
for (int VWi = 0; VWi < VWNbr; VWi++) {
if (MyPosX == VWX[VWi] && MyPosY == VWY[VWi]) {
MyPosY -=1;
Serial.print("aie le petit mur");
}
}
if (MyPosY > 10) {
MyPosY = 10;
Serial.print("aie le mur");
}
Serial.print("(");
Serial.print(MyPosX);
Serial.print(",");
Serial.print(MyPosY);
Serial.println(")");
}
}
if (GoingLeft != lastGoingLeft) {
if (GoingLeft == LOW) {
MyPosX -= 1;
Serial.println("Left");
for (int HWi = 0; HWi < HWNbr; HWi++) {
if (MyPosX == HWX[HWi]-1 && MyPosY == HWY[HWi]) {
MyPosX +=1;
Serial.print("aie le petit mur");
}
}
if (MyPosX < 1){
MyPosX = 1;
Serial.print("aie le mur");
}
Serial.print("(");
Serial.print(MyPosX);
Serial.print(",");
Serial.print(MyPosY);
Serial.println(")");
}
}
if (GoingRight != lastGoingRight) {
if (GoingRight == LOW) {
MyPosX += 1;
Serial.println("Right");
for (int HWi = 0; HWi < HWNbr; HWi++) {
if (MyPosX == HWX[HWi] && MyPosY == HWY[HWi]) {
MyPosX -=1;
Serial.print("aie le petit mur");
}
}
if (MyPosX > 22){
MyPosX = 22;
Serial.print("aie le mur");
}
Serial.print("(");
Serial.print(MyPosX);
Serial.print(",");
Serial.print(MyPosY);
Serial.println(")");
}
}
for (int Mi = 0; Mi < MNbr; Mi++) {
if(MyPosX == MX[Mi] && MyPosY == MY[Mi]) {
Serial.print("Monster !!!");
Serial.println(" Back to start");
MyPosX = 1;
MyPosY = 1;
}
}
if (MyPosX == 22 && MyPosY == 1){
Serial.println("Félicitation ! Vous êtes sorti !");
}
if (GoingDown != lastGoingDown && GoingUp != lastGoingUp && GoingDown == LOW && GoingUp == LOW) {
Serial.println("Réinitialisation...");
MyPosX = 1;
MyPosY = 1;
Serial.print("(");
Serial.print(MyPosX);
Serial.print(",");
Serial.print(MyPosY);
Serial.println(")");
}
lastGoingUp = GoingUp;
lastGoingDown = GoingDown;
lastGoingLeft = GoingLeft;
lastGoingRight = GoingRight;
delay(50);
}