Grid Words
We're going to write some words that will make it a little easier to
set/check values in "grid".
| Editor |
to grid.get :i ; ; output the grid item "X "O "? ; output item :i :grid end |
The word "grid.get" will return what is at the current grid
position.
| Editor |
to grid.put :i :c ; ; sets the grid item to :c ; setitem :i :grid :c end |
The word "grid.put" will allow us to remember what is at the
current grid position
| Editor |
to grid.empty :i ; ; output "true if specified position in grid is empty ; output (grid.get :i) = "? end |
This will return "true" or "false", indicating if the
specified position in the grid is empty. "true" is another way of
saying "yes". "false" is a way of saying "no".
| Editor |
to grid.me :i ; ; output "true if the grid item is current player ; output (grid.get :i) = :turn end |
This will return "true" or "false", indicating if the
specified position in the grid contains the same type of playing piece as whose
turn it is.
| Editor |
to grid.other :i
;
; output "true if the grid item is opponent
;
output not (OR grid.empty :i grid.me :i)
end
|
This will return "true" or "false", indicating if the
specified position in the grid contains the opponents playing piece (i.e., if
there is no piece, or the piece is me, then it is not the opponent). Another way
of doing this would be "output AND (not grid.empty :i not grid.me :i)"
Let's test these:
| Commander |
init show :turn O show grid.get 2 ? grid.put 2 "X show grid.get 2 X show :grid {? ? X ? ? ? ? ? ?} grid.put 3 "O show grid.get 3 O show grid.empty 2 false show grid.empty 3 false show grid.empty 4 true show grid.me 2 false show grid.me 3 true show grid.me 4 false show grid.other 2 true show grid.other 3 false show grid.other 4 false |
Changing Turn
This is a word to change whose turn it is. This word "flip" will
change "turn" from "O" to "X" or "X" to
"O". Note the "ifelse" command. If the condition is
"true" (it is currently turn "O") then do the first command
(in square brackets) otherwise (else) do the second command. In this case, we'll
change the turn to "X" or "O" respectively.
| Editor |
to flip ; ; change player ; these must be capital O and capital X ; ifelse (:turn = "O) [make "turn "X] [make "turn "O] end |
Let's test this:
| Commander |
init show :turn O flip show :turn X flip show :turn O |
Piece Placement
We'll define the word "place" to place the
"O" or "X" (depending on who's turn it is) on the screen in
the correct box, and record it internally. Here we do a neat trick. We take the
word "O" or "X" specified in "turn" and execute
that word. If you remember, we defined the two words earlier to draw an
"X" or "O" respectively. The word "run" will take
a list, and will treat it as a command. The list in this case has the word
stored in "turn" and the value you specify after the word
"place".
| Editor |
to place :i ; ; place current piece and mark it in grid ; grid.put :i :turn run (list :turn :i) end |
Try this out, make sure that we place an "O" in
box number 7 (center top):
| Commander |
show :turn O show grid.empty 7 true place 7 show grid.get 7 O |

|