From a1a7fa1bf5376bb2cdd1fe7ea00048545fb46b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9tro?= <yann.roberge@polymtl.ca> Date: Fri, 23 Oct 2020 22:57:54 -0400 Subject: [PATCH] Ajout des fonctions de traversements de zone. Propagation de l'argument 'grounded' --- rsiTurtle.lua | 65 ++++++++++++++++++++++++++++++++++++++++----------- test.lua | 46 ++++++++++++++++++++---------------- 2 files changed, 78 insertions(+), 33 deletions(-) diff --git a/rsiTurtle.lua b/rsiTurtle.lua index 581f9bc..4b49e1a 100644 --- a/rsiTurtle.lua +++ b/rsiTurtle.lua @@ -268,9 +268,10 @@ end --- -- \brief Déplacement avant +-- \param bool grounded Se déplacer en restant toujours au niveau du sol -- \return bool Succès -function RSI_Turtle:forward() - return self:moveForward(1) +function RSI_Turtle:forward(grounded) + return self:moveForward(1, grounded) end --- @@ -284,6 +285,7 @@ function RSI_Turtle:moveForward(distance, grounded) if (grounded) then self:ground() + self:climb() end while (traveled < distance) and turtle.forward() @@ -391,22 +393,22 @@ end --- -- \brief Déplacement à gauche +-- \param bool grounded Se déplacer en restant toujours au niveau du sol -- \return bool Succès -function RSI_Turtle:left() +function RSI_Turtle:left(grounded) self:turnLeft() - result = self:forward() - self:turnRight() + result = self:forward(grounded) return result end --- -- \brief Déplacement à droite +-- \param bool grounded Se déplacer en restant toujours au niveau du sol -- \return bool Succès -function RSI_Turtle:right() +function RSI_Turtle:right(grounded) self:turnRight() - result = self:forward() - self:turnLeft() + result = self:forward(grounded) return result end @@ -437,11 +439,48 @@ end --- -- \brief Quadriller une zone carrée. La tortue traverse chaque case, ligne par -- ligne. N'affecte pas l'altitude. --- \param pointeur_de_fonction action Pointeur de l'action à réaliser juste +-- \param pointeur_de_fonction action Pointeur de l'action à réaliser juste -- après être arrivé sur chaque nouveau bloc. +-- \param int lines Nombre de lignes (direction: avant) +-- \param int cols Nombre de colonnes (direction: droite) +-- \param bool grounded Se déplacer en restant toujours au niveau du sol -- \return bool Succès -function RSI_Turtle:goThroughArea(action) - --TODO: Implementer +function RSI_Turtle:goThroughArea(lines, cols, action, grounded) + --TODO: Prendre en compte le paramètre "action" + local success = true + + assert(lines >= 1) + assert(cols >= 1) + + -- La ligne de la case de départ compte comme 1 + lines = lines - 1 + + if (grounded) + then + self:ground() + end + + for i=1,cols,1 + do + print("Debut de colonne.") + success = self:moveForward(lines, grounded) + + if not success + then + return success + end + + if ( (i%2) == 0 ) + then + success = self:left(grounded) + self:turnLeft() + else + success = self:right(grounded) + self:turnRight() + end + + end + return success end @@ -451,8 +490,8 @@ end -- \param pointeur_de_fonction action Pointeur de l'action à réaliser juste -- après être arrivé sur chaque nouveau bloc. -- \return bool Succès -function RSI_Turtle:goThroughAreaGround(action) - --TODO: Implementer +function RSI_Turtle:goThroughAreaGrounded(lines, cols, action) + return self:goThroughArea(lines, cols, action, true) end diff --git a/test.lua b/test.lua index 4ee13d5..610e8ab 100644 --- a/test.lua +++ b/test.lua @@ -97,51 +97,57 @@ end -- assert(t:moveTo2D(5,0)) -- print(t:getPos()) -- assert(checkPos(5,0,0)) --- +-- -- assert(t:moveToOrigin()) -- print(t:getPos()) -- assert(checkPos(0,0,0)) --- +-- -- assert(t:moveTo2D(0, 5)) -- print(t:getPos()) -- assert(checkPos(0,0,5)) --- +-- -- assert(t:moveTo2D(0,-5)) -- print(t:getPos()) -- assert(checkPos(0,0,-5)) --- +-- -- assert(t:moveToOrigin()) -- print(t:getPos()) -- assert(checkPos(0,0,0)) --- +-- -- assert(t:moveTo(10,2,-1)) -- print(t:getPos()) -- assert(checkPos(10,2,-1)) --- +-- -- assert(t:moveTo(-5,5,5)) -- print(t:getPos()) -- assert(checkPos(-5,5,5)) -assert(t:moveToOrigin()) -print(t:getPos()) -assert(checkPos(0,0,0)) -t:face(12) +-- assert(t:moveToOrigin()) +-- print(t:getPos()) +-- assert(checkPos(0,0,0)) +-- t:face(12) -- print("Tests climb") -- assert(t:climb()) -print("Tests moveForwardGrounded") -assert(t:moveTo(0,5,0)) -assert(t:moveForwardGrounded(15)) -assert(checkPos(15,0,0)) +-- print("Tests moveForwardGrounded") +-- assert(t:moveTo(0,5,0)) +-- assert(t:moveForwardGrounded(15)) +-- assert(checkPos(15,0,0)) +-- +-- print("Tests moveGrounded") +-- assert(t:moveToGrounded(-5,5)) +-- assert(checkPos(-5,0,5)) +-- +-- assert(t:moveTo(-5,2,-1)) +-- print(t:getPos()) +-- assert(checkPos(-5,2,-1)) -print("Tests moveGrounded") -assert(t:moveToGrounded(-5,5)) -assert(checkPos(-5,0,5)) +-- assert(t:goThroughArea(5, 5, nil)) +-- assert(checkPos(4,0,4)) -assert(t:moveTo(-5,2,-1)) -print(t:getPos()) -assert(checkPos(-5,2,-1)) +-- assert(t:goThroughArea(15, 15, nil, true)) +assert(t:goThroughAreaGrounded(3, 3)) assert(t:moveToOrigin()) print(t:getPos()) -- GitLab