Maze Game

Maze Game preview image

1 collaborator

Cover Benjamin Menashe (Author)

Tags

game 

Tagged by Benjamin Menashe about 5 years ago

games 

Tagged by Benjamin Menashe about 5 years ago

labyrinth 

Tagged by Benjamin Menashe about 5 years ago

maze 

Tagged by Benjamin Menashe about 5 years ago

mazes 

Tagged by Benjamin Menashe about 5 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 6.1.1 • Viewed 513 times • Downloaded 33 times • Run 0 times
Download the 'Maze Game' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Click to Run Model

breed [ pacmans pacman ]
globals [ my-time ]

;; create maze
patches-own [ xxx ]

to setup
  clear-all

  ;; set white left and right sides
  ask patches with [count neighbors != 8]
    [ set xxx 1 ]
  ask patches with [pxcor = min-pxcor + 1] [set pcolor white]
  ask patches with [pxcor = max-pxcor - 1] [set pcolor white]

  ;; set top and bottom white
  ask patches with [pycor = max-pycor or pycor = min-pycor] [ set pcolor white ]
  clean-corners

  setup-start-finish ;set start and finish red patches

  ;; sprout turtles
  ask n-of edges (patches with [pcolor = white]) [
    sprout 1
      [ set color blue
        facexy -1 * pxcor pycor
        fd 1 ]
  ]
  reset-ticks
end 

to setup-start-finish
  ;; setup the red patches at both ends
  ask patches with [pxcor = max-pxcor and pycor = Right-Height] [
    set pcolor red
    ask neighbors [ set pcolor black ]
    ask patch-at 0 2 [ set pcolor white ]
    ask patch-at 0 -2 [ set pcolor white ]
  ]
  ask patches with [pxcor = min-pxcor and pycor = Left-Height] [
    set pcolor red
    ask neighbors [ set pcolor black ]
    ask patch-at 0 2 [ set pcolor white ]
    ask patch-at 0 -2 [ set pcolor white ]
  ]
end 

to clean-corners
  ;; just delete the corners from being white
  ask patches with [pycor = max-pycor and pxcor = max-pxcor ] [ set pcolor black ]
  ask patches with [pycor = max-pycor and pxcor = min-pxcor ] [ set pcolor black ]
  ask patches with [pycor = min-pycor and pxcor = max-pxcor ] [ set pcolor black ]
  ask patches with [pycor = min-pycor and pxcor = min-pxcor ] [ set pcolor black ]
end 

to go
  ;; die if neighbors are turtles
  ask turtles [
    if count turtles-on neighbors > 0
    [ die ]
  ]

  ;; turn if facing edge
  ask turtles [
    if patch-ahead 1 = nobody
    or
    patch-left-and-ahead 45 1 = nobody
    or
    patch-right-and-ahead 45 1 = nobody
    [ rt 180 ]
  ]

  ;; die if facing white
  ask turtles [
    if [pcolor] of patch-ahead 1 = white
    or
    [pcolor] of patch-left-and-ahead 45 1 = white
    or
    [pcolor] of patch-right-and-ahead 45 1 = white
    or
    [pcolor] of patch-left-and-ahead 90 1 = white
    or
    [pcolor] of patch-right-and-ahead 90 1 = white
    [ die ]
  ]


  ;; if not dead, move to random black spot with xxx = 0
  ask turtles [
    set pcolor white
    set xxx 1
    downhill4 xxx
    ;; mark the black spots connected to the move with xxx = 1 so no turtle will move their
    ask patch-left-and-ahead 135 1 [ set xxx 1 ]
    ask patch-right-and-ahead 135 1 [ set xxx 1 ]
  ]

  ;; sprout new turtles
  ask n-of edges (patches with [pcolor = white])
  [
    sprout 1
      [ set color blue
        let yyy random 4
        if yyy > 2.5
        [ set heading 0 ]
        if yyy > 1.5 and yyy < 2.5
        [ set heading 90 ]
        if yyy > 0.5 and yyy < 1.5
        [ set heading 180 ]
        if yyy > -0.5 and yyy < 0.5
        [ set heading 270 ]
        ;; turn around if facing edge
        ifelse patch-ahead 1 = nobody
        [ rt 180 fd 1]
        [ fd 1]
    ]
  ]
  tick
end 

to kill
  ;; once maze is pretty, remove al current turtles
  ask turtles [ die ]
  ;; also clean the start and finish spots a bit
  ask patches with [ pcolor = red ]
  [ask neighbors4 [ set pcolor black ]
  ]
end 

to-report edges
  ;; for how many turtles build the maze - the more the easier
    if Difficulty = "Easy" [ report count patches with [count neighbors != 8] - 6 ]
    if Difficulty = "Medium" [ report max-pycor / 3 ]
    if Difficulty = "Hard" [ report 1 ]
end 
;; end of maze creation

;; start to solve

to play
  kill
  ;; place the pacman at start
  create-pacmans 1 [
    set shape "turtle" set color green set size 1.5
    setxy max-pxcor Right-Height set heading 270
  pen-down
  ]
  reset-timer
end 

to move-pacman
    ask pacmans [
    if (patch-ahead 1 != nobody) and ([pcolor] of patch-ahead 1 != white)
    [ fd 1 ]
    if xcor = min-pxcor and ycor = Left-Height [
      set my-time timer
      user-message (word "Great job! Your time is: " my-time "sec")
    ]
  ]
end 

to move-up
  ask pacmans [ set heading 0 ]
  move-pacman
end 

to move-right
  ask pacmans [ set heading 90 ]
  move-pacman
end 

to move-down
  ask pacmans [ set heading 180 ]
  move-pacman
end 

to move-left
  ask pacmans [ set heading 270 ]
  move-pacman
end 

to restart
  clear-drawing
  ask pacmans [ die ]
  play
end 

There are 2 versions of this model.

Uploaded by When Description Download
Benjamin Menashe about 5 years ago added timer to user message Download this version
Benjamin Menashe about 5 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Maze Game.png preview Preview for 'Maze Game' about 5 years ago, by Benjamin Menashe Download

This model does not have any ancestors.

This model does not have any descendants.