SHOW:
|
|
- or go back to the newest paste.
| 1 | -- Turtle Maze Program v2.0 | |
| 2 | ||
| 3 | -- GLOBAL VARIABLES | |
| 4 | ||
| 5 | local maze = {}
| |
| 6 | local visited = {}
| |
| 7 | ||
| 8 | local stackTrace = {}
| |
| 9 | local stackTraceCount = 0 | |
| 10 | ||
| 11 | local mazeSize = 50 | |
| 12 | local posX = 25 | |
| 13 | local posY = 0 | |
| 14 | ||
| 15 | local currentDirection = 1 -- {"N", "E", "S", "W"}
| |
| 16 | ||
| 17 | ||
| 18 | -- FUNCTIONS | |
| 19 | ||
| 20 | function init() | |
| 21 | ||
| 22 | for x = 1, mazeSize do | |
| 23 | maze[x] = {}
| |
| 24 | ||
| 25 | for y = 1, mazeSize do | |
| 26 | maze[x][y] = nil | |
| 27 | end | |
| 28 | end | |
| 29 | ||
| 30 | for x = 1, mazeSize do | |
| 31 | visited[x] = {}
| |
| 32 | ||
| 33 | for y = 1, mazeSize do | |
| 34 | visited[x][y] = 0 | |
| 35 | end | |
| 36 | end | |
| 37 | ||
| 38 | end | |
| 39 | ||
| 40 | ||
| 41 | function forward() | |
| 42 | ||
| 43 | if currentDirection == 1 then | |
| 44 | posY = posY + 1 | |
| 45 | end | |
| 46 | if currentDirection == 2 then | |
| 47 | posX = posX + 1 | |
| 48 | end | |
| 49 | if currentDirection == 3 then | |
| 50 | posY = posY - 1 | |
| 51 | end | |
| 52 | if currentDirection == 4 then | |
| 53 | posX = posX - 1 | |
| 54 | end | |
| 55 | ||
| 56 | turtle.forward() | |
| 57 | ||
| 58 | visited[posX][posY] = 1 | |
| 59 | ||
| 60 | saveToStackTrace() | |
| 61 | end | |
| 62 | ||
| 63 | ||
| 64 | function win() | |
| 65 | ||
| 66 | local success, data = turtle.inspect() | |
| 67 | ||
| 68 | if success then | |
| 69 | if data.name == "minecraft:cobblestone" then | |
| 70 | return true | |
| 71 | end | |
| 72 | end | |
| 73 | ||
| 74 | return false | |
| 75 | end | |
| 76 | ||
| 77 | ||
| 78 | function mapPosition() | |
| 79 | ||
| 80 | if currentDirection == 1 then | |
| 81 | if maze[posX][posY + 1] == nil then -- front | |
| 82 | maze[posX][posY + 1] = (turtle.detect() and 1 or 0) | |
| 83 | end | |
| 84 | if maze[posX - 1][posY] == nil then -- left | |
| 85 | turtle.turnLeft() | |
| 86 | maze[posX - 1][posY] = (turtle.detect() and 1 or 0) | |
| 87 | turtle.turnRight() | |
| 88 | end | |
| 89 | if maze[posX + 1][posY] == nil then -- right | |
| 90 | turtle.turnRight() | |
| 91 | maze[posX + 1][posY] = (turtle.detect() and 1 or 0) | |
| 92 | turtle.turnLeft() | |
| 93 | end | |
| 94 | return | |
| 95 | end | |
| 96 | ||
| 97 | if currentDirection == 2 then | |
| 98 | if maze[posX + 1][posY] == nil then -- front | |
| 99 | maze[posX + 1][posY] = (turtle.detect() and 1 or 0) | |
| 100 | end | |
| 101 | if maze[posX][posY + 1] == nil then -- left | |
| 102 | turtle.turnLeft() | |
| 103 | maze[posX][posY + 1] = (turtle.detect() and 1 or 0) | |
| 104 | turtle.turnRight() | |
| 105 | end | |
| 106 | if maze[posX][posY - 1] == nil then -- right | |
| 107 | turtle.turnRight() | |
| 108 | maze[posX][posY - 1] = (turtle.detect() and 1 or 0) | |
| 109 | turtle.turnLeft() | |
| 110 | end | |
| 111 | return | |
| 112 | end | |
| 113 | ||
| 114 | if currentDirection == 3 then | |
| 115 | if maze[posX][posY - 1] == nil then -- front | |
| 116 | maze[posX][posY - 1] = (turtle.detect() and 1 or 0) | |
| 117 | end | |
| 118 | if maze[posX + 1][posY] == nil then -- left | |
| 119 | turtle.turnLeft() | |
| 120 | maze[posX + 1][posY] = (turtle.detect() and 1 or 0) | |
| 121 | turtle.turnRight() | |
| 122 | end | |
| 123 | if maze[posX - 1][posY] == nil then -- right | |
| 124 | turtle.turnRight() | |
| 125 | maze[posX - 1][posY] = (turtle.detect() and 1 or 0) | |
| 126 | turtle.turnLeft() | |
| 127 | end | |
| 128 | return | |
| 129 | end | |
| 130 | ||
| 131 | if currentDirection == 4 then | |
| 132 | if maze[posX - 1][posY] == nil then -- front | |
| 133 | maze[posX - 1][posY] = (turtle.detect() and 1 or 0) | |
| 134 | end | |
| 135 | if maze[posX][posY - 1] == nil then -- left | |
| 136 | turtle.turnLeft() | |
| 137 | maze[posX][posY - 1] = (turtle.detect() and 1 or 0) | |
| 138 | turtle.turnRight() | |
| 139 | end | |
| 140 | if maze[posX][posY + 1] == nil then -- right | |
| 141 | turtle.turnRight() | |
| 142 | maze[posX][posY + 1] = (turtle.detect() and 1 or 0) | |
| 143 | turtle.turnLeft() | |
| 144 | end | |
| 145 | return | |
| 146 | end | |
| 147 | end | |
| 148 | ||
| 149 | ||
| 150 | function chooseDirection() | |
| 151 | ||
| 152 | if not hasPossibleDirection() then | |
| 153 | return false | |
| 154 | end | |
| 155 | ||
| 156 | while true do | |
| 157 | ||
| 158 | local number = math.random(1,4) | |
| 159 | ||
| 160 | if currentDirection == 1 then | |
| 161 | if number == 1 then | |
| 162 | if canMove(posX, posY + 1) then | |
| 163 | return true | |
| 164 | end | |
| 165 | end | |
| 166 | if number == 2 then | |
| 167 | if canMove(posX + 1, posY) then | |
| 168 | turtle.turnRight() | |
| 169 | currentDirection = 2 | |
| 170 | return true | |
| 171 | end | |
| 172 | end | |
| 173 | if number == 3 then | |
| 174 | if canMove(posX, posY - 1) then | |
| 175 | turtle.turnRight() | |
| 176 | turtle.turnRight() | |
| 177 | currentDirection = 3 | |
| 178 | return true | |
| 179 | end | |
| 180 | end | |
| 181 | if number == 4 then | |
| 182 | if canMove(posX - 1, posY) then | |
| 183 | turtle.turnLeft() | |
| 184 | currentDirection = 4 | |
| 185 | return true | |
| 186 | end | |
| 187 | end | |
| 188 | end | |
| 189 | ||
| 190 | if currentDirection == 2 then | |
| 191 | if number == 1 then | |
| 192 | if canMove(posX + 1, posY) then | |
| 193 | return true | |
| 194 | end | |
| 195 | end | |
| 196 | if number == 2 then | |
| 197 | if canMove(posX, posY - 1) then | |
| 198 | turtle.turnRight() | |
| 199 | currentDirection = 3 | |
| 200 | return true | |
| 201 | end | |
| 202 | end | |
| 203 | if number == 3 then | |
| 204 | if canMove(posX - 1, posY) then | |
| 205 | turtle.turnRight() | |
| 206 | turtle.turnRight() | |
| 207 | currentDirection = 4 | |
| 208 | return true | |
| 209 | end | |
| 210 | end | |
| 211 | if number == 4 then | |
| 212 | if canMove(posX, posY + 1) then | |
| 213 | turtle.turnLeft() | |
| 214 | currentDirection = 1 | |
| 215 | return true | |
| 216 | end | |
| 217 | end | |
| 218 | end | |
| 219 | ||
| 220 | if currentDirection == 3 then | |
| 221 | if number == 1 then | |
| 222 | if canMove(posX, posY - 1) then | |
| 223 | return true | |
| 224 | end | |
| 225 | end | |
| 226 | if number == 2 then | |
| 227 | if canMove(posX - 1, posY) then | |
| 228 | turtle.turnRight() | |
| 229 | currentDirection = 4 | |
| 230 | return true | |
| 231 | end | |
| 232 | end | |
| 233 | if number == 3 then | |
| 234 | if canMove(posX, posY + 1) then | |
| 235 | turtle.turnRight() | |
| 236 | turtle.turnRight() | |
| 237 | currentDirection = 1 | |
| 238 | return true | |
| 239 | end | |
| 240 | end | |
| 241 | if number == 4 then | |
| 242 | if canMove(posX + 1, posY) then | |
| 243 | turtle.turnLeft() | |
| 244 | currentDirection = 2 | |
| 245 | return true | |
| 246 | end | |
| 247 | end | |
| 248 | end | |
| 249 | ||
| 250 | if currentDirection == 4 then | |
| 251 | if number == 1 then | |
| 252 | if canMove(posX - 1, posY) then | |
| 253 | return true | |
| 254 | end | |
| 255 | end | |
| 256 | if number == 2 then | |
| 257 | if canMove(posX, posY + 1) then | |
| 258 | turtle.turnRight() | |
| 259 | currentDirection = 1 | |
| 260 | return true | |
| 261 | end | |
| 262 | end | |
| 263 | if number == 3 then | |
| 264 | if canMove(posX + 1, posY) then | |
| 265 | turtle.turnRight() | |
| 266 | turtle.turnRight() | |
| 267 | currentDirection = 2 | |
| 268 | return true | |
| 269 | end | |
| 270 | end | |
| 271 | if number == 4 then | |
| 272 | if canMove(posX, posY - 1) then | |
| 273 | turtle.turnLeft() | |
| 274 | currentDirection = 3 | |
| 275 | return true | |
| 276 | end | |
| 277 | end | |
| 278 | end | |
| 279 | ||
| 280 | end | |
| 281 | end | |
| 282 | ||
| 283 | ||
| 284 | function hasPossibleDirection() | |
| 285 | ||
| 286 | if not (canMove(posX, posY + 1)) and not (canMove(posX, posY - 1)) and not (canMove(posX + 1, posY)) and not (canMove(posX - 1, posY)) then | |
| 287 | return false | |
| 288 | end | |
| 289 | return true | |
| 290 | end | |
| 291 | ||
| 292 | ||
| 293 | function canMove(x, y) | |
| 294 | ||
| 295 | if x == 0 or x == mazeSize + 1 or y == 0 or y == mazeSize + 1 then | |
| 296 | return false | |
| 297 | end | |
| 298 | ||
| 299 | if (visited[x][y] == 1) or (maze[x][y] == 1) then | |
| 300 | return false | |
| 301 | end | |
| 302 | ||
| 303 | return true | |
| 304 | end | |
| 305 | ||
| 306 | ||
| 307 | function goBack() | |
| 308 | ||
| 309 | if (stackTrace[stackTraceCount] - currentDirection == 1) or (stackTrace[stackTraceCount] - currentDirection == -3) then | |
| 310 | turtle.turnRight() | |
| 311 | elseif stackTrace[stackTraceCount] ~= currentDirection then | |
| 312 | turtle.turnLeft() | |
| 313 | end | |
| 314 | ||
| 315 | currentDirection = stackTrace[stackTraceCount] | |
| 316 | ||
| 317 | if currentDirection == 1 then | |
| 318 | posY = posY - 1 | |
| 319 | end | |
| 320 | if currentDirection == 2 then | |
| 321 | posX = posX - 1 | |
| 322 | end | |
| 323 | if currentDirection == 3 then | |
| 324 | posY = posY + 1 | |
| 325 | end | |
| 326 | if currentDirection == 4 then | |
| 327 | posX = posX + 1 | |
| 328 | end | |
| 329 | ||
| 330 | stackTrace[stackTraceCount] = nil | |
| 331 | stackTraceCount = stackTraceCount - 1 | |
| 332 | ||
| 333 | turtle.back() | |
| 334 | end | |
| 335 | ||
| 336 | ||
| 337 | function saveToStackTrace() | |
| 338 | ||
| 339 | stackTraceCount = stackTraceCount + 1 | |
| 340 | stackTrace[stackTraceCount] = currentDirection | |
| 341 | end | |
| 342 | ||
| 343 | ||
| 344 | -- CODE | |
| 345 | ||
| 346 | init() | |
| 347 | ||
| 348 | forward() | |
| 349 | ||
| 350 | while not win() do | |
| 351 | ||
| 352 | mapPosition() | |
| 353 | ||
| 354 | if not hasPossibleDirection() then | |
| 355 | ||
| 356 | goBack() | |
| 357 | else | |
| 358 | ||
| 359 | chooseDirection() | |
| 360 | forward() | |
| 361 | end | |
| 362 | ||
| 363 | end | |
| 364 | ||
| 365 | turtle.up() | |
| 366 | print("FINISHED MODAFAKA")
| |
| 367 | ||
| 368 | sleep(2) | |
| 369 | ||
| 370 | turtle.down() | |
| 371 | ||
| 372 | while stackTraceCount > 0 do | |
| 373 | goBack() | |
| 374 | end |