Use a 2D array to make a Tic Tac Toe game — 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
Core concepts practiced:
- 2D arrays
Prerequisite concepts to know/review:
- Input and output
Just using these core concepts, we can create a whole game of Tic Tac Toe from start to finish.
Moreover, we learn how we can turn a commonly played game into code by learning to think like a programmer. Without further ado, let’s get started!
Click run to play the Tic Tac Toe Java game yourself below!
You can also view my project solution code if you get stuck.
Specific features to consider:
- After asking us for our names, the program prints out a 3x3 board filled with dashes, signifying empty spots.
- 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.
- 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.
- 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:
- Create a Tic Tac Toe board and fill it with dashes.
- Create a function that draws the board like a square.
- Keep track of the player’s turn and what symbol they are using.
- Keep asking the user to enter a row and col until they are valid.
- Set the right position on the board to the proper symbol.
- Create a function that checks if either player has won.
- Check if the game has ended in a tie.
- 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
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 input from the user, by adding
import java.util.Scanner to the top of our program.
Then, we create our
Next, we print out a message asking the user to type in their name using
We store their input in a String called
Do the same for
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.
Step 4: Print out the correct player’s turn and store the player’s char (x or o).
We need a way to keep track of which player’s turn it is in our game.
Hint: We can use a boolean called player1 which is true if it is player 1’s turn and false if it is player 2’s turn.
We can use a conditional to check whose turn it is.
Also, we can use string concatenation to print out the player’s name.
Step 5: Ask the user for the row and col and check if it is valid.
Print a message asking the user for a row and use the
Scanner to get their input, storing it in a variable called
row; repeat this for
Now, why would the row and col the user entered not be valid?
Hint: If the user types a row and col that is a spot that is not on the board, then the row and col aren’t valid. Use a conditional to check if the row and col are not greater than 2 and not less than 0.
Hint: If the user types a row and col that is a spot that already has an x or o on it, then the row and col aren’t valid. Use a conditional to check if the position on the board at row and col does not already have an x or o.
Step 6: Use a loop to keep asking the player to enter a valid row and col.
If the user enters a row and col that is out of bounds or a row and col that already has an x or o on it, then we want to ask the user to re-enter a row and col. We can use a loop to do this!
Hint: We can use a while(true) loop and break once the player has entered a valid row and col.
Step 7: Set the right position on the board to the player char.
Outside of the while loop, we know that we have a valid row and col.
We can get the position on the board by doing
Now we can set this position to be equal to the char of the player, which we stored in the variable
Step 8: Create a function that checks if either player has won.
In tic tac toe, a player wins if they have 3 of their symbols in one row, column, or diagonal.
Let’s start with rows. We can use a for loop to iterate through each row
Inside the for loop, we can use a conditional to check if
board[i] and if
board[i]. Remember, we also have to check if
board[i] doesn’t equal a dash so that we don’t win if there are three empty spots in a row. If that is all true, then we can return the value of
We can repeat similar steps for columns.
There are two diagonals on the board that we have to check. We can use two if statements to check the two diagonals, similar to the if statements we used for rows and columns.
If we reach the end of our function, that means that nobody has won. Here, we can just return a space.
Step 9: Print out which player has won if a player has won.
In our main method, we can use the function we just created to check if a player has won.
Hint: We can use conditionals to check if our function returns x or o. If it returns x, then print that player 1 has won. If it returns o, then print that player 2 has won.
Step 10: Check if the game has ended in a tie.
Tic tac toe ends in a tie if nobody has won and the board is full. We already have checked if someone has won. Now we just need to check if the board is full.
Let’s create a function that returns true if the board is full and false if there are still empty spots on the board. Remember, an empty spot is a dash.
Hint: We can use nested for loops to iterate through each position on the board. Inside the inner for loop, we can use a conditional to check if board[i][j] is equal to -, and if so, return true.
Once we finish going through the nested for loops and find that no position on the board equals a dash, then we know that the board is full so we can return true.
Step 11: Use a loop to keep the game going.
We can create a boolean called
gameEnded and initially set it to false. Inside the if statement where we check if a player has won or if it is a tie, we can set
gameEnded to true.
We can make a while loop, with its condition simply being
gameEnded, so that the program keeps asking a player to enter a row and col until there is a winner or a tie.
If we run our program, we notice that the player doesn’t alternate every round. How can we fix this?
Hint: If there is no winner and no tie, we can switch the player1 boolean by writing the following:
player1 = !player1. The
!means not, so if player1 was true, this line sets it to not true, or false, and if player1 was false, this line sets it to not false, or true.
After the while loop is over, we can draw the board a final time so that both players can see the final state of the board.
Great job! Ready for a challenge?
Thanks for watching and hope you had fun making Tic Tac Toe with me! I've also made a tutorial for a Next Level Tic Tac Toe Game — where we build a version with even more rows and columns on our board. Coming soon!
Tried the project above? We'd love to see it! If you're interested in sharing your coding project or experiences with diversity in STEM, please reach out to us at firstname.lastname@example.org.
Every week, we’ll be posting project tutorials like this one, for different coding languages and experience levels, as well as math tutorials.
Visit our coding projects blog page to find our other tutorials in more coding languages! You can also subscribe to our newsletter using the form to the right of this page for more updates on our programs and newest coding tutorials.
Need more help?
Looking up your coding questions is one of the best ways to learn!
Another great way to learn is from an experienced coder or instructor. Irith is a Juni student who started as a beginner in our AP Computer Science track with a Juni Instructor. After learning more from our USACO and Web courses, he now loves to code his own projects and build iPhone apps!
Juni Learning Computer Science Instructors work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.