Skip to content

Commit

Permalink
Basic game functionality completed
Browse files Browse the repository at this point in the history
  • Loading branch information
7ingyu committed Dec 2, 2023
1 parent 2113d3a commit 55cfc20
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function App() {
]

const [ score, setScore ] = useState({
player1: {
"1": {
wins: 0,
losses: 0
},
player2: {
"2": {
wins: 0,
losses: 0
}
Expand All @@ -44,6 +44,8 @@ function App() {
board={board}
setBoard={setBoard}
frozen={frozen}
setFrozen={setFrozen}
score={score}
setScore={setScore}
/>
</main>
Expand Down
18 changes: 15 additions & 3 deletions src/components/Board.jsx
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) => {
Expand Down
10 changes: 5 additions & 5 deletions src/components/Score.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function Score ({ score: { player1, player2 }}) {
export default function Score ({ score }) {

return (
<table className="scoreboard">
Expand All @@ -16,10 +16,10 @@ export default function Score ({ score: { player1, player2 }}) {
<td>Losses</td>
</tr>
<tr>
<td>{player1.wins}</td>
<td>{player1.losses}</td>
<td>{player2.wins}</td>
<td>{player2.losses}</td>
<td>{score[1].wins}</td>
<td>{score[1].losses}</td>
<td>{score[2].wins}</td>
<td>{score[2].losses}</td>
</tr>
</tbody>
</table>
Expand Down
67 changes: 67 additions & 0 deletions src/utils/detectWin.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 55cfc20

Please sign in to comment.