CS108L Computer Science for All Week 5: Sample Lab Program 2

CS108L Computer Science for All
Week 5: Sample Lab Program 2
;;===========================================================
;;
setup_2Black
;;===========================================================
to setup_2Black
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask patch -10 0 [ set pcolor black ]
ask patch 10 0 [ set pcolor black ]
create-turtles 1
[
setxy 6 0
set heading 270 ;;Facing directly left (west)
Document1
]
set color white
pen-down
end
;;===========================================================
;;
setup_4Red
;;===========================================================
to setup_4Red
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask patch
ask patch
ask patch
ask patch
0 0 [ set pcolor red ]
1 10 [ set pcolor red ]
10 9 [ set pcolor red ]
9 -1 [ set pcolor red ]
create-turtles 1
[
setxy 15 0 ;;Starting OUTSIDE the box, but will move onto the box
set heading 270 ;;Facing directly left (west)
set color white
pen-down
]
end
;;===========================================================
;;
setup_L
;;===========================================================
to setup_L
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask patch 0 0 [ set pcolor red ]
ask patch 1 10 [ set pcolor black ]
ask patch 1 -1 [ set pcolor blue ]
Document1
ask patch 10 0 [ set pcolor black ]
create-turtles 1
[
setxy 5 0 ;;Must start INSIDE the L
set heading 270 ;;Facing directly left (west)
set color white
pen-down
]
end
;;===========================================================
;;
setup_L_TwoTurtles
;;===========================================================
to setup_L_TwoTurtles
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask patch
ask patch
ask patch
ask patch
0 0 [ set pcolor red ]
1 10 [ set pcolor black ]
1 -1 [ set pcolor blue ]
10 0 [ set pcolor black ]
create-turtles 2
[
if (who = 0)
[ setxy 5 0
;;Must start INSIDE the L
set heading 270 ;;Facing directly left (west)
]
if (who = 1)
[ setxy 6 0
set heading 90 ;;Facing directly right (east)
]
set color white
pen-down
]
end
Document1
;;===========================================================
;;
setupCrazy
;;===========================================================
to setupCrazy
clear-all
reset-ticks
ask patches
[
set pcolor green
]
;;Path of turtle 0
ask patch -3 -4 [ set pcolor blue ]
ask patch 12 -3 [ set pcolor blue ]
ask patch 11 9 [ set pcolor blue ]
ask patch -5 8 [ set pcolor blue ]
ask patch -4 2 [ set pcolor blue ]
ask patch 8 3 [ set pcolor red ]
ask patch 7 -1 [ set pcolor red ]
ask patch -4 0 [ set pcolor blue ]
;;Bumpers to fix turtle 0 when it gets turns to avoid other turtles
ask patch 16 0 [ set pcolor black ]
ask patch 13 8 [ set pcolor black ]
ask patch -8 -3 [ set pcolor black ]
ask patch -8 3 [ set pcolor black ]
ask patch -3 14 [ set pcolor black ]
ask patch -4 14 [ set pcolor black ]
;;Path for turtle 1
ask patch 4 13 [ set pcolor black ]
ask patch 4 -8 [ set pcolor black ]
;;Path to turtle 2
ask patch -10 15 [ set pcolor blue ]
ask patch -9 5 [ set pcolor blue ]
ask patch 0 6 [ set pcolor blue ]
ask patch -1 16 [ set pcolor blue ]
;;Bumpers to fix turtle 2 when it gets turns to avoid turtle 0
ask patch -16 6 [ set pcolor black ]
ask patch -1 1 [ set pcolor black ]
;;Path for turtle 4
ask patch -9 -8 [ set pcolor red ]
ask patch -8 -9 [ set pcolor blue ]
Document1
create-turtles 5
[
if (who = 0)
[ setxy -3 -1
set heading 180
]
if (who = 1)
[ setxy 4 10
set heading 0
]
if (who = 2)
[ setxy -9 10
set heading 180
]
if (who = 3)
[ setxy -8 -8
set heading 90
]
if (who = 4)
[ setxy -8 -8
set heading 0
]
set color white
pen-down
]
end
;;===========================================================
;;
partlyWorking_Go
;;
;; if Patch ahead is black, reverse direction
;; if patch ahead has other turtle, reverse direction
;; if patch ahead is green, move forward 1
;;
;; This partly working version does not do anything when the
;; patch ahead is blue or red.
;;===========================================================
to partlyWorking_Go_WithComments
ask turtles
[
let colorOfPatchAhead green
ask patch-ahead 1
;;gets the patch that is one square ahead.
[
Document1
]
set colorOfPatchAhead pcolor ;;Normally when inside an ask turtles, pcolor is the
;; color of the patch the turtle is on.
;;Here, however, the code is inside an ask patch-ahead
;; which is inside and ask turtles.
;;Thus, pcolor is the patch color of the patch that is
;; one square ahead of the current turtle.
if (colorOfPatchAhead = black)
[
left 180
]
if (colorOfPatchAhead = green)
[
;;This is non-deterministic:
;; If a pair of turtles are two steps away and headed toward each other, then
;; whichever turtle moves first will be able to move one nearer, but when
;; the second turtle tries to move it will see a turtle one ahead.
ifelse (any? turtles-on patch-ahead 1)
[ left 180
]
[ forward 1
]
]
]
tick
end
;;===========================================================
;;
partlyWorking_Go
;;===========================================================
to partlyWorking_Go
ask turtles
[
let colorOfPatchAhead green
ask patch-ahead 1
;;reports patch that is 1 ahead.
[
set colorOfPatchAhead pcolor;;pcolor is the color of the patch 1 ahead.
]
if (colorOfPatchAhead = black)
[
Document1
]
left 180
if (colorOfPatchAhead = green)
[
ifelse (any? turtles-on patch-ahead 1)
[
left 180
]
[
forward 1
]
]
]
tick
end
;;===========================================================
;;
Go
;;===========================================================
to Go
ask turtles
[
let colorOfPatchAhead green
ask patch-ahead 1
[ set colorOfPatchAhead pcolor
]
if (colorOfPatchAhead = black)
[
left 180
]
if (colorOfPatchAhead = blue)
[
left 90
]
if (colorOfPatchAhead = red)
[
right 90
]
if (colorOfPatchAhead = green)
[
Document1
;;This is non-deterministic:
;; If a pair of turtles are two steps away and headed toward each other, then
;; whichever turtle moves first will be able to move one nearer, but when
;; the second turtle tries to move it will see a turtle one ahead.
ifelse (any? turtles-on patch-ahead 1)
[ left 180
]
[ forward 1
]
]
]
tick
end
Document1