Snakes and Ladders

Snakes and Ladders preview image

4 collaborators

Stefan Dulman (Advisor)
Roger Beeden (Advisor)
Dieter Tracey (Advisor)

Tags

coral reef 

Tagged by Stuart Kininmonth over 11 years ago

ecology 

Tagged by Stuart Kininmonth over 11 years ago

game simulation 

Tagged by Stuart Kininmonth over 11 years ago

marine management 

Tagged by Stuart Kininmonth over 11 years ago

Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.1 • Viewed 1094 times • Downloaded 43 times • Run 0 times
Download the 'Snakes and Ladders' modelDownload this modelEmbed this model

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


Info tab cannot be displayed because of an encoding error

Comments and Questions

on line note

just a quick note that this model will need to be downloaded to work since it contains extensions. importantly there is a background image that will be missing. Contact me for a zip file with both (stuart@kininmonth.com.au) but note the Modelling Commons engineers are trying some upgrades to help fix this problem.

Posted over 11 years ago

all up and runing

Thanks to the magnificent efforts of Reuven the model is now working online. Allow some extra time to upload and there will be 2 permissions screens.

Posted over 11 years ago

Click to Run Model

extensions [array bitmap]


;; ----------------------------------------------------------------
breed [helplines helpline]
breed [swarmants swarmant]

globals [
  nrows
  ncols
  posturtle
  steps
  meanvalue
  unfinishedvalue
  vstd
  timeout 
]
patches-own [jumpv]
swarmants-own [antpos]
helplines-own [temp2]

;; ----------------------------------------------------------------

;; startup definition

to startup
  
  __clear-all-and-reset-ticks
  
  set nladders 10
  set nsnakes 10
  set nrows 10
  set ncols 10
  
  ; define world size
  set-patch-size 30
  resize-world 0 (nrows + 1) 0 (ncols + 1)
  bitmap:copy-to-drawing (bitmap:import "board.jpg") 0 0

  set meanvalue 0
  set unfinishedvalue 0

  set vstd 3
  set timeout 1000
  
  ;set showunfinishedgames true
  
  reset-ticks
end 

;; ----------------------------------------------------------------

;; generate snakes and ladders - on a square grid

to generatesnakesladders
  
  ; pick the heads of snakes and ladders
  let temp shuffle filter [? > 0] n-values (nrows * ncols - 1) [?]
  let lheads sublist temp 0 nladders
  let sheads sublist temp nladders (nladders + nsnakes)

  set-default-shape helplines "dot"
  create-helplines 1
  
  ; compute the falls
  foreach sheads [
    ask patch (1 + getcolfromindex ?) (1 + getrowfromindex ?) [
      
      let adjmean averagelength
      let adjstd vstd
      if (averagelength + 3 * vstd) > ? [
        let k ? / (averagelength + 3 * vstd)
        set adjmean averagelength * k
        set adjstd vstd * k
      ]
      
      let a round random-normal adjmean adjstd
      while [a > ?] [
        set a round random-normal adjmean adjstd  
      ]
      if a <= 0 [set a 1]

      set jumpv ? - 1 * a

      ;; draw the line
      ask helplines [
        move-to myself
        set temp2 [jumpv] of myself
        set color [255 0 0]
        set pen-size 9
        set size 1.3
        stamp
        pen-down
        setxy (1 + getcolfromindex temp2) (1 + getrowfromindex temp2)
        pen-up
      ]
      
      ;; draw the line
      ask helplines [
        move-to myself
        set temp2 [jumpv] of myself
        set color [255 255 255]
        set pen-size 3
        set size 0.8
        stamp
        pen-down
        setxy (1 + getcolfromindex temp2) (1 + getrowfromindex temp2)
        pen-up
      ]
    ]
  ]
  
  
    ; compute the jumps
  foreach lheads [
    ask patch (1 + getcolfromindex ?) (1 + getrowfromindex ?) [
      
      let adjmean averagelength
      let adjstd vstd
      if (averagelength + 3 * vstd + ?) > nrows * ncols [
        let k (nrows * ncols - ?) / (averagelength + 3 * vstd)
        set adjmean averagelength * k
        set adjstd vstd * k
      ]
      
      let a round random-normal adjmean adjstd
      while [( a + ? ) >= (nrows * ncols)] [
        set a round random-normal adjmean adjstd
      ]
      
      ;if ( a + ? ) >= (nrows * ncols) [ set a (nrows * ncols) - ? - 1]
      if a <= 0 [set a 1]
      set jumpv ? + a
      
      ;; draw the line
      ask helplines [
        move-to myself
        set temp2 [jumpv] of myself
        set color [0 0 0]
        set pen-size 10
        pen-down
        setxy (1 + getcolfromindex temp2) (1 + getrowfromindex temp2)
        pen-up
      ]
      
      ask helplines [
        move-to myself
        set temp2 [jumpv] of myself
        set color [255 255 255]
        set pen-size 3
        pen-down
        setxy (1 + getcolfromindex temp2) (1 + getrowfromindex temp2)
        pen-up
       
      ]
    ]
  ]
