Programming Tic-Tac-Toe in Java


<<Previous | ToC | Next >>

Iterating the Board

Here's the board again after a couple plays, so we can see what needs to be done:
 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 = 0
at the beginning of the display subroutine (before any iteration), and then before we want to touch that next board cell,
Add 1 to step
Then 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.

Me, I want to run this right now (you can skip this if you are otherwise inclined, but you will hit the same problems a little later in the development process) so I'm going to use the "Character" command in the English computer to extract the single character at position step of the variable board, which I initialized to be "0123456789", so I changed this line to something the English computer will run, and the whole subroutine looks like this:
"Show current board"
  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
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:
  Repeat 3
    let line = ""
    Repeat 3
      Add 1 to step
      Character square = board,step
      let line = line # square
      let line = line # "|"
      Next
    Show line
    Show "---+---+---"
That's better, but the horizontal spacing is crowded. The correct game board has spaces around each digit
    Repeat 3
      Add 1 to step
      Character square = board,step
      let line = line # " " # square # " "
      let line = line # "|"
      Next
Also, 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:
    let cols = 0
    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
You can do the rows. If you think about it, you don't need a new row variable. Why?

Think about your answer, then turn the page.

<<Previous | ToC | Next >>

Revised: 2021 October 2