Programming Tic-Tac-Toe in Java


<<Previous | ToC | Next >>

This tutorial is probably the third rewrite for this workshop, the first two were too hard to understand. Maybe this is too, so I did a sequence of videos (see links in the ToC). The code came out different each time. When you write a program from scratch it comes out different every time. And different from everybody else's program doing the same thing, it's like a fingerprint. That's how I can tell if my students wrote their own code or copied somebody else's.

If you watch the videos after you have done the previous programs, you should be able to get TTT working.

If you read (this) text after you have done the previous programs, you should be able to get TTT working.

If you skipped or slacked through the previous programs, you will have trouble with both the video and the text TTT.

If you did all the previous work, but you are still struggling with converting ideas into code, then you might still have trouble with the video TTT. It invites you to look at the text (that would be here), but it's different. That is intentional. This is about ideas, and ideas have many ways of expressing the same idea as code. You need to understand the difference between ideas and particular ways of expressing those ideas. That's what the previous programming exercises are for, to help you think your way through the transition from ideas to code. You need to understand all that. If necessary, go back and work through them again. If one of them is too hard, you need to spend the time, don't skip it. Ask for help, I need to know where the hard parts are, so I can make them better.

So watch the videos, then read the text, get the ideas in yourr head, then go to the IDE and write your own code from start to finish without looking at my code at all. If you can make it work, Bravo! If not, then pick out one place where it's not doing what you want, then come back and read about that part only. If that didn't help, ask another programmer, another student in your workshop, or else come get a mentor's help. But you get to be a good programmer by doing it, much less by seeing other people doing it (unless you are stuck). We are here to get you unstuck.
 

ToC This Program

Setting Goals
Displaying the Board
Input Play
Who Won
ASCII Score in Java
Score in GameEngine
AI to Win
Data As Bits

Setting Goals

Some board games are played with dumb luck, others are pure strategy, many games are somewhere in between. Programming a computer to play dumb luck may require significant programming skills, but the part of it that amounts to intelligent decisions is trivial. Strategy games tend to be easier to display, but much harder to program the computer so it plays a credible game. Tic-Tac-Toe is at the low end of difficulty -- there is a no-lose strategy -- but still a challenge.

I always think of TTT as a good programming exercise because one can write a reasonably intelligent strategy that will do well -- it is possible, but not easy, to beat it -- without making it impossibly large. The optimal (never-lose) game strategy must consider several hundred possible games; a program that size is easy to make mistakes. Instead we can program what some scholars derisively call "Good Old-Fashion AI" (GOFAI, pronounced "Go-Fye") which is actually smarter and easier to program correctly than the neural nets that get called "AI" today. And you can do it in under a hundred lines of code (the board display is extra). By contrast, a neural net solution can be programmed in "20 lines of C" (approximately the same as Java, but much easier to make mistakes) plus thousands of lines of training data, which is where the real programming takes place, and where it's much easier to make undetectable but catastrophic blunders (like Google's famous racist slur). And it takes hundreds or thousands of hours of computer time, plus more programmer time than the GOFAI solution. Your tax dollars at work, since nobody can afford that kind of cost except the Government and a few mega-corporations like Google.

Programming is not something that people invent for themselves, all of us learned it from seeing what other people did. Even the very first programmers on the first computers more than half a century ago, they only wrote simple programs. Later programmers, "standing," as Newton put it, "on the shoulders of giants," could see farther and write better programs by looking at what had already been done. Most of us (me too!) have a high opinion of our own competence, so if you think you are up to it, jump right in and write your own code in Java. If you get stuck, no harm, no foul, maybe we can help.

Anyway, even my "under a hundred lines" seemed to stump some people, so I've broken the task down into four or five developmental stages:

First we will use the computer to keep score and display the game board while two humans play. We will design first in English (starting here) and then later, after we know our design is good, translate it into Java. If you want to try going directly to Java yourself, Go for it! If you get stuck, come back and see if what I did helps.

Then (optionally) we will do the same thing, but graphically, in the GameEngine.

Then we will discuss the simple GOFAI strategy, telling the computer how to play to win.

Then, if you feel so inclined, you can implement the same strategy in the GameEngine.

Finally, if you are still interested, we can delve into different data representations.

Also, we will try to do most of our design in (understandable) English -- and maybe, if you are so inclined -- try out the partial designs in the Kitchen computer. So rather than fiddling with the Java text editor at the beginning, you should open a text window (NotePad, or whatever it is called in your computer), so you can write (and save!) code snippets and just plain (non-code) English, and look at it (and modify it) little by little, as we move through the design. I have a big screen on my computer so I can put two windows side-by-side, it's very helpful. Modern laptops have a pretty wide screen for watching movies, which also helps us programmers.

The first stage in our 5-step design process is to keep score and display the game.

Give some thought to how you might do that, before you turn the page.

<<Previous | ToC | Next >>

Revised: 2021 October 2