From 9d973a8fd936123e3079778bc63b2ff61a7c7f5b Mon Sep 17 00:00:00 2001 From: rayaalmudena Date: Tue, 15 Mar 2022 12:21:22 +0100 Subject: [PATCH 1/2] done maze --- 39-laberinth.js | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ 2 files changed, 99 insertions(+) create mode 100644 39-laberinth.js diff --git a/39-laberinth.js b/39-laberinth.js new file mode 100644 index 0000000..c01c991 --- /dev/null +++ b/39-laberinth.js @@ -0,0 +1,96 @@ +//URL: https://www.codewars.com/kata/58663693b359c4a6560001d6 + +//función para encontrar donde se encuentra el jugador, en el caso de que se cambie de posición o que se quiera usar en otro maze +//index1 es la altura del maze, y index2 es el ancho +function findPlayer(maze) { + for (let index1 = 0; index1 < maze.length; index1++) { + for (let index2 = 0; index2 < maze[index1].length; index2++) { + if( maze[index1][index2]==2){ + return [index1,index2]; + } + } + } +} + +//Función que hace que funcione todo junto +function mazeRunner(maze, directions) { + //Posición original del jugador, array con dos posiciones + let positionPlayer = findPlayer(maze); + let keepGoing = true; + let directionIndex=0; + //Por cada dirección que nos da el usuario, "movemos" el jugador de posicioón y luego comprobamos los posibles estados del player + while (keepGoing) { + // Esta función nos devuelve una nueva array con la nuevas posiciones de indeces a causa de mover el player Ejemplo [5,1] = move("N",6,1) + let newPositionPlayer = move(directions[directionIndex],positionPlayer[0],positionPlayer[1]); + // hay que sobrescribir los valores antiguos para poder mover el player, si no, el personaje no se mueve + positionPlayer[0] = newPositionPlayer[0]; + positionPlayer[1] = newPositionPlayer[1]; + + console.log(positionPlayer); + checkStatus = endGame(maze,positionPlayer[0],positionPlayer[1],directionIndex,directions.length); + + //añadir uno mas para cambiar posición en la array de directions + directionIndex++; + //si el resultado de la funcion nos da falso el while acabara + keepGoing=checkStatus[1]; + } + return (checkStatus[0]); +} + +// "cambia" la posición del player atraves de las variables, no se modifica la array maze +function move(direction,newIndex1,newIndex2){ + switch (direction) { + case "N": + newIndex1--; + break; + case "S": + newIndex1++; + break; + case "W": + newIndex2--; + break; + case "E": + newIndex2++; + break; + } + return [newIndex1,newIndex2]; +} + +function endGame(maze,positionPlayer1,positionPlayer2,directionIndex,directionsLenght){ + //comprobar si el player esta dentro de la array, si punto maze[x][y] realmente existe + //por alguna razon comprobar la posición maze[x][y] si es undefined daba error, y lo he tenido que controlar por los lenghts + if (positionPlayer1>=0 && positionPlayer1 < maze.length && positionPlayer2>=0 && positionPlayer2 < maze[1].length ) { + //cada uno de los casos posibles + //si se encuetra con una pared 1, muerto + if (maze[positionPlayer1][positionPlayer2]==1) { + return ["Dead, you hit a wall",false]; + // el jugador a encontrado la salida + }else if (maze[positionPlayer1][positionPlayer2]==3) { + return ["Finish",false]; + // en el caso que no se encuentre en finish(3) y es la última oportunidad de moverse, el jufador esta perdido + }else if (directionIndex==directionsLenght) { + return ["Lost",false]; + //no es ninguno de los otros resultados por lo tanto se encuentra en 0 y puede continuar hacia la siguiente dirección + }else{ + return ["Still going",true]; + } + // si el punto maze[x][y] no existe, el player se ha salido del laberinto + }else { + return ["Dead, falling out of the Labyrinth",false]; + } +} + +var maze = +[[1, 1, 1, 1, 1, 1, 1], +[1, 0, 0, 0, 0, 0, 3], +[1, 0, 1, 0, 1, 0, 1], +[0, 0, 1, 0, 0, 0, 1], +[1, 0, 1, 0, 1, 0, 1], +[1, 0, 0, 0, 0, 0, 1], +[1, 2, 1, 0, 1, 0, 1]]; + +console.log(mazeRunner(maze, ["N", "N", "N", "N", "N", "E", "E", "E", "E", "E"]), "Finish"); +console.log(mazeRunner(maze, ["N", "N", "N", "W", "W"]), "Dead"); +console.log(mazeRunner(maze, ["N", "N", "N", "N", "N", "E", "E", "S", "S", "S", "S", "S", "S"]), "Dead", "Expected Dead"); +console.log(mazeRunner(maze, ["N", "E", "E", "E", "E"]), "Lost"); +console.log(mazeRunner(maze, ["W", "N", "E", "E", "E"]), "Wall"); diff --git a/README.md b/README.md index 133cb14..734d755 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Recently, it has been updated with the members of the team [Full Stack Altia](ht 36. [DNA](https://www.codewars.com/kata/554e4a2f232cdd87d9000038) 37. [PositiveSum](https://www.codewars.com/kata/5715eaedb436cf5606000381) 38. [abrevName](https://www.codewars.com/kata/57eadb7ecd143f4c9c0000a3) +39. [Maze Runner](https://www.codewars.com/kata/58663693b359c4a6560001d6) + ## List of algorithms with TDD 1. [Reversed Words](https://www.codewars.com/kata/51c8991dee245d7ddf00000e) @@ -51,6 +53,7 @@ Recently, it has been updated with the members of the team [Full Stack Altia](ht [Aarón Aira García - 31](https://github.com/aaronaira) [Adrián González Filgueira - 29](https://github.com/AdrianGonzalezFilgueira) [Alba Guzman](https://github.com/aguzsol) +[Almudena Raya](https://github.com/rayaalmudena) [Angel Amado - 30](https://github.com/angel-amado) [Ariel Neme - 23](https://github.com/ArielFabianN) [Arnau Mas - 22](https://github.com/Arnau-Mas) From 9cf54946f6bb453f05c0a2e8e14e0bb4908174f1 Mon Sep 17 00:00:00 2001 From: rayaalmudena Date: Tue, 15 Mar 2022 12:23:17 +0100 Subject: [PATCH 2/2] done --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 734d755..ffed756 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Recently, it has been updated with the members of the team [Full Stack Altia](ht [Aarón Aira García - 31](https://github.com/aaronaira) [Adrián González Filgueira - 29](https://github.com/AdrianGonzalezFilgueira) [Alba Guzman](https://github.com/aguzsol) -[Almudena Raya](https://github.com/rayaalmudena) +[Almudena Raya - 39](https://github.com/rayaalmudena) [Angel Amado - 30](https://github.com/angel-amado) [Ariel Neme - 23](https://github.com/ArielFabianN) [Arnau Mas - 22](https://github.com/Arnau-Mas) @@ -70,9 +70,9 @@ Recently, it has been updated with the members of the team [Full Stack Altia](ht [Ignacio Spadavecchia - 21](https://github.com/Ignacio-Spadavecchia) [Jesús Calatayud](https://github.com/JesusCalatayud) [Jose Cantuarias](https://github.com/JoseCantuarias) -[Jose Castillo](https://github.com/josecastp) -[Kendry Carvajal - 36](https://github.com/Kecar2) -[Kevin Salcedo - 18](https://github.com/kevinsalcedoUab) +[Jose Castillo](https://github.com/josecastp) +[Kendry Carvajal - 36](https://github.com/Kecar2) +[Kevin Salcedo - 18](https://github.com/kevinsalcedoUab) [Júlia Martinez](https://github.com/juliverd59) [Laia Guillén - 20](https://github.com/laiagc892) [Lenny Cala](https://github.com/LennyCC) @@ -87,6 +87,6 @@ Recently, it has been updated with the members of the team [Full Stack Altia](ht [Pedro Ninci](https://github.com/pppeedrito) [Sergi Justiniano](https://github.com/homell100) [Silvia Gutierrez](https://github.com/silviagb2) -[Vanessa Collazos-25](https://github.com/vcollazos) +[Vanessa Collazos-25](https://github.com/vcollazos) [Víctor Cabello-35](https://github.com/VCabelloDel) [Xiande Qiu](https://github.com/Xiande-zx)