Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/components/TicTacToe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -230,7 +229,7 @@ export default function TicTacToe({ boardWidth, boardHeight }) {
<button
className="text-center border p-2 rounded bg-gray-100
hover:bg-gray-200"
onClick={() => alert("BUG: Replace this with something else...")}
onClick={() => resetGame()}
>
Reset Game
</button>
Expand Down
52 changes: 52 additions & 0 deletions src/functions/checkForWinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/HelloPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function HelloPage() {
}
<button
className="p-3 mx-8 rounded border-green-500 bg-green-500 hover:bg-green-600 text-white"
onClick={() => setCount(currentCount => currentCount + 2)}
onClick={() => setCount(currentCount => currentCount + 1)}
>
Current Count: {count}
</button>
Expand All @@ -84,7 +84,7 @@ export default function HelloPage() {
}
<button
className="p-2 mx-8 rounded border bg-gray-100 hover:bg-gray-200"
onClick={() => setCount(100)}
onClick={() => setCount(0)}
>
Reset Count
</button>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/TicTacToePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<>
Expand Down