-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
176 lines (153 loc) · 4.61 KB
/
App.tsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import React, { useState, useEffect } from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
useColorScheme,
View,
Alert
} from 'react-native';
import {
Colors
} from 'react-native/Libraries/NewAppScreen';
import Params from './src/params';
import {
createMinedBoard,
cloneBoard,
openField,
hadExplosion,
wonGame,
showMines,
invertFlag,
flagsUsed
} from './src/functions'
// Components
import MineField from './src/components/MineField';
import Header from './src/components/Header';
import LevelSelection from './src/screens/LevelSelection';
function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
// Estado do tabuleiro
const [board, setBoard] = useState<any[]>([]);
const [won, setWon] = useState<boolean>(false);
const [lost, setLost] = useState<boolean>(false);
const [showLevelSelection, setLevelSelection] = useState<boolean>(false);
// Função para os estados do jogo.
function setState(states: {
board?: any[],
won?: boolean,
lost?: boolean,
showLevelSelection?: boolean
}) {
let {
board,
won,
lost,
showLevelSelection
} = states;
if(board !== undefined){
setBoard(board);
}
if(won !== undefined){
setWon(won);
}
if(lost !== undefined){
setLost(lost);
}
if(showLevelSelection !== undefined){
setLevelSelection(showLevelSelection);
}
}
// Função para calcular a quantidade de minas
function minesAmount() {
const rows = Params.getRowsAmount();
const cols = Params.getColumnsAmount();
return Math.ceil((rows * cols) * Params.difficultLevel);
}
// Função assíncrona para criar o estado inicial do tabuleiro
async function createState() {
const rows = Params.getRowsAmount();
const cols = Params.getColumnsAmount();
const newBoard = await createMinedBoard(rows, cols, minesAmount());
setBoard(newBoard);
setState({
won:false,
lost:false,
showLevelSelection:false
})
}
// Funcao para abrir os campos do tabuleiro.
function onOpenField(row: number, col: number) {
const clonedBoard = cloneBoard(board);
openField(clonedBoard, row, col);
const lost = hadExplosion(clonedBoard);
const won = wonGame(clonedBoard);
if (lost) {
showMines(clonedBoard);
Alert.alert('Que pena ! Você Perdeu.', ':(');
}
if (won) {
Alert.alert('Parabéns ! Você Venceu.', ':)');
}
setState({
board: clonedBoard,
lost: lost,
won: won
});
}
// Funcao para flegar os campos.
function onSelectField(row: number, col: number) {
const clonedBoard = cloneBoard(board);
invertFlag(clonedBoard, row, col);
const won = wonGame(clonedBoard);
if(won){
Alert.alert('Parabéns ! Você Venceu.', ':)');
}
setState({
board: clonedBoard,
won: won
});
}
// Função para mudar o nivel selecionado do jogo.
function onLevelSelected(level:number) {
Params.difficultLevel = level;
createState();
}
// Efeito para chamar createState ao montar o componente
useEffect(() => {
createState();
}, []);
return (
<SafeAreaView style={[backgroundStyle, styles.container]}>
<View style={styles.container}>
<LevelSelection
isVisible={showLevelSelection}
onLevelSelected={onLevelSelected}
onNewGame={createState}
onCancel={() => setState({showLevelSelection:false}) }>
</LevelSelection>
<Header flagsLeft={minesAmount() - flagsUsed(board)} openMenu={() => setState({showLevelSelection:true})}>
</Header>
</View>
<View style={styles.board}>
<MineField board={board}
onOpenField={onOpenField}
onSelectField={onSelectField}></MineField>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-end',
},
board: {
alignItems: 'center',
backgroundColor: '#AAA'
}
});
export default App;