Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kerollmops/b2db0f4c6c8ee15b1d71 to your computer and use it in GitHub Desktop.
Save Kerollmops/b2db0f4c6c8ee15b1d71 to your computer and use it in GitHub Desktop.
Maze resolver (bad version)
/*
rouge: personnage
bleu: win !
noir: mur
blanc: vide
*/
boolean touch_wall = false;
int compteur = 0;
int []choixDuPixel() {
int what_pos[] = {0, 0};
int pos[] = {0, 0};
int bad_pos;
int color_pos[] = {0, 0, 0};
do {
what_pos[0] = getX();
what_pos[1] = getY();
while (what_pos[0] == getX() && what_pos[1] == getY()) {
// infinite loop ? obligatoire
pos[0] = getX();
pos[1] = getY();
}
color_pos = getPixelColor(pos[0], pos[1]);
if (color_pos[0] < 127 && color_pos[1] < 127 && color_pos[2] < 127) {
println("Erreur: vous n'etes pas sur du blanc");
println("veuillez choisir une autre position");
bad_pos = 1;
}
else
bad_pos = 0;
}
while (bad_pos == 1);
return (pos);
}
boolean cmpAutour(int a[][], int b[][]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (a[i][j] == -1 || b[i][j] == -1)
continue ;
if (a[i][j] != b[i][j])
return (false);
}
}
return (true);
}
int []computNewSens(int a[], int b[], int sens[])
{
int []ret = {0, 0};
ret[0] = b[1] - a[1];
ret[1] = b[0] - a[0];
if (ret[0] == 0 && ret[1] == 0)
return (sens);
return (ret);
}
boolean isBlue(int pos[]) {
int color_pos[] = {0, 0, 0};
color_pos = getPixelColor(pos[0], pos[1]);
if (color_pos[0] < 127 && color_pos[1] < 127 && color_pos[2] > 200) // bleu
return (true);
return (false);
}
boolean isWhite(int pos[]) {
int color_pos[] = {0, 0, 0};
color_pos = getPixelColor(pos[0], pos[1]);
if (color_pos[0] > 127 && color_pos[1] > 127 && color_pos[2] > 127)
return (true);
return (false);
}
boolean isBlack(int pos[]) {
int color_pos[] = {0, 0, 0};
color_pos = getPixelColor(pos[0], pos[1]);
if (color_pos[0] < 127 && color_pos[1] < 127 && color_pos[2] < 127)
return (true);
return (false);
}
int []move(int a[], int b[]) {
if (isWhite(b) == true || isBlue(b) == true) {
setPixel(a[0], a[1], 255, 255, 255);
setPixel(b[0], b[1], 255, 0, 0);
return (b);
}
return (a);
}
// tout droit
int []getForward(int pos[], int sens[], int autour[][]) {
int new_pos[] = {0, 0};
new_pos[0] = pos[0] + sens[1]; // inversé, merci java
new_pos[1] = pos[1] + sens[0]; // inversé, merci java
if (autour[1 + new_pos[1] - pos[1]][1 + new_pos[0] - pos[0]] == 0)
{
return (new_pos);
}
return (pos);
}
// rotation droite
int []rotateRight(int sens[]) {
int new_sens[] = {0, 0};
if (sens[0] == 0) // vertical
{
if (sens[1] == -1) // gauche
{
new_sens[0] = -1; // haut
}
else if (sens[1] == 1) // droite
{
new_sens[0] = 1; // bas
}
}
else if (sens[1] == 0) // horizontal
{
if (sens[0] == -1) // haut
{
new_sens[1] = 1; // droite
}
else if (sens[0] == 1) // bas
{
new_sens[1] = -1; // gauche
}
}
return (new_sens);
}
// rotation gauche
int []rotateLeft(int sens[]) {
int new_sens[] = {0, 0};
if (sens[0] == 0) // vertical
{
if (sens[1] == -1) // gauche
{
new_sens[0] = 1; // bas
}
else if (sens[1] == 1) // droite
{
new_sens[0] = -1; // haut
}
}
else if (sens[1] == 0) // horizontal
{
if (sens[0] == -1) // haut
{
new_sens[1] = -1; // gauche
}
else if (sens[0] == 1) // bas
{
new_sens[1] = 1; // droite
}
}
return (new_sens);
}
int [][]completeAutour(int pos[]) {
int tmp_pos[] = {0, 0};
int x;
int y;
int autour[][] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0} };
x = 0;
while (x < 3)
{
y = 0;
while (y < 3)
{
tmp_pos[0] = pos[0] + x - 1;
tmp_pos[1] = pos[1] + y - 1;
if (isBlack(tmp_pos) == true) // mur noir
{
autour[y][x] = 1;
}
y++;
}
x++;
}
return (autour);
}
int [][]rotateMatrix(int matrix[][]) {
int [][]ret = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0} };
for (int layer = 0; layer < 3 / 2; layer++) {
int first = layer;
int last = 3 - 1 - first;
for (int i = first; i < last; i++) {
int offset = i - first;
int temp = matrix[first][i];
ret[first][i] = matrix[last-offset][first];
ret[last-offset][first] = matrix[last][last-offset];
ret[last][last-offset] = matrix[i][last];
ret[i][last] = temp;
}
}
return (ret);
}
int [][]rotateAutourWithSens(int autour[][], int sens[])
{
int ret_autour[][] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0} };
if (sens[0] == -1) // haut
{
// don't rotate
ret_autour = autour;
}
else if (sens[0] == 1) // bas
{
// rotate 180°
ret_autour = rotateMatrix(autour);
ret_autour = rotateMatrix(ret_autour);
}
else if (sens[1] == -1) // gauche
{
// rotate 90°
ret_autour = rotateMatrix(autour);
}
else if (sens[1] == 1) // droite
{
// rotate 270°
ret_autour = rotateMatrix(autour);
ret_autour = rotateMatrix(ret_autour);
ret_autour = rotateMatrix(ret_autour);
}
return (ret_autour);
}
int []getNextMove(int pos[], int sens[], int autour[][]) {
//static int compteur = 0;
int new_pos[] = {0, 0};
int tmp_autour[][] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0} };
int mur_face[][] = { {-1, 1, -1},
{-1, -1, -1},
{-1, -1, -1} };
int angle_mur_gauche[][] = { {-1, 1, -1},
{1, 0, 0},
{-1, -1, -1} };
int angle_mur_droit[][] = { {-1, 1, -1},
{0, 0, 1},
{-1, -1, -1} };
int mur_arriere_gauche[][] = { {-1, -1, -1},
{0, 0, -1},
{1, -1, -1} };
tmp_autour = rotateAutourWithSens(autour, sens);
//println(compteur);
if (touch_wall == false || compteur != 0)
{
if (cmpAutour(tmp_autour, angle_mur_droit) == true)
{
touch_wall = true;
sens = rotateRight(sens);
compteur--;
sens = rotateRight(sens);
compteur--;
}
else if (cmpAutour(tmp_autour, mur_face) == true)
{
// tourner droite
touch_wall = true;
sens = rotateRight(sens);
compteur--;
}
else if (compteur != 0 && cmpAutour(tmp_autour, angle_mur_gauche) == true)
{
sens = rotateRight(sens);
compteur--;
}
else if (compteur != 0 && cmpAutour(tmp_autour, mur_arriere_gauche) == true)
{
// tourner gauche (suivre le mur)
sens = rotateLeft(sens);
compteur++;
}
}
else if (compteur == 0)
{
touch_wall = false;
sens[0] = -1;
sens[1] = 0;
}
new_pos = getForward(pos, sens, autour);
return (new_pos);
}
void main() {
boolean win = false;
int pos[] = {0, 0};
int tmp_pos[] = {0, 0};
int new_pos[] = {0, 0};
int sens[] = { -1, 0 }; // haut
int autour[][] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0} };
load("labyrinthe.jpg", false);
compteur = 0;
touch_wall = false;
// get pixel position
pos = choixDuPixel();
// tant que je ne suis pas sur du bleu
while (win == false)
{
autour = completeAutour(pos);
// get next move (longer les murs)
new_pos = getNextMove(pos, sens, autour);
// comput new sens
sens = computNewSens(pos, new_pos, sens);
if (isWhite(new_pos) == true || isBlue(new_pos) == true)
{
// savoir si l'on a gagné avant de l'ecraser de rouge
if (isBlue(new_pos) == true)
win = true;
tmp_pos = move(pos, new_pos);
pos[0] = tmp_pos[0];
pos[1] = tmp_pos[1];
}
sleep(30);
}
println("BRAVO TU AS TROUVÉ LA SORTIE !!!");
//println(compteur);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment