From 55cfc2004eae83e41a35befb9b63e9d4795272eb Mon Sep 17 00:00:00 2001 From: Ting-Yu Chen <7ingyu@gmail.com> Date: Sat, 2 Dec 2023 11:24:44 -0800 Subject: [PATCH] Basic game functionality completed --- src/App.jsx | 6 ++-- src/components/Board.jsx | 18 +++++++++-- src/components/Score.jsx | 10 +++--- src/utils/detectWin.js | 67 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 src/utils/detectWin.js diff --git a/src/App.jsx b/src/App.jsx index e7307ed..23e3acb 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -16,11 +16,11 @@ function App() { ] const [ score, setScore ] = useState({ - player1: { + "1": { wins: 0, losses: 0 }, - player2: { + "2": { wins: 0, losses: 0 } @@ -44,6 +44,8 @@ function App() { board={board} setBoard={setBoard} frozen={frozen} + setFrozen={setFrozen} + score={score} setScore={setScore} /> diff --git a/src/components/Board.jsx b/src/components/Board.jsx index e085345..186f0ac 100644 --- a/src/components/Board.jsx +++ b/src/components/Board.jsx @@ -1,8 +1,11 @@ import { useState, useEffect } from 'react'; +import getWinner from '../utils/detectWin'; import Cell from './Cell' -export default function Board ({ board, setBoard, frozen }) { +export default function Board ( + { board, setBoard, frozen, setFrozen, score, setScore } +) { const [ lastStart, setLastStart ] = useState(1) const [ turn, setTurn ] = useState(1) @@ -22,8 +25,17 @@ export default function Board ({ board, setBoard, frozen }) { useEffect(() => { // checkBoard for win - - + const winner = getWinner(board) + if (winner) { + const loser = winner === 1 ? 2 : 1 + let newScore = {...score} + score[winner].wins += 1 + score[loser].losses += 1 + setMsg(`Player ${winner} wins!`) + setScore({...newScore}) + setFrozen(true) + } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [board]) const handleClick = (col) => { diff --git a/src/components/Score.jsx b/src/components/Score.jsx index de4ab64..6e35093 100644 --- a/src/components/Score.jsx +++ b/src/components/Score.jsx @@ -1,4 +1,4 @@ -export default function Score ({ score: { player1, player2 }}) { +export default function Score ({ score }) { return ( @@ -16,10 +16,10 @@ export default function Score ({ score: { player1, player2 }}) { - - - - + + + +
Losses
{player1.wins}{player1.losses}{player2.wins}{player2.losses}{score[1].wins}{score[1].losses}{score[2].wins}{score[2].losses}
diff --git a/src/utils/detectWin.js b/src/utils/detectWin.js new file mode 100644 index 0000000..80620cc --- /dev/null +++ b/src/utils/detectWin.js @@ -0,0 +1,67 @@ +export default function detectWin (board) { + // horizontals + console.log('checking horizontals') + for (let r = 0; r < board.length; r++) { + let count = 0 + let player = 0 + + for (let c = 0; c < board[0].length; c++) { + let cell = board[r][c] + if (!!cell && cell !== player) { + player = cell + count = 1 + } else if (!!cell && cell === player) { + count ++ + } + if (count >= 4 && !!player) { + return player + } + } + } + + // verticals + console.log('checking verticals') + for (let c = 0; c < board[0].length; c++) { + let count = 0 + let player = 0 + for (let r = 0; r < board.length; r++) { + let cell = board[r][c] + if (!!cell && cell !== player) { + player = cell + count = 1 + } else if (!!cell && cell === player) { + count ++ + } + if (count >= 4 && !!player) { + return player + } + } + } + + // diagonals + console.log('checking diagonals') + let startRow = 0 + while (startRow < 4) { + let count = 0 + let player = 0 + let r = startRow + let c = 0 + while (r < board.length && c < board[0].length) { + let cell = board[r][c] + if (!!cell && cell !== player) { + player = cell + count = 1 + } else if (!!cell && cell === player) { + count ++ + } + if (count >= 4 && !!player) { + return player + } + r++ + c++ + } + startRow++ + } + + return 0 +} \ No newline at end of file