<<Previous | ToC | Next >>
You don't need a new row variable, because the step variable
will have counted up to 9 at the time you don't want to print the last
divider line. Or you can use a separate variable, counted up each row,
either way works fine.
But it's not really enough. We need to do something with the variable that has the user play. It also helps to remind the players whose turn it is, which is in the variable who, initialized to "X". We have not actually written the Java code for printing the board, but in the English computer, all I did is print out the contents of the board variable for that cell, which was initialized with the nine board cell numbers.
So when X plays, if we put an X in that position of the board, we are done with input:
Input playActually, we have a subroutine to do that. You could move this single line of English into the "Update board" subroutine, or else just leave the new line here and delete both the subroutine and its call. I moved it to the subroutine because this one line is neither good Java nor good English. If it gets bigger, the subroutine is easier to see what is going on. An important part of being a good programmer is knowing what every part of your program does. The human mind has a limited attention span, about seven concepts at once, so smaller subroutines help focus your mental resources on a chunk that is the right size for your mind to grasp everything.
put who into board[play]
We will get to doing all this in Java in the next chapter -- there are significant differences that our translation (from English to Java) must overcome, and it helps (but is not necessary) to have tested the algorithm first in a (mostly) understandable language like English -- so now for a couple more paragraphs, I will look at running the new lines in English. You can skip to there if you wish to omit thinking about it in the English computer. Me, I think that the more thought you give to your program here in the design phase, the fewer errors there will be in your final (Java) program to iron out the hard way. Obviously, a lot of programmers have a different opinion; otherwise they wouldn't be programming in C, which is a "shoot (jump in and code) first, then ask questions later" kind of language. People can die that way, especially with life-critical software like medical and weapons and self-driving vehicles. I want all the help I can get from my tools.
Mostly I need to convert the generic array reference to the "Substring" command again. Replacing (for example) the contents of cell #3 in the array amounts to capturing the first three characters of my text string, concatenating the play value ('X' or 'O'), then concatenating all the rest of the line onto the end, like this:
print "Update board: " who " => [" play "]"
Substring frnt = board,0,play
Substring back = board,play+1,9
put frnt # who # back into board
If you run this in the English computer, the program displays a nice board, and takes input and displays the updated board, just like we wanted. Or does it? What went wrong? Why do you think it did that?
Think about your answer, then turn the page.
<<Previous | ToC | Next >>
Revised: 2021 August 30