Code a Tic Tac Toe Game With Java | Coding Tutorial

juni logo
Code a Tic Tac Toe Game in Java!

Project Intro

Use a 2D array to learn the code for Tic Tac Toe in Java — and practice using conditionals, loops, and functions!

This is a challenging project for Java coders familiar with basic concepts, and is also great practice for AP Computer Science students.

  • Coding language: Java
  • Approx. lines of code: ~150
  • Approx. time needed to build: 30-60 min

Who is this for?

  • Coding experience in language: Beginner
  • Juni level: Java Level 1
  • Challenge Level: Hard

Learning Outcomes

Core concepts practiced:

  • 2D arrays
  • Functions

Prerequisite concepts to know/review:

  • Variables
  • Input and output
  • Conditionals
  • Loops

Just using these core concepts, we can complete the Tic Tac Toe Java code from start to finish.

Moreover, we learn how we can turn a commonly played game into code by learning to think like a programmer and follow an algorithm. Without further ado, let’s get started!

Project Demo

Click run to play the Tic Tac Toe Java two player game yourself below!

You can also view my project solution code if you get stuck.

Specific features to consider:

  1. Display the board. After asking us for our names, the program prints out a 3x3 board filled with dashes, signifying empty spots.
  2. Take turns placing noughts and crosses. Each turn it asks either player 1 or player 2 to enter a row and col index which is where they want to place their x and o, and then the board is printed again with the x or o in the right spot.
  3. Skip invalid plays. If the position the player entered is “off the board” or already has an x or o on it, then our program notifies the player, who is prompted to enter another row and col?
  4. State the result. Once the player wins by getting 3 in a row, column, or diagonal, the program prints that player 1 or 2 has won and prints out the final board.

General order of steps to implement:

  1. Create a Tic Tac Toe board and fill it with dashes.
  2. Create a function that draws the board like a square.
  3. Keep track of the player’s turn and what symbol they are using.
  4. Keep asking the user to enter a row and col until they are valid.
  5. Set the right position on the board to the proper symbol.
  6. Create a function that checks if either player has won.
  7. Check draw: check if the game has ended in a tie.
  8. Use a loop to keep the game going.

How do we do each of these steps?

Step 1: Create a 3x3 array to represent the tic tac toe board and fill it with dashes.

We need to make a 2D array of characters, which can be x, o, or -.

Hint: We can use the following line of code to make a 3x3 array of chars: char[][] board = new char[3][3]

Now we have to fill our board with dashes.

Hint: We can use a nested for loop to iterate through each position on our board. Inside both for loops, we can set board[i][j] equal to a dash.

Step 2: Ask the users for their names.

First, we import the Scanner to help us get user input by adding import java.util.Scanner to the top of our program. Then, we initialize our Scanner variable by making a new ScannerNext, we print out a message asking the user to type in their name using System.out.print(). We store their input in a String called p1. Do the same for p2. Step 3: Create a function that draws the board and prints it out like a 3x3 square. In order for our function to draw the board and print it out, do we need to pass a parameter into the function? Do we need to return anything? Hint: We need to pass in the board 2D array in order for the function to be able to print it. We don’t need to return anything since the function is simply printing out the board. Inside our function, we need to print out each position on our board. Hint: If we do System.out.println(), then each position is on a new line. Hint: If we do System.out.print(), then all of the positions are on one line. Hint: We can do System.out.print() in the inner for loop, and do System.out.println() at the end of the outer for loop so that it starts a new line after each row has been printed.

Related Reading