end 


;; ----------------------------------------------------------------



;; ----------------------------------------------------------------
;; get the column coordinate from an index

to-report getcolfromindex [index] 
    let row getrowfromindex index
    ifelse (row mod 2) = 0 [
      ;; we are counting from left to right
      report index mod ncols
    ] [
      ;; we are counting from right to left
      report ncols - 1 - (index mod ncols)
    ]
end 

;; get the row coordinate from an index

to-report getrowfromindex [index] 
    report floor (index / ncols)
end 

;; ----------------------------------------------------------------

to rolldice
  
  set steps steps + 1
  
  ;; roll the dice
  let dice (1 + random 6)
  
  ;; advance the turtle
  set posturtle posturtle + dice
  
  ;; check if we are done
  ifelse posturtle >= (ncols * nrows - 1) [
    show (word "simulation done. number of steps: " steps)
    set posturtle 0
  ] [
    ;; account for ladders and snakes
    set posturtle [jumpv] of patch (1 + getcolfromindex posturtle) (1 + getrowfromindex posturtle)
  ]
  
  ;; update the heading of the turtle
  let row getrowfromindex posturtle
  ifelse (row mod 2) = 0 [
    ask turtles [set heading 90]
  ] [
    ask turtles [set heading 270]
  ]
  if posturtle = (ncols * nrows - 1) [
    ask turtles [set heading 0]
  ]

  ;; update the position of the turtle
  ask turtles [set xcor 1 + getcolfromindex posturtle]
  ask turtles [set ycor 1 + getrowfromindex posturtle]
  
  ;; update the display
  show (word "dice roll: " dice)
  show (word "position: " posturtle)
  show ""
end 

;; ----------------------------------------------------------------

;; setup button

to setup
  
  __clear-all-and-reset-ticks
  
  let i 0
  
  set nrows 10
  set ncols 10
  ; define world size
  set-patch-size 30
  resize-world 0 (nrows + 1) 0 (ncols + 1)
  bitmap:copy-to-drawing (bitmap:import "board.jpg") 0 0

  set steps 0
  
  ;set showunfinishedgames true
  set vstd 3
  set timeout 1000
  
  clear-patches
  
  ;; fill in the default values
  foreach n-values (nrows * ncols) [?] [
    ask patch (1 + getcolfromindex ?) (1 + getrowfromindex ?) [set jumpv ?]
  ]
    
  clear-output
  
  ;; generate a new setup
  generatesnakesladders
  
  ;; place a turtle at the origin
  set posturtle 0
  
  ask turtles [die]
  create-turtles 1
  ask turtles [
    setxy 1 1
    set heading 90
    set color yellow
  ]
end 

;; ----------------------------------------------------------------

;; function generates a bunch of ants and runs statistics

to runswarm
  
  let clock 0
  let i 0
  let statexit []
  
  clear-all-plots
  reset-ticks
  
  ; clear additional variables
  set meanvalue 0
  set unfinishedvalue 0
  
  ; disable display
  no-display
  
  create-swarmants ngames
  ask swarmants [
    set antpos 0
  ]
  
  while [clock < timeout] [
    
    ask swarmants [
      
      ;; roll the dice
      let dice (1 + random 6)
  
      ;; advance the ant
      set antpos antpos + dice
  
      ;; check if we are done
      ifelse antpos >= (ncols * nrows - 1) [
        ;; record the statistics
        set statexit lput clock statexit
        die
      ] [
        ;; account for ladders and snakes
        set antpos [jumpv] of patch (1 + getcolfromindex antpos) (1 + getrowfromindex antpos)
      ]
      
    ]
    
    set-current-plot "Number of Finished Games versus Number of Rolls"
    ifelse empty? statexit [
    ] [
      set-plot-x-range 1 last statexit
      histogram statexit
    ]
    
    set clock clock + 1
    tick
  ]
  
  ;; update variables
  ifelse length statexit = 0 [
    set meanvalue 0 
  ] [
    set meanvalue mean statexit
  ]
  
  ; update the unfinished games variable
  set unfinishedvalue unfinishedvalue + count swarmants
  
  set-current-plot "Number of Finished Games versus Number of Rolls"
  set-plot-x-range 1 (timeout + 1)
  histogram statexit
  
  ;; kill all ants
  ask swarmants [die]
  
  ; enable the display again
  display
end 

There is only one version of this model, created over 11 years ago by Stuart Kininmonth.

Attached files

File Type Description Last updated
board.jpg jpeg background image over 11 years ago, by Stuart Kininmonth Download
Snakes and Ladders.png preview Preview for 'Snakes and Ladders' over 11 years ago, by Stuart Kininmonth Download

This model does not have any ancestors.

This model does not have any descendants.