var palette=['#000000', '#FF0000', '#FF8800', '#FFFF00'];
createCanvas(windowWidth, windowHeight);
for (var i=0; i<6; i++) {
for (var j=0; j<7; j++) {
if ((i>0&&i<5)&&(j>0&&j<6))places[i][j]=true;
pieces.push(new Piece(1, 1, 0, 4, 0));
pieces.push(new Piece(1, 1, 3, 4, 0));
pieces.push(new Piece(1, 1, 1, 3, 0));
pieces.push(new Piece(1, 1, 2, 3, 0));
pieces.push(new Piece(2, 1, 1, 2, 1));
pieces.push(new Piece(1, 2, 0, 0, 2));
pieces.push(new Piece(1, 2, 3, 0, 2));
pieces.push(new Piece(1, 2, 0, 2, 2));
pieces.push(new Piece(1, 2, 3, 2, 2));
pieces.push(new Piece(2, 2, 1, 0, 3));
line(width/2-2*cote-margin, height/2-2.5*cote-margin, width/2+2*cote+margin, height/2-2.5*cote-margin);
line(width/2+2*cote+margin, height/2-2.5*cote-margin, width/2+2*cote+margin, height/2+2.5*cote+margin);
line(width/2+2*cote+margin, height/2+2.5*cote+margin, width/2+cote+margin, height/2+2.5*cote+margin);
line(width/2-cote-margin, height/2+2.5*cote+margin, width/2-2*cote-margin, height/2+2.5*cote+margin);
line(width/2-2*cote-margin, height/2+2.5*cote+margin, width/2-2*cote-margin, height/2-2.5*cote-margin);
for (var i=0; i<10; i++) {
if(won)text("You win in "+ncoups+" moves!",width/2,50);
else text(ncoups+" move"+((ncoups<2)?"":"s"),width/2,50);
for (var i=0; i<6; i++) {
for (var j=0; j<7; j++) {
ellipse(width/2-2*cote+(i-1)*cote+dcote, height/2-2.5*cote+(j-1)*cote+dcote, 5, 5);
ellipse(width/2-2*cote+(i-1)*cote+dcote, height/2-2.5*cote+(j-1)*cote+dcote, 5, 5);
function mousePressed() {
for (var i=0; i<10; i++) {
function Piece(nh, nv, xint, yint, col) {
this.rfree=this.lfree=this.ufree=this.dfree=false;
this.move=createVector(0, 0, 0);
this.x=width/2-2*cote+this.xint*cote+dcote*this.nh;
this.y=height/2-2.5*cote+this.yint*cote+dcote*this.nv;
this.w=this.nh*cote-2*margin;
this.h=this.nv*cote-2*margin;
for (var i=0; i<nh; i++) {
for (var j=0; j<nv; j++) {
places[xint+i+1][yint+j+1]=false;
Piece.prototype.checkprise=function() {
if (mouseX>this.x-this.w/2 &&mouseX<this.x+this.w/2&&mouseY>this.y-this.h/2 &&mouseY<this.y+this.h/2&&this.move.z==0) {
for (var i=0; i<this.nv; i++) {
if (places[1+this.xint+this.nh][1+this.yint+i])freeplaces++;
if (freeplaces==this.nv) {
for (var i=0; i<this.nv; i++) {
if (places[this.xint][1+this.yint+i])freeplaces++;
if (freeplaces==this.nv) {
for (var i=0; i<this.nh; i++) {
if (places[1+this.xint+i][1+this.yint+this.nv]) {
if (freeplaces==this.nh || (pieces.indexOf(this)==9 && this.yint==3 && this.xint==1)) {
for (var i=0; i<this.nh; i++) {
if (places[1+this.xint+i][this.yint])freeplaces++;
if (freeplaces==this.nh) {
var dr=abs(mouseX-(this.x+this.w/2));
var dl=abs(mouseX-(this.x-this.w/2));
var dd=abs(mouseY-(this.y+this.h/2));
var du=abs(mouseY-(this.y-this.h/2));
if (this.rfree)this.moveright();
else if (this.lfree)this.moveleft();
else if (this.ufree)this.moveup();
else if (this.dfree)this.movedown();
} else if (this.freedom==2) {
if (this.rfree&&this.lfree) {
if (mouseX>this.x)this.moveright();
} else if (this.dfree&&this.ufree) {
if (mouseY>this.y)this.movedown();
} else if (this.rfree&&this.dfree) {
if (dr>dd)this.movedown();
} else if (this.rfree&&this.ufree) {
} else if (this.lfree&&this.dfree) {
if (dl>dd)this.movedown();
} else if (this.lfree&&this.ufree) {
} else if (this.freedom==3) {
switch(min(this.rfree?dr:1000, this.lfree?dl:1000, this.dfree?dd:1000, this.ufree?du:1000)) {
this.rfree=this.lfree=this.ufree=this.dfree=false;
Piece.prototype.moveright=function() {
for (var i=0; i<this.nv; i++) {
places[1+this.xint+this.nh][1+this.yint+i]=false;
places[1+this.xint][1+this.yint+i]=true;
this.move=createVector(5, 0, width/2-2*cote+this.xint*cote+dcote*this.nh);
Piece.prototype.moveleft=function() {
for (var i=0; i<this.nv; i++) {
places[this.xint][1+this.yint+i]=false;
places[this.xint+this.nh][1+this.yint+i]=true;
this.move=createVector(-5, 0, width/2-2*cote+this.xint*cote+dcote*this.nh);
Piece.prototype.movedown=function() {
for (var i=0; i<this.nh; i++) {
places[1+this.xint+i][1+this.yint+this.nv]=false;
places[1+this.xint+i][1+this.yint]=true;
if(this.yint==4&&pieces.indexOf(this)==9)won=true;
this.move=createVector(0, 5, height/2-2.5*cote+this.yint*cote+dcote*this.nv);
Piece.prototype.moveup=function() {
for (var i=0; i<this.nh; i++) {
places[1+this.xint+i][this.yint]=false;
places[1+this.xint+i][this.yint+this.nv]=true;
this.move=createVector(0, -5, height/2-2.5*cote+this.yint*cote+dcote*this.nv);
Piece.prototype.display=function() {
if (abs(this.x-this.move.z)<5) {
if (abs(this.y-this.move.z)<5) {
rect(this.x, this.y, this.w, this.h, 10, 10, 10, 10);
if (pieces.indexOf(this)==9) {
if(won)text("Thanks!!!",this.x,this.y);
else text("Get me \n outta here!",this.x,this.y);