Subscribe to our newsletterWe’ll send you our favorite blog posts, tutorials, events, and more!
In our first Tic Tac Toe Java tutorial, we learned how to make a basic game of tic tac toe in code from start to finish. Now, instead of a basic 3x3 board, let’s make our Tic Tac Toe game work for any size board!
It’s definitely a tough challenge, but you can do it!
Who is this for?
- Coding experience in language: Beginner
- Juni level: Java Level 1
- How would you describe the challenge level? Hard
Core concepts practiced:
- 2D arrays
Prerequisite concepts to know/review:
- Input and output
Click run to see the project yourself below!
You can also view my project solution code if you get stuck.
General order of steps to implement:
- Create a variable that stores the size of our board.
- Change the nested for loops so that they iterate the correct number of times, instead of just 3 times.
- Change the conditions that check if our row and col are out of bounds.
- Rewrite the function that checks for a winner to make it work for any board size – this is a challenging step!
How do we do each of these steps?
- We can create a variable n in our main method that represents the size of our board and make our board nxn.
- We need to use board.length and board.length in the nested for loops in our functions instead of 3.
- When checking if the row and col are out of bounds, we have to check if row and col are greater than or equal to n instead of checking if row and col are greater than 2.
- Finally, the function in which we check if a player has won needs to be rewritten in a way that works for any board size.
For the last step, the general construction is as follows:
- Create a boolean that is true if you have n chars in a row or column or diagonal that you are checking.
- Create a variable called value which stores one of the positions on the row or column or diagonal that you are checking.
- If value is equal to a dash, then you can set the boolean to false since there is an empty space.
- Else, loop through all of the other positions on the row or column or diagonal that you are checking.
- If a position does not equal value, then you can set the boolean to false and break out of the loop.
- Outside the loop, if the boolean still is true, return value.
More creative suggestions:
- Try asking both players what symbol they want to use in the game!
Great job — now check out more tutorials!
Thanks for watching and hope you had fun making this project with me!
Built 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, or want to keep learning?
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.