forked from b00tc4mp/eurofirms-bootcamp-202402
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minesweeper is now split into components b00tc4mp#200
- Loading branch information
Showing
4 changed files
with
143 additions
and
113 deletions.
There are no files selected for viewing
116 changes: 3 additions & 113 deletions
116
staff/joseramon-rodriguez/shapes/minesweeper/src/App.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
staff/joseramon-rodriguez/shapes/minesweeper/src/components/Board.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useState } from "react"; | ||
import { generateBoard } from "../logic/generateBoard"; | ||
import Cell from "./Cell" | ||
|
||
const columns = 8 | ||
const rows = 8 | ||
const bombs = 10 | ||
|
||
const initialBoard = generateBoard(columns, rows, bombs) | ||
|
||
function Board() { | ||
const [board, setBoard] = useState(initialBoard) | ||
const [gameState, setGameState] = useState(null) | ||
|
||
const handleCellClick = (i, j) => { | ||
if (gameState === 'loose') return | ||
|
||
const boardCopy = [...board].map(row => [...row]) | ||
|
||
const cellCopy = { ...boardCopy[i][j] } | ||
|
||
cellCopy.isClicked = true | ||
|
||
boardCopy[i][j] = cellCopy | ||
|
||
setBoard(boardCopy) | ||
|
||
if (cellCopy.isBomb) { | ||
setTimeout(() => alert('explotaste'), 20) | ||
|
||
setGameState('loose') | ||
} | ||
} | ||
|
||
const handleResetClick = () => { | ||
const newBoard = generateBoard(columns, rows) | ||
|
||
setBoard(newBoard) | ||
setGameState(null) | ||
} | ||
|
||
return ( | ||
<div> | ||
<section | ||
className="board" | ||
style={{ | ||
gridTemplateColumns: `repeat(${columns},1fr)`, | ||
gridTemplateRows: `repeat(${rows},1fr)` | ||
}} | ||
> | ||
{board.map((row, i) => row.map((cell, j) => ( | ||
<Cell | ||
onCellClick={handleCellClick} | ||
coords={{ i, j }} | ||
key={`${i}-${j}`} | ||
cell={cell} | ||
/>) | ||
)).flat()} | ||
</section> | ||
{gameState === 'loose' && <button onClick={handleResetClick}>Reset</button>} | ||
</div> | ||
) | ||
} | ||
|
||
export default Board |
20 changes: 20 additions & 0 deletions
20
staff/joseramon-rodriguez/shapes/minesweeper/src/components/Cell.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function Cell(props) { | ||
const { cell, coords, onCellClick } = props | ||
|
||
if (cell.isClicked) { | ||
return ( | ||
<div className="clicked-cell"> | ||
{cell.isBomb ? 'Boom' : cell.bombsAside} | ||
</div> | ||
) | ||
} | ||
else { | ||
return ( | ||
<div className="no-clicked-cell"> | ||
<button onClick={() => onCellClick(coords.i, coords.j)} className="cell-button" /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default Cell |
55 changes: 55 additions & 0 deletions
55
staff/joseramon-rodriguez/shapes/minesweeper/src/logic/generateBoard.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
function generateBoard(columns, rows, bombs) { | ||
const board = new Array(rows) | ||
|
||
const cellsQuantity = columns * rows | ||
|
||
const indexes = [] | ||
|
||
for (let i = 0; i < bombs; i++) { | ||
const index = Math.floor(Math.random() * cellsQuantity - 1) | ||
|
||
const indexI = Math.floor(index / 8) | ||
const indexJ = index % 8 | ||
|
||
if (indexes.some((index => index.i === indexI && index.j === indexJ))) | ||
i-- | ||
|
||
else indexes.push({ i: indexI, j: indexJ }) | ||
} | ||
|
||
for (let i = 0; i < board.length; i++) { | ||
board[i] = new Array(columns) | ||
|
||
for (let j = 0; j < board[i].length; j++) { | ||
const isBomb = indexes.some(index => index.j === j && index.i === i) | ||
|
||
const cell = { | ||
isBomb, | ||
isClicked: false | ||
} | ||
|
||
board[i][j] = cell | ||
} | ||
} | ||
|
||
for (let i = 0; i < board.length; i++) { | ||
for (let j = 0; j < board[i].length; j++) { | ||
let bombsAside = 0 | ||
|
||
if (board[i]?.[j - 1]?.isBomb) bombsAside++ | ||
if (board[i]?.[j + 1]?.isBomb) bombsAside++ | ||
if (board[i - 1]?.[j]?.isBomb) bombsAside++ | ||
if (board[i + 1]?.[j]?.isBomb) bombsAside++ | ||
if (board[i - 1]?.[j - 1]?.isBomb) bombsAside++ | ||
if (board[i - 1]?.[j + 1]?.isBomb) bombsAside++ | ||
if (board[i + 1]?.[j - 1]?.isBomb) bombsAside++ | ||
if (board[i + 1]?.[j + 1]?.isBomb) bombsAside++ | ||
|
||
board[i][j].bombsAside = bombsAside | ||
} | ||
} | ||
|
||
return board | ||
} | ||
|
||
export { generateBoard } |