-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
109 lines (94 loc) · 2.6 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React, { Component } from 'react'
import { StyleSheet, View, Text, Alert } from 'react-native'
import params from './src/params';
import MineField from './src/components/MineField'
import Header from './src/components/Header'
import LevelSelection from './src/screens/LevelSelection'
import {
createMinedBoard,
cloneBoard,
openField,
hadExplosion,
wonGame,
showMines,
invertFlag,
flagsUsed
} from './src/functions'
class App extends Component {
constructor(props) {
super(props)
this.state = this.createState()
}
minesAmount = () => {
const cols = params.getColumnsAmount()
const rows = params.getRowsAmount()
return Math.ceil(cols * rows * params.difficultLevel)
}
createState = () => {
const cols = params.getColumnsAmount()
const rows = params.getRowsAmount()
return {
board: createMinedBoard(rows , cols, this.minesAmount()),
won: false,
lost: false,
showLevelSelection: false,
}
}
onOpenField = (row, column) => {
const board = cloneBoard(this.state.board)
openField(board, row, column)
const lost = hadExplosion(board)
const won = wonGame(board)
if (lost) {
showMines(board)
Alert.alert('Perdeeeeeu!', 'Que buuuuurroo!')
}
if (won) {
Alert.alert('Parabéns', 'Você Venceu!')
}
this.setState({ board, lost, won })
}
onSelectField = (row, column) => {
const board = cloneBoard(this.state.board)
invertFlag(board, row, column)
const won = wonGame(board)
this.setState({ board, won })
}
onLevelSelected = (level) => {
params.difficultLevel = level
this.setState(this.createState())
}
render() {
return (
<View style={styles.container}>
<LevelSelection isVisible={this.state.showLevelSelection}
onLevelSelected={this.onLevelSelected}
onCancel={() => this.setState({ showLevelSelection: false })} />
<Header flagsLeft={this.minesAmount() - flagsUsed(this.state.board)}
onNewGame={() => this.setState(this.createState())}
onFlagPress={() => this.setState({ showLevelSelection: true })} />
<View style={styles.board}>
<MineField board={this.state.board}
onOpenField={this.onOpenField}
onSelectField={this.onSelectField} />
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-end',
},
welcome: {
fontSize: 20,
textAlign: 'center',
color: '#000',
},
board: {
alignItems: 'center',
backgroundColor: '#AAA',
}
})
export default App;