class Grid {
int mur[][] = new int[18][18] ;
int murVisible[][] = new int[18][18];
int casesParcourues[][] = new int [18][18];
int xarrivee = (int) random (13)+5;
int yarrivee = (int) random (13)+5;
int CompteurJoueur = 0;
int CompteurManhattan = abs(xarrivee+yarrivee);
// création aléatoire des murs
void create() {
for (int xb = 0; xb < 18; xb++) {
for (int yb = 0; yb < 18; yb++) {
int square = (int)random(4);
if (square == 0) mur[xb][yb] = 1;
else mur[xb][yb] = 0;
}
}
//suppression des éventuels murs sur la case d'arrivée et de départ
mur[0][0] = 0;
mur[xarrivee][yarrivee] = 0;
}
void display() {
stroke(255);
//affichage si joueur sur la case d'arrivée
if (CompteurManhattan==0) {
for (int xb = 0; xb < 18; xb++) {
for (int yb = 0; yb < 18; yb++) {
int xpath = xb*20+20;
int ypath = yb*20+20;
//affichage du parcours
if (casesParcourues[xb][yb] == 1) {
fill(240,240,0);
rect(xpath,ypath,20,20);
}
//affichage de tous les murs
if (mur[xb][yb] == 1) {
fill(100,100,100);
rect(xpath,ypath,20,20);
}
}
}
fill (0,0,204);
rect(xarrivee*20+20,yarrivee*20+20,20,20);
}
//Affichage des murs découverts
fill(204,0,0);
for (int xb = 0; xb < 18; xb++) {
for (int yb = 0; yb < 18; yb++) {
int xmur = xb*20+20;
int ymur = yb*20+20;
if (murVisible[xb][yb] == 1) rect(xmur,ymur,20,20);
}
}
}
}
PFont font;
PImage flag;
PImage smiley;
PImage arrow;
Player p;
Grid g;
boolean upPressed, leftPressed, downPressed, rightPressed;
void setup()
{
size(600, 400);
font = loadFont("AlMateen-Bold-48.vlw");
textFont(font, 45);
frameRate(10);
initialize();
}
void initialize()
{
p = new Player(20, 20);
g = new Grid();
g.create();
}
void draw() {
background(230);
fill (230);
rect(19,19,361,361);
p.display();
g.display();
p.move();
fill(0);
arrow = loadImage("ArrowKeys.png");
image(arrow, 430, 220);
smiley = loadImage("High-contrast-face-plain.png");
image(smiley, 420, 60);
flag = loadImage("flagCheck.gif");
image(flag, 420, 140);
text(g.CompteurJoueur, 540, 100 );
text(g.CompteurManhattan, 540, 170 );
//upPressed = false;
//leftPressed = false;
//downPressed = false;
//rightPressed = false;
}
void keyPressed()
{
if (keyCode == UP) upPressed = true;
if (keyCode == LEFT) leftPressed = true;
if (keyCode == DOWN) downPressed = true;
if (keyCode == RIGHT) rightPressed = true;
}
class Player
{
int x, y;
int w, h;
Player(int x, int y)
{
this.x = x;
this.y = y;
w = h = 20;
}
void display()
{
fill(0,0,0);
rect(x,y,w,h);
}
void move()
{
int xpos= int(x/20-1);
int ypos= int(y/20-1);
g.CompteurManhattan= abs(g.xarrivee-xpos)+abs(g.yarrivee-ypos);
g.casesParcourues[xpos][ypos] = 1;
//gestion des évènement clavier
if (rightPressed && xpos<17) {
if (g.mur[xpos+1][ypos] == 0) {
x += 20;
g.CompteurJoueur++;
}
else {
g.murVisible[xpos+1][ypos] = 1;
}
rightPressed = false;
}
if (leftPressed && xpos > 0) {
if (g.mur[xpos-1][ypos] == 0) {
g.CompteurJoueur++;
x -= 20;
}
else {
g.murVisible[xpos-1][ypos] = 1;
}
leftPressed = false;
}
if (downPressed && ypos < 17) {
if (g.mur[xpos][ypos+1] == 0) {
g.CompteurJoueur++;
y += 20;
}
else {
g.murVisible[xpos][ypos+1] = 1;
}
downPressed = false;
}
if (upPressed && ypos > 0) {
if (g.mur[xpos][ypos-1] == 0) {
g.CompteurJoueur++;
y -= 20;
}
else {
g.murVisible[xpos][ypos-1] = 1;
}
upPressed = false;
}
}
}