-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
110 lines (95 loc) · 2.79 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
110
import React, { Component } from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Alert
} from 'react-native';
import params from './src/params';
import Field from './src/components/Field';
import MineField from './src/components/MineFiled';
import Header from './src/components/Header'
import LevelSelection from './src/screens/LevelSelection'
import { createMinedBoard, cloneBoard, openField, hadExplosion, wonGame, showMines, invertFlag, flagsUsed } from './src/functions';
export default 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('Perdeu!', 'Que burro!')
}
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)
if (won) {
Alert.alert('Parabéns', 'Você Venceu!')
}
this.setState({ board, won })
}
onLevelSelected = level => {
params.difficultLevel = level
this.setState(this.createState())
}
render() {
return (
<SafeAreaView 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>
</SafeAreaView >
);
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-end',
// alignItems: 'center',
backgroundColor: '#f5fcff'
},
board: {
alignItems: 'center',
backgroundColor: '#AAA'
},
// welcome: {
// fontSize: 20,
// textAlign: 'center',
// margin: 10
// }
});
// export default App;