Programming Tic-Tac-Toe in Java


<<Previous | ToC | Next >>
 

Data Structures

Before we can display or update the board, we need to know what the board looks like, and more importantly, what kind of variable to use. Tic-Tac-Toe is played on a 3x3 square board, which (when it is useful, like today) is commonly numbered from 1 to 9, like so:
 1 | 2 | 3
---+---+---
 4 | 5 | 6
---+---+---
 7 | 8 | 9


Actually, the lines on a real board are solid. This is sort of a hint at how we will display the board using ASCII graphics. Later, when we do it in the GameEngine, we can use solid lines. Each time somebody plays, we replace that square's number with the X or O that was played there. Whenever the human plays, they will type in a single digit (or in the GameEngine, click on that square, in which case you don't need to show numbers), and the computer can then update the board from that input.

So what kind of variable can we use to keep track of who played where?

If you answered "array" you are on your way to becoming a good programmer. We have nine squares all alike and numbered sequentially, so an array is a natural implementation. What kind of array? The numbering is sequential from 1 to 9, which suggests a single-dimension array. However, the display is 2-dimensional, and when we start to think about scoring, that is also 2-dimensional. Usually when faced with such a decision, I pick one, then later discover that the program is really messy that way, so I throw all that code away and rewrite it the other way. Once or twice in my life I did that and got into writing it the other way, and that was even messier, so I went back to the first. I never throw code away, I just save it off into a file that (maybe) I will never look at again. Until I need to look at what I threw away, then I have it to look at. File space is cheap. About 30 or 40 years ago it became cheaper than paper (for equivalent storage).

I'm not going to tell you which is easier. It might be a matter of personal preference. Single-dimensioned arrays are slightly faster, but not so much that you can tell without a stopwatch in this game (and maybe not even then). But I do a lot of programs where speed matters, so I will do mine with a single-dimension array. But at the same time I will help you do it in a 2-dim array, so you can experience both, which is a good idea if you want to do this professionally.

The Kitchen computer does not have arrays, so for this test (which you do not need to follow) I will use a single line of text:

let board = "0123456789"


In Java you'd write:

static int[] board = new int[10];
or:
static int[][] board = new int[3][3];
but that's getting ahead of ourselves. We don't really know what data type it should be until after we know what data we expect to store in it.

So now let's write the display routine. Which one of the five concepts do you need to apply first?

Think about your answer, then turn the page.

<<Previous | ToC | Next >>

Revised: 2021 July 2