-
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.
First commit and minesweeper has been created
- Loading branch information
Showing
6 changed files
with
9,276 additions
and
4,313 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Pressable, Text } from 'react-native'; | ||
|
||
const Square = ({ square: { hasMine, minesAround, cleared }, clearSquare }) => { | ||
const title = hasMine ? 'x' : minesAround; | ||
return ( | ||
<Pressable onPress={clearSquare} style={{flexDirection: 'row', backgroundColor: 'gray', border: 'solid 2px black'}}> | ||
<Text style={{fontFamily: 'monospace'}}>{cleared ? title : ' '}</Text> | ||
</Pressable> | ||
); | ||
} | ||
|
||
export default Square; |
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,38 @@ | ||
import { Button, View, Text } from 'react-native'; | ||
import { useEffect, useState } from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
import { resetSquares, clearSquare } from './squares'; | ||
|
||
import Square from './Square'; | ||
|
||
const Minesweeper = ({ squares, resetSquares, clearSquare }) => { | ||
useEffect(() => void resetSquares(), []); | ||
const [gameover, setGameover] = useState(false); | ||
|
||
const pressSquare = ([x, y]) => { | ||
clearSquare([x, y]) | ||
if (squares[x][y].hasMine) setGameover(true); | ||
}; | ||
|
||
const newGame = () => { | ||
resetSquares(); | ||
setGameover(false); | ||
}; | ||
|
||
return ( | ||
<View> | ||
{gameover && <Text>Game Over</Text>} | ||
<Button title='Reset' onPress={() => newGame()} /> | ||
{squares.map((row, r) => ( | ||
<View key={r} style={{flexDirection: 'row'}}> | ||
{row.map((square, c) => ( | ||
<Square square={square} clearSquare={() => !gameover && pressSquare([r, c])} key={c} /> | ||
))} | ||
</View> | ||
))} | ||
</View> | ||
); | ||
}; | ||
|
||
export default connect(({ squares }) => ({ squares }), { resetSquares, clearSquare })(Minesweeper); |
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,61 @@ | ||
import {createSlice} from '@reduxjs/toolkit'; | ||
|
||
const ten = [1,2,3,4,5,6,7,8,9,10]; | ||
const createArrayTo = (length, value={}) => { | ||
const arr = []; | ||
for(let i=0; i!=length; ++i) arr.push(value); | ||
return arr; | ||
}; | ||
|
||
const squares = createSlice({ | ||
name: 'squares', | ||
initialState: createArrayTo(10, createArrayTo(10)), | ||
reducers: { | ||
resetSquares: state => { | ||
// clear | ||
state.forEach(squares => squares.forEach(square => { | ||
square.hasMine = false; | ||
square.minesAround = 0; | ||
square.cleared = false; | ||
})) | ||
|
||
// add new mines | ||
let minesLeft = 10; | ||
while(minesLeft > 0) { | ||
const x = Math.floor(Math.random() * 10) | ||
const y = Math.floor(Math.random() * 10); | ||
|
||
if(!state[x][y].hasMine) { | ||
state[x][y].hasMine = true; | ||
|
||
// Add to minesAround | ||
for(let i = x - 1; i <= x + 1; ++i) | ||
for(let k = y - 1; k <= y + 1; ++k) // TODO: no optional chaining in react native??!?!? | ||
state[i] && state[i][k] && ++state[i][k].minesAround; | ||
|
||
--minesLeft; | ||
} | ||
} | ||
}, | ||
|
||
clearSquare: (state, { payload: [x, y] }) => { | ||
const clearSquaresAroundIfZero = (state, x, y) => { | ||
if (state[x][y].cleared) return; | ||
|
||
state[x][y].cleared = true; | ||
|
||
// if zero, then reveal all connecting zeros | ||
if (state[x][y].minesAround === 0) { | ||
for(let i = x - 1; i <= x + 1; ++i) | ||
for(let k = y - 1; k <= y + 1; ++k) | ||
state[i] && state[i][k] && clearSquaresAroundIfZero(state, i, k) | ||
} | ||
}; | ||
|
||
clearSquaresAroundIfZero(state, x, y); | ||
}, | ||
}, | ||
}); | ||
|
||
export const { resetSquares, clearSquare } = squares.actions; | ||
export default squares.reducer; |
Oops, something went wrong.