Labyrinth -always- surving turtle

Started by poisoned, Mar 19, 2021, 12:03 AM

Previous topic - Next topic

poisoned

Hey there,
so as i kept on playing with the turtle, i wanted her to go trough a labyrinth.
Also the turtle "prints" the map (on a monitor, by filewrite...whatever...)

So...i did it...:0)

I dont know, if this is any "clean" code - but its my code and it worked.
As ths is my first post in any forum i've been so far ( i ALWAYS find any solution already described when i have problems) i hope i'm doing ths right.

So...there is only 1 Way to get through a Labyrinth.
Whatever u do - if u choose to turn left or right...whatever..u have to stay with this.
So..if you chose to turn left as your first choice in a labyrinth u have to turn left every time u can. If not, keep on going forward( if u run into "nil..backward also is forward"...^^)

This is, what i tell the turtle - either turn left or right.
Then she goes her way until....end....

The first part is the "net file" of (in this case) a computer, writing the turtles positions on a 8*8 monitor.
So thats y i set Cursor to x/2 and y on the bottom, so there is place to write up.
Also this i used as array to save all the inspects.

Monitor get Net data :

Spoiler
xSize,ySize = p_tv.top.getSize()

cPosX=34
cPosY=39

p_tv.top.setTextScale(1)
p_tv.top.setTextColour(colors.black)
p_tv.top.setBackgroundColour(colors.lightBlue)

p_tv.top.setCursorPos(34,39)
p_tv.top.clear()


print("Grösse gesamt: ".."x: "..xSize.." y: "..ySize)
--print ("\nAbsolute Mitte: ".."x: "..cPosX.." y: "..cPosY)


function p_Red_receive()
--    print("Computer steht auf Empfang---strg+t für break")
 --p_tv.top.setCursorPos(X,Y)
    while rednet.isOpen() do
          print("Computer steht auf Empfang---strg+t für break")
          p_red_id, p_red_msg, p_red_port = rednet.receive()
         

              if p_red_msg == "reboot" then
                    os.reboot()

              elseif p_red_msg == "cls" then
                    --xsize,ysize = p_tv.top.getSize()
                    cPosX = 34
                    cPosY = 39
                    p_tv.top.clear()
                    p_tv.top.setCursorPos(cPosX,cPosY)

              elseif p_red_port == "shell" then
                    shell.run(p_red_msg)

--              elseif p_red_port == "map" then
              end


              if p_red_port == "X" then
                    local rcPosX = p_red_msg
                    print(rcPosX)
                    cPosX = cPosX + rcPosX
                   
                    p_tv.top.setCursorPos(cPosX,cPosY)

              end

              if p_red_port == "Y" then
                 
                    local rcPosY = p_red_msg
                    print(rcPosY)
                    cPosY = cPosY + rcPosY
                    p_tv.top.setCursorPos(cPosX,cPosY)
              end

             
              if p_red_port == "map" then
                    --p_tv.top.setCursorPos(cPosY,cPosY)
                    p_tv.top.write(p_red_msg)
              end             
    end
end





The next part would be the turtle placing herself (only for herself absolute...) into absolute startpos 0,0. with inspect direction beginnig "with her north"....
So this is also, where i calculate the Movement Offset (1,-1) and also where she "finds her own North" and then send the offset via wlan to the computer to set his x/Y cursors.
So....this is my "compass"....and the "map over the map..."


Spoiler
--#region <<< Init >>>

North = 1
West = 4
South = 3
East = 2

currPos = vector.new(0,0)
currDirection = North
currAbsMovementOffs = vector.new(0,-1)


function _GetCurrentDirectionId()
    return currDirection
end

--#endregion


--#region <<< Options >>>
  -------------------------------------
  debugprintactive = true
  -------------------------------------
--#endregion


--#region <<< Kompass >>>

---comment  Errechnet das Offset in der absolutenWelt, wenn sich die Turtle geradeaus bewegt.
function CalcNewAbsMovementOffset()
    if currDirection == North then
        currAbsMovementOffs.x = 0
        currAbsMovementOffs.y = -1
    elseif currDirection == West then
        currAbsMovementOffs.x = -1
        currAbsMovementOffs.y = 0
    elseif currDirection == South then
        currAbsMovementOffs.x = 0
        currAbsMovementOffs.y = 1
    elseif currDirection == East then
        currAbsMovementOffs.x = 1
        currAbsMovementOffs.y = 0
    end
