From 530753a4f26f52a58536b99ceea983fe40f98c88 Mon Sep 17 00:00:00 2001 From: Henri Comer Date: Thu, 10 Oct 2024 20:37:36 -0400 Subject: [PATCH] Fixed all bugs --- src/components/TicTacToe.jsx | 10 +++---- src/functions/checkForWinner.js | 50 +++++++++++++++++++++++++++++++-- src/pages/HelloPage.jsx | 6 ++-- src/pages/TicTacToePage.jsx | 4 +-- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/components/TicTacToe.jsx b/src/components/TicTacToe.jsx index 44afe50..7ede753 100644 --- a/src/components/TicTacToe.jsx +++ b/src/components/TicTacToe.jsx @@ -61,15 +61,13 @@ export default function TicTacToe({ boardWidth, boardHeight }) { * sets currentTurn to "X" and sets winner to null */ function resetGame() { - // Clear the Tic-Tac-Toe board - // Hint: Use the generateEmptyBoard function! + setBoard(initialBoard); - // Set currentTurn to "X" + setCurrentTurn("X"); - // Set winner to null - + setWinner(null); } @@ -230,7 +228,7 @@ export default function TicTacToe({ boardWidth, boardHeight }) { diff --git a/src/functions/checkForWinner.js b/src/functions/checkForWinner.js index 2b713c3..b653e5f 100644 --- a/src/functions/checkForWinner.js +++ b/src/functions/checkForWinner.js @@ -52,9 +52,55 @@ export default function checkForWinner(board) { } } - // Add additional winner checking logic here... - // Under what conditions can someone win? + for (let column = 0; column < board.length; column++) { + let firstCell = board[0][column]; + if (firstCell === null) { + continue; + } + + let isWinningColumn = true; + for (let row = 1; row < board.length; row++) { + if (board[row][column] !== firstCell) { + isWinningColumn = false; + break; + } + } + + if (isWinningColumn) { + return firstCell; + } + } + + let firstDiagonalCell = board[0][0]; + if (firstDiagonalCell !== null) { + let isWinningDiagonal = true; + for (let i = 1; i < board.length; i++) { + if (board[i][i] !== firstDiagonalCell) { + isWinningDiagonal = false; + break; + } + } + + if (isWinningDiagonal) { + return firstDiagonalCell; + } + } + + let secondDiagonalCell = board[0][board.length - 1]; + if (secondDiagonalCell !== null) { + let isWinningDiagonal = true; + for (let i = 1; i < board.length; i++) { + if (board[i][board.length - 1 - i] !== secondDiagonalCell) { + isWinningDiagonal = false; + break; + } + } + + if (isWinningDiagonal) { + return secondDiagonalCell; + } + } // Return null if no winners return null; diff --git a/src/pages/HelloPage.jsx b/src/pages/HelloPage.jsx index f9976da..eadef83 100644 --- a/src/pages/HelloPage.jsx +++ b/src/pages/HelloPage.jsx @@ -45,7 +45,7 @@ export default function HelloPage() { * Can you find where the PageTitle component declaration is? */ } - + { /** * This is a
which allows us to divide a webpage @@ -70,7 +70,7 @@ export default function HelloPage() { } @@ -84,7 +84,7 @@ export default function HelloPage() { } diff --git a/src/pages/TicTacToePage.jsx b/src/pages/TicTacToePage.jsx index ed815fb..3affcda 100644 --- a/src/pages/TicTacToePage.jsx +++ b/src/pages/TicTacToePage.jsx @@ -14,8 +14,8 @@ export default function TicTacToePage() { * NOTE: In order to get an updated board, * modify these values and refresh your page. */ - const boardHeight = 4; - const boardWidth = 4; + const boardHeight = 3; + const boardWidth = 3; return( <>