Java How to Make a Check an Input and if False Let the User Enter It Again
Project Intro
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
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 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!
Project Demo
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[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 input from the user, by adding import java.util.Scanner
to the top of our program.
Then, we create our Scanner
variable.
Next, 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 doSystem.out.print()
in the inner for loop, and doSystem.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 col
.
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 board[row][col]
.
Now we can set this position to be equal to the char of the player, which we stored in the variable c
.
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 i
.
Inside the for loop, we can use a conditional to check if board[i][0]
equals board[i][1]
and if board[i][1]
equals board[i][2]
. Remember, we also have to check if board[i][0]
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 board[i][0]
.
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?
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
Learning outcomes
Core concepts practiced:
- 2D arrays
- Functions
Prerequisite concepts to know/review:
- Variables
- Input and output
- Conditionals
- Loops
Demo
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[0].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 hello@learnwithjuni.com.
Visit our coding projects blog page to find our other tutorials in more coding languages!
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.
Read more about our online coding courses and curriculum, or speak with a Juni Advisor today by calling (650) 263-4306 or emailing advisors@learnwithjuni.com.
Source: https://junilearning.com/blog/coding-projects/java-beginner-tic-tac-toe-tutorial/
0 Response to "Java How to Make a Check an Input and if False Let the User Enter It Again"
Postar um comentário