-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
99 lines (76 loc) · 2.47 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
import { StyleSheet, Text, View, Alert, Image} from 'react-native';
import MineField from './src/components/MineField';
import params from './src/params';
import { createMinedBoard, cloneBoard, hadExplosion, openField, showMines, wonGame , invertFlag, flagsUsed} from './src/functions';
import { Component } from 'react';
import Header from './src/components/Header';
import LevelSelection from './src/screens/LevelSelection';
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('Perdeeuuuu','Que pena');
}
if(won){
Alert.alert('Ganhouuuu','Parabéns');
}
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('Ganhouuuu','Parabéns');
}
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',
},
board: {
alignItems: 'center',
backgroundColor: '#AAA'
}
});