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.
add clearCells and logic to minesweeper b00tc4mp#200
- Loading branch information
Showing
4 changed files
with
197 additions
and
16 deletions.
There are no files selected for viewing
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
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
148 changes: 148 additions & 0 deletions
148
staff/joseramon-rodriguez/shapes/minesweeper/src/logic/clearCells.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,148 @@ | ||
function clearCells(board, i, j) { | ||
if (board[i][j - 1]) { | ||
const cellCopy = { ...board[i][j - 1] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i][j - 1] = cellCopy | ||
|
||
clearCells(board, i, j - 1) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i][j - 1] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i][j + 1]) { | ||
const cellCopy = { ...board[i][j + 1] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i][j + 1] = cellCopy | ||
|
||
clearCells(board, i, j + 1) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i][j + 1] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i - 1]?.[j]) { | ||
const cellCopy = { ...board[i - 1][j] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i - 1][j] = cellCopy | ||
|
||
clearCells(board, i - 1, j) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i - 1][j] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i + 1]?.[j]) { | ||
const cellCopy = { ...board[i + 1][j] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i + 1][j] = cellCopy | ||
|
||
clearCells(board, i + 1, j) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i + 1][j] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i - 1]?.[j - 1]) { | ||
const cellCopy = { ...board[i - 1][j - 1] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i - 1][j - 1] = cellCopy | ||
|
||
clearCells(board, i - 1, j - 1) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i - 1][j - 1] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i - 1]?.[j + 1]) { | ||
const cellCopy = { ...board[i - 1][j + 1] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i - 1][j + 1] = cellCopy | ||
|
||
clearCells(board, i - 1, j + 1) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i - 1][j + 1] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i + 1]?.[j - 1]) { | ||
const cellCopy = { ...board[i + 1][j - 1] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i + 1][j - 1] = cellCopy | ||
|
||
|
||
clearCells(board, i + 1, j - 1) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i + 1][j - 1] = cellCopy | ||
} | ||
} | ||
|
||
if (board[i + 1]?.[j + 1]) { | ||
const cellCopy = { ...board[i + 1][j + 1] } | ||
|
||
if (cellCopy.bombsAside === 0 && !cellCopy.isRevealed) { | ||
cellCopy.isRevealed = true | ||
|
||
board[i + 1][j + 1] = cellCopy | ||
|
||
clearCells(board, i + 1, j + 1) | ||
} | ||
|
||
else { | ||
cellCopy.isRevealed = true | ||
|
||
board[i + 1][j + 1] = cellCopy | ||
} | ||
} | ||
} | ||
|
||
export { clearCells } |
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