end

function DecDirection()
    if currDirection == 1 then
        currDirection = 4
    else
 currDirection = currDirection - 1
    end
   
    CalcNewAbsMovementOffset()
end

local function IncDirection()
    if currDirection == 4 then
        currDirection = 1       
    else
    currDirection = currDirection + 1 
    end

    CalcNewAbsMovementOffset()
end

--#endregion



--#region <<< API >>>

---Initialisiert die Turtle mit Startrichtung(als string, deutsch oder englisch, und einem 2D-Vektor)
function _SetStartParams(startDirectionName, xpos, ypos)
    print("SEtStartbla")
    if string.sub(startDirectionName,1,1) == "N" then currDirection = North end
    if string.sub(startDirectionName,1,1) == "W" then currDirection = West end
    if string.sub(startDirectionName,1,1) == "S" then currDirection = South end
    if string.sub(startDirectionName,1,1) == "E" then currDirection = East end
    if string.sub(startDirectionName,1,1) == "O" then currDirection = East end

  currPos.x = xpos
  currPos.y = ypos
   
  CalcNewAbsMovementOffset() -- Das hat auch gefehlt

  DebugPrint("SetStartParams")
end



---Gibt die aktuelle Position der Turtle zurück.
---@return number
function _GetCurrentPos()
    return currPos
end



---Dreht die Turtle nach links.
function _RotateLeft() 
    DecDirection()

    DebugPrint("RotateLeft")
end

---Dreht die Turtle nach rechts.
function _RotateRight()
    IncDirection()

    DebugPrint("RotateRight")
end

---Gibt die Position zurück, welche die Turtle im Blickfeld hat.
---@return number
function _GetDetectionPos()
    return currPos + currAbsMovementOffs
end

---Bewegt die Turtle in ihre Blickrichtung
function _MoveForward()
    currPos = currPos + currAbsMovementOffs

    DebugPrint("MoveForward")
end

--#endregion



--#region <<< Debughelper >>>

function DebugPrint(commandname)
  if debugprintactive then
 --print("> "..commandname.." | "..currDirection.."currDirection "..currAbsMovementOffs.."currAbsMovementOffs")
  end
end

--#endregion
[/spoiler]



Next part would be the array to save all the data - not really important...but i wanted to,,,




Spoiler
p_TMap = {}

function p_Array(maxColums,maxRows,value)

    for i=0,maxRows do
    p_TMap[i] = {}
        for j=0,maxColums do
            p_TMap[i][j] = value
        end
    end
end
-----array wirdgleich auf Basis der monitor Werte erstellt
p_Array(30,30)
[/spoiler]





And now, i use the "position offset data" to give her the orientation, she needs - this would be the "detect all and decide, what then" - part:



Spoiler
require("pos")
require("array")

msg1 = "0"
msg2 = "B"


function _pFalse()
  if _GetCurrentDirectionId() == 1 then
        rednet.send(2,-1,"Y")
        rednet.send(2,msg1,"map")
        rednet.send(2,1,"Y")
    end

    if _GetCurrentDirectionId() == 4 then
        rednet.send(2,-1,"X")
        rednet.send(2,msg1,"map")
        rednet.send(2,1,"X")
    end

    if _GetCurrentDirectionId() == 2 then
        rednet.send(2,1,"X")
        rednet.send(2,msg1,"map")
        rednet.send(2,-1,"X")
    end

    if _GetCurrentDirectionId() == 3 then
        rednet.send(2,1,"Y")
        rednet.send(2,msg1,"map")
        rednet.send(2,-1,"Y")
    end
end

function _pTrue()
    if _GetCurrentDirectionId() == 1 then
        rednet.send(2,-1,"Y")
        rednet.send(2,msg2,"map")
        rednet.send(2,1,"Y")
    end
    if _GetCurrentDirectionId() == 4 then
        rednet.send(2,-1,"X")
        rednet.send(2,msg2,"map")
        rednet.send(2,1,"X")
    end
    if _GetCurrentDirectionId() == 2 then

        rednet.send(2,1,"X")
        rednet.send(2,msg2,"map")
        rednet.send(2,-1,"X")
    end
    if _GetCurrentDirectionId() == 3 then
        rednet.send(2,1,"Y")
        rednet.send(2,msg2,"map")
        rednet.send(2,-1,"Y")
   
    end
end


-------  ROTATE LL
function _pRotate_L()
    turtle.turnLeft()
    _RotateLeft()
end

function _pRotate_R()
    turtle.turnRight()
    _RotateRight()
end

function _g2()
    turtle.forward()
    _MoveForward()
    if _GetCurrentDirectionId() == 1 then
        rednet.send(2,-1,"Y")
    end
    if _GetCurrentDirectionId() == 4 then
        rednet.send(2,-1,"X")
    end
    if _GetCurrentDirectionId() == 2 then
        rednet.send(2,1,"X")
    end
    if _GetCurrentDirectionId() == 3 then
        rednet.send(2,1,"Y")
    end
end


function _g()
    for i=0,3 do
        if not turtle.detect() then
            _pFalse()
        end

        if turtle.detect() then
            _pTrue()
        end
        _pRotate_L()
        print(_GetCurrentDirectionId())
    end
end
[/spoiler]




So as we now gave the turtle orientation and absolute positions in her "little world", she "decides", ehat to do - in this case, i tell her, to turn left if possible (as it was the very long way of the labyrinth)


Spoiler
require("init")
require("wrap")
require("p_Net")
require("array")
require("pos")
require("det")

p_det =false
p_vor = false
p_back = false
p_left = false
p_180 = false
p_360 = false
p_right = false
p_Go = false


   
     
     
     

------------------------------------IF NOT DETECT
function _GO()
repeat
    _g()
    if not turtle.detect() then
 p_vor = true
 _pRotate_L() ---  <----!!!!!
 if turtle.detect() then
                _pRotate_R()
 end
 if not turtle.detect() then
 _g2()
        _g()
        p_vor= false
    end
 if p_vor then
        _g2()
        _g()
 p_vor = false
 end
    sleep(0.6)
end

    -------------------------------------IF DETECT

        if turtle.detect() then
            _pRotate_L()
    if turtle.detect() then
        p_left = true
        _pRotate_R()
                _pRotate_R()
                if turtle.detect() then
    p_right = true
                    _pRotate_L()
    end
    end

        if not turtle.detect() then
    _g2()
            _g()
        end

        if p_left and p_right then
            _pRotate_L()
            _pRotate_L()
            _g2()
            _g()
            p_left = false
            p_right = false
        end
                p_left = false
                sleep(0.6)

    end
    sleep(0.6)

    until turtle.detectUp()
end
[/spoiler]



Yeah...so...she keeps on until...at the end...she reachs the end (i put a block in "above" to tell her to stop if turtle.detectUp() as it took some time...^^

Oh and the turtle sends a "0" for "way is free" and she sends an "B" for "block" in her way tot the computer next to the monitor....
Its like a good old tilemap...:0)


This was so much fun.
Also my little boy really enjoyed this.
In Germany you would say " Da war die eine oder andre Nuss zu knacken"...its about nuts....and more nuts to get...hahahah

I really really hope, he will stop playing "fck%"ing" gta in future and do sth. for his education.

Greez from andi and Louis



[attach name=Unbenannt.jpg type=image/jpeg]233[/attach]

poisoned

aehm....as i just saw, my comments are still in German...if u do me want to translate them, i will tell the boy to do so....:0)

Lupus590

Your code is quite long there, for readability you might want to put it into a spoiler tag, or put it onto pastebin or github (or some other service) and link to them.

poisoned

i tried spoiler tag but its "greyed"...

poisoned

aehm....this is me - posting code in the cc-forum and trying the sixth time to wrap the code into spoilers...

I don't get it.
Is it right 2 use

[spoiler]...[/spoiler]

I tried - but if i look at previe, the tags are gone and some tags are put in between..
What am i not getting here??

poisoned

so...whatever i tried...this spoiler thing seems to hate me - ....
maybe i have a bug in chrome?
i rewrote the hole post, i copy/pasted it, i tried several spoiler/code tag combis...nothing works...^^

this is getting ridicolous....i am feeling a bit dumb now..^^

QuickMuffin8782

I have the same problem. I at least tried to work it out, but it seperates into other *dab*.
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
linktr.ee/quickmuffin8782

poisoned

Yeah....maybe 2morrow, i'll try some other browsers...
Can't be- my first post on a coding site ever and i am 2 idiotic 2 post "conform"...hahaha