From 4d0dd1c8c4530c797149c3c07cfbb1429931d608 Mon Sep 17 00:00:00 2001 From: Lindsay Ulrey Date: Thu, 10 Oct 2024 21:25:10 -0400 Subject: [PATCH] Implemented TODOs --- src/components/TicTacToe.jsx | 9 +++--- src/functions/checkForWinner.js | 52 +++++++++++++++++++++++++++++++++ src/pages/HelloPage.jsx | 4 +-- src/pages/TicTacToePage.jsx | 4 +-- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/components/TicTacToe.jsx b/src/components/TicTacToe.jsx index 44afe50..528a06e 100644 --- a/src/components/TicTacToe.jsx +++ b/src/components/TicTacToe.jsx @@ -63,14 +63,13 @@ export default function TicTacToe({ boardWidth, boardHeight }) { function resetGame() { // Clear the Tic-Tac-Toe board // Hint: Use the generateEmptyBoard function! - + setBoard(generateEmptyBoard(boardWidth, boardHeight)) // Set currentTurn to "X" - + setCurrentTurn("X") // Set winner to null - - + setWinner(null) } /** @@ -230,7 +229,7 @@ export default function TicTacToe({ boardWidth, boardHeight }) { diff --git a/src/functions/checkForWinner.js b/src/functions/checkForWinner.js index 2b713c3..fb26afc 100644 --- a/src/functions/checkForWinner.js +++ b/src/functions/checkForWinner.js @@ -55,6 +55,58 @@ export default function checkForWinner(board) { // Add additional winner checking logic here... // Under what conditions can someone win? + // Checking if a player has any vertical wins + for (let col = 0; col < board[0].length; col++) { + let firstCell = board[0][col]; + + // Skip cols with empty first spaces + if (firstCell == null) { + continue; + } + + let isWinningCol = true; + for (let row = 1; row < board.length; row++) { + if (board[row][col] !== firstCell) { + isWinningCol = false; + break; + } + } + + if (isWinningCol) { + return firstCell; + } + } + + // Board must be square for diagonal win + if (board.length == board[0].length) { + // Checking if a player has top left to bottom right diagonal win + let firstLeft = board[0][0]; + let isWinningDiagL = true; + + for (let i = 0; i < board.length; i++) { + if (board[i][i] !== firstLeft) { + isWinningDiagL = false; + break; + } + } + if (isWinningDiagL) { + return firstLeft; + } + + // Checking if a player has top right to bottom left diagonal win + let firstRight = board[0][board[0].length-1]; + let isWinningDiagR = true; + + for (let i = 0; i < board.length; i++) { + if (board[i][board.length-1 - i] !== firstRight) { + isWinningDiagR = false; + break; + } + } + if (isWinningDiagR) { + return firstRight; + } + } // Return null if no winners return null; diff --git a/src/pages/HelloPage.jsx b/src/pages/HelloPage.jsx index f9976da..7ca3b2f 100644 --- a/src/pages/HelloPage.jsx +++ b/src/pages/HelloPage.jsx @@ -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( <>