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
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
Hint: We can use the following line of code to make a 3x3 array of
chars: char[][] board = new char[3][3]
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.
Next, we print out a message asking the user to type in their name
using System.out.print() .
Inside our function, we need to print out each position on our board.
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.
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.
Hint: We can use a while(true) loop and break once the player has
entered a valid row and col.
Now we can set this position to be equal to the char of the player,
which we stored in the variable c .
Let’s start with rows. We can use a for loop to iterate through each
row i .
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.
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.
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.
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.