Skip to content

Henri Comer Tic Tac Toe #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 4 additions & 6 deletions src/components/TicTacToe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down Expand Up @@ -230,7 +228,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
50 changes: 48 additions & 2 deletions src/functions/checkForWinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/pages/HelloPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function HelloPage() {
* Can you find where the PageTitle component declaration is?
*/
}
<PageTitle contents="Hello, JumboCode Developers!" />
<PageTitle contents="Hello, Henri!" />
{
/**
* This is a <div> which allows us to divide a webpage
Expand All @@ -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