From 80b867a8237bb1c31ddb3944c9c410c40b8760f6 Mon Sep 17 00:00:00 2001 From: nbillard Date: Thu, 1 Dec 2022 04:49:58 +0100 Subject: [PATCH] fixed moving in labyrinth problem --- README.md | 4 ++-- script.js | 29 +++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e7d19a7..2ca6b50 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# sokoban +# Sokoban -projet Web 3Mic \ No newline at end of file +projet Web 3Mic diff --git a/script.js b/script.js index eb5ad66..050e496 100644 --- a/script.js +++ b/script.js @@ -58,6 +58,7 @@ class Position { this.x = x; this.y = y; } + move(direction) { switch (direction) { case MoveDirection.Right: @@ -92,8 +93,16 @@ class Position { } isWithin(pos) { - return this.x < pos.x && this.y < pos.y + return this.x < pos.x && this.y < pos.y; } + + isEqual(pos) { + return this.x === pos.x && this.y === pos.y; + } +} + +const copyPosition = (pos) => { + return new Position(pos.x, pos.y); } class Tile { @@ -233,17 +242,22 @@ const generatePlayground = (levelBlueprint, canvasWidth, canvasHeight) => { canMove(pos) { const foregroundAnswer = this.foreground[pos.y][pos.x].isMovable(); const backgroundAnswer = this.background[pos.y][pos.x].isMovable(); - if (backgroundAnswer === CanMove.No) { + if (backgroundAnswer == CanMove.No) { return CanMove.No; } else { return foregroundAnswer; } }, move(direction) { - let aux = this.playerPos; + let aux = copyPosition(this.playerPos); let willMove = false; let finishedChecking = false; + let moveCount = 0; while (aux.isWithin({x: this.width, y: this.height}) && !finishedChecking) { + console.log("checking at position:"); + console.log(aux); + console.log("answer"); + console.log(this.canMove(aux)); switch(this.canMove(aux)) { case CanMove.Yes: willMove = true; @@ -255,13 +269,16 @@ const generatePlayground = (levelBlueprint, canvasWidth, canvasHeight) => { break; case CanMove.Maybe: aux.move(direction); + moveCount++; break; } } + console.log(this.playerPos); + console.log("in playground.move"); if (willMove) { - console.log("in playground.move"); - let posOfObjectToMove = aux; - while (aux != this.playerPos) { + this.playerPos.move(direction); + let posOfObjectToMove = copyPosition(aux); + for (let i = 0; i < moveCount; i++) { posOfObjectToMove.moveBackwards(direction); console.log("I try to move"); [this.foreground[aux.y][aux.x], this.foreground[posOfObjectToMove.y][posOfObjectToMove.x]] =