Tic Tac Toe  Part 3
If you made it here, then your game lets a player pick a spot on the board:
It also doesn't let them pick a spot that's already taken.
Onto the most exciting part! You will write code that determines the winner.
The checkWin()
function
This function has to check for a win in every row:
every column:
the left diagonal:
and the right diagonal:
Task 6: Write the function
1. Function name: The name of the function is checkWin()
. It will determine the winner.
/** * Function name  checkWin < * @param board (char[][]) * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). */
2. Parameters: The function takes one parameter: char[][] board
.
/** * Function name  checkWin * @param board (char[][]) < * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). */
3. Return value: The function returns a count.
/** * Function name  checkWin * @param board (char[][]) * @return count (int) < * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). */
Inside the function, make a count variable that starts at 0
. This count will determine who won the game.
/** * Function name  checkWin * @param board (char[][]) * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. < * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). */
As you write the function, you'll need to call it from your for
loop in main
. The loop should break if the returned count is 3 or 3.
Task 6  Call the function.
if (returned count == 3) {
1. print: X wins
2. break the loop
} else if (returned count == 3) {
1. print: O wins
2. break the loop
}
In essence, this checks for a winner after every turn.
Task 7: Checking every row
Everything inside the function is its own task. First, the function will check every row for a straight X or straight O.
/** * Function name  checkWin * @param board (char[][]) * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) < * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). */
Your outer loop must run through each row while the inner loop runs through every character in that row.
In each row, add 1 to count
if there's an X. Subtract 1 if there's an O.
If count
isn't 3 or 3, reset it and move to the next row.
A count of 3 means X won (conversely, a count of 3 means O won).
If count
is 3 OR 3, break the checkWin
function by returning count
.
Notice that the game stops after a win.
Task 8: Checking every column
If none of the rows result in a count of 3 or 3, reset the count to 0 (important).
Now, check every column for a count of 3 or 3.
/** * Function name  checkWin * @param board (char[][]) * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) < * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). */
Hints:

The outer loop picks a column. The inner loop will index each row for that column.

When looking at the image below, think of
i
andj
.
A player wins if one of the columns results in a count
of 3 or 3:
Test every column before you move to Task 9.
Task 9: Checking the left diagonal
If none of the rows or columns result in a count of 3 or 3, reset the count to 0.
Now, check the left diagonal for a straight X or O.
/** * Function name  checkWin * @param board (char[][]) * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). < * 5. Check the right diagonal for a straight X/O (Task 10). */
Hints:

Do you see a pattern in the indices?

You don't need a nested loop. Use a single
for
loop.
Output:
Task 10: Checking the right diagonal
If none of the rows, columns, or left diagonal result in a win, reset the count to 0.
Now, check the right diagonal for a straight X or O.
/** * Function name  checkWin * @param board (char[][]) * @return count (int) * * Inside the function: * 1. Make a count variable that starts at 0. * 2. Check every row for a straight X/O (Task 7) * 3. Check every column for a straight X/O (Task 8) * 4. Check the left diagonal for a straight X/O (Task 9). * 5. Check the right diagonal for a straight X/O (Task 10). < */
Hint:

You don't need a nested loop. Use a single
for
loop. 
The pattern here is a bit tricky:

2  0 = 2

2  1 = 1

2  2 = 0

Compare the bolded numbers from the hint to the indices below:
Output:
Task 11: What if it's a tie?
If the game goes all nine turns and nobody wins, print: "it's a tie!".
Task 12: Test your code!
Test your code for every type of win:

straight row

straight column

left diagonal

right diagonal.
Test each case for X and O. Also, see if your code works in the event of a tie.
If everything works, you have my sincere congratulations! Tic tac toe is not an easy project, and going through this challenge is a strong testament to your growth.