<<Previous | ToC
| Next >>
1 | 2 | O
---+---+---
4 | X | 6
---+---+---
7 | 8 | 9
We decided first to iterate the three rows of the
board. Then inside that, we can now iterate the three columns.
In our English computer that would be:
Repeat 3
Repeat 3
Show this square
Show "|"
Next
Show "---+---+---"
Next
Done
This doesn't work quite right, so let's think about the problems.
Or, because I'm lazy, I might try to run it and see what it looks like.
But first I need to do something about the "Show this square"
line, either make another stub subroutine, or (if it's simple enough) replace
it with one of the five things the computer knows how to do.
First we need some way to step the "this square" part through
the board array. Recall that the squares are numbered from 1 to 9, so I
can create a
Variable step = 0at the beginning of the display subroutine (before any iteration), and then before we want to touch that next board cell,
Add 1 to stepThen we can use the variable step to index the board array, something like
Show board[step]That isn't exactly Java, and it isn't the English of the English computer, but you get the idea.
"Show current board"Now obviously this doesn't draw a TTT board at all, it just does 21 lines, the nine numbers, each followed by a vertical bar on its own line, then three horizontal row markers after the third number in each row. Java has a way to print several things on the same line, but in different commands, but English does not. The easy solution (you can do this in Java also) is to build a single line of output for each row:
print "Show current board"
Variable step = 0
Repeat 3
Repeat 3
Add 1 to step
Character square = board,step
Show square
Show "|"
Next
Show "---+---+---"
Next
Done
Repeat 3That's better, but the horizontal spacing is crowded. The correct game board has spaces around each digit
let line = ""
Repeat 3
Add 1 to step
Character square = board,step
let line = line # square
let line = line # "|"
Next
Show line
Show "---+---+---"
Repeat 3Also, a real TTT board has vertical and horizontal lines only between the lines and columns, but this does it also to the right and below. We can fix that with a conditional in each case, by counting the columns or lines:
Add 1 to step
Character square = board,step
let line = line # " " # square # " "
let line = line # "|"
Next
let cols = 0You can do the rows. If you think about it, you don't need a new row variable. Why?
Repeat 3
Add 1 to step
Add 1 to cols
Character square = board,step
let line = line # " " # square # " "
if cols<3 let line = line # "|"
Next
Think about your answer, then turn the page.
<<Previous | ToC | Next >>
Revised: 2021 October 2