Start With Something Simple – Tic Tac Toe

The first game I ever tried to make was a 3D first person shooter. I gave up after the second day. I didn’t have the math or programming background to do it and I didn’t realize what I was getting myself into when I tried starting.

Tic Tac Toe Game

Your first game should be unbelievably simple, something to show you that you CAN do it. So with that in mind I am going to try and walk you through the simplest Tic-Tac-Toe (TTT for short) game.

TTT is simple because it is easy to visualize the game board without having to make an actual applet. You can simply print to console the current state of the board. Furthermore, it only accepts user inputs and tests for victory.  It is also a good example because if you feel up to it you can turn it into player vs. computer with a simple AI (decision tree) instead of just player vs. player. For now though, we will leave it as player vs. player.

I’m going to assume you are using Java, it is the language that pretty much everyone starts on (and for many good reasons). First you need to figure out a plan. Figure out how you are going to store the board, for beginners I recommend a simple double array.

int (or String)[][] board;                //don’t forget to define the size in your constructor [3][3]

This will allow you to simply add to board the user input. Now you need to setup a way to track whose turn it is. An easy trick is to create a turn integer that gets added too each time a player takes a turn. with the integer you can use the modulus function to find it’s remainder and create an if statement based on if turn%2 =1 or 0. This turn integer will also be useful later for multiple reasons. In TTT there can only ever be a max of 9 turns, so the turn integer can be use to tell the game when to stop playing. Also, no player can win until the 5th turn or later, so you can use it to tell your testForVictory() method when to start running.

Next, how are you going to get the player input? Use a simple JOptionPane.showInputDialog(“text prompt”). With that input you can decide where to put the marker (you can limit it to accept only integer inputs). For our board, if a user inputs position 1 (let’s assume they aren’t computer savvy and don’t know things start at 0). 1 will be position [0][0] in our array. So find, based on turn, whose marker goes there and place it. For reference, an input of 5 will be [1][1], 7 will be [2][0] , 3 will be [0][2] and 9 will be [2][2]. That should give you an idea of how to place the marker.

Now you should basically have a functioning game, except no one can win. What’s up with that? Testing for victory is probably the most complicated part of the whole game but it’s super easy! In TTT the only way to win is to have 3 in a row of either vertical, horizontal, or diagonal directions of the same marker.  So now we just need to test for those conditions.

TTT Video Game

There are better ways to approach testing for victory but this breaks down what is happening better and makes it easier to understand I think. So we are going to test on based on direction.

First, create two for loops, one that loops through the first part of our [][] array first, and one that loops through the second part of our [][] array first. Doing this means you have access in horizontal and vertical order of the board. Inside of the first for loop (there should be one nested for loop) you need to find what that row or column starts with. So something like this.


String test = ourArray[i][0];  (or [0][i] for the second one)


Now inside that second for loop, test to see if the strings stored in the other positions are the same as the one we already found. If they are the same (you can use a counter to keep track of how many are the same) then you have a victory and can end the game. Now you will notice that our second loop starts at 1 and not 0, this is just to save a bit of time, we already know what is stored in the first position, no need to check to see if it is equal to the information we just stored.

For the diagonal aspects of the board, it’s going to be easiest to just set up an if statement structure rather than looping through them. So something like this:

if (ourArray[0][0].equals(ourArray[1][1])  && ourArray[1][1].equals(ourArray[2][2]){


Alternatively, we could use .compareTo, this is probably what I would do but .equals is easier to               read

That is pretty much it, you have your board, you have your input, a way to track the progress of the game, and a way to test for victory. Now it’s just about putting the pieces together. (cheaters can look below  for help setting up the code but boo on you)

public class TTTboard {

static boolean isWon = false;


public static void main(String args[]){

String whoseTurn; //

int turn=0, userInput;

String [][] board = new String[3][3];


while (!isWon){

String temp = JOptionPane.showInputDialog(“Please input a whole positive number where you’d like to ” +

“place your marker, 1 = top left, 10 = bottom right”);

userInput = Integer.parseInt(temp);

if (turn % 2 == 0)whoseTurn = “x”;              //determine whose turn it is for placement on board

else whoseTurn = “o”;

theMove(board, userInput, whoseTurn);                  //make the actual move, useinput used to find location on board, whoseturn for placement

if (turn >=5)testForVictory(board);                    //test for victory


if (isWon == true)   { //if we have a victory (print who won and break the loop, the break really isn’t needed)

System.out.println(“We have a victory!!! he/she is ” + whoseTurn);





printBoard(board);//print the board to see where we are


if (turn == 9)break; //if turn == 9 we are out of turns




//condition for handling a draw

if (isWon == false && turn ==9){

System.out.println(“we have a draw”);




And there you go!

Tags: , ,
Previous Post
Gaming Industry Outlook 2013
Industry News

The Gaming Industry Outlook for 2013

Next Post
Becoming a Game Programmer
Game Programming

An Introduction To Game Programming