Minesweeper is the addictive logic game that comes with Windows:
Each button is a patch in a minefield that either is or isn't mined. When clicked, an unmined patch reveals the number of its neighbors that are unmined.
In the NetLogo version minefield patches are patches in thee turtle world:
To randomly mine the patches, the init button is pressed. The slider controls the percentage of patches that will bee mined. The more the harder. To start the timer and to begin listening for the mouse, the start button is pressed.
Clicking on an unmined patch turns the patch green, reveals the number of unmined neighbors, and reduces the number of umined patches left. Clicking on a mined patch turns the patch red, sets its label to an asterisks, makes a beep sound, prints "Boom!" in the command center, and increments the number exploded.
The goal of the game is to uncover all unmined patches in the shortest amount of time (as measured by the ticks counter) while minimizing the number exploded. (In the Windows version the game ends when a mine is exploded.)
Here's a link that might work: Minesweep.
Every patch has certain pre-defined properties:
pxcor = the x-coordinate (column)
pycor = the y-coordinate (row)
pcolor = the color
plabel = the string label
For example, here's how we can label each patch with a question mark:
ask patches [set plabel "?"]
Programmers can also define new patch properties. This is done at the top of the program file:
patches-own [mined?] ; = true if patch is mined, false otherwise
We can use the of command to see the value of a patch property within
print [mined?] of patch 0 0
We can use the with command to create a set of patches having some property:
count patches with [pcolor = red] ; = # of exploded mines
Of course the same is true of turtles.
The update procedure will be tied to the start button:
Notice that the forever box is checked. That means NetLogo will call the update procedure over and over again (until the start button is clicked again).
Here's the skeleton of the update procedure:
to update
tick
if mouse-down?
[
let p patch round mouse-xcor round
mouse-ycor
ifelse [mined?] of p
[
; ???
]
[
; ???
]
]
end
Turtle world has a clock (which is displayed at the top of the window.) The tick procedure advances the clock by one tick.
The mouse-down? procedure reports true if it is called while the mouse button is down.
The mouse-xcor and mouse-ycor procedures report the position of the mouse when its button went down. We round these numbers off to get the patch, p, under the mouse when its button was clicked.
What should happen if the patch was mined? Un-mined?
A reporter is a special type of procedure that reports the value of some important property. For example, the following reporter reports the number of exploded patches:
to-report num-exploded
report count patches with [pcolor =
red]
end
Not that use to-report instead of to to begin the definition and report to output the result.
We can associate a reporter with a user interface monitor that will call the reporter and displays its value each time update is called:
Minesweeper allows players to set the percentage of mined patches. Which patches are mined will be random. How is this done?
NetLogo has a procedure that generates random numbers:
random N ; generates a random integer from 0 up to but not including N
The following code generates a random number for each patch. Suppose the player wants 20% mined. If the random number generator is "fair", then when generating a random number below 100, it should generate a number below 20 about 20% of the time. Here it is in NetLogo:
ask patches
[
ifelse random 100 < percentMined
[set mined? true]
[set mined? false]
]