Created
June 1, 2019 00:55
-
-
Save cezar08/75eb81b56bd16fa36c959e0bd3ac9846 to your computer and use it in GitHub Desktop.
App.js
This file contains hidden or 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
| import React, {Component} from 'react'; | |
| import {Platform, StyleSheet, Text, View, Alert} from 'react-native'; | |
| import params from './src/params'; | |
| import MineField from './src/components/MineField' | |
| 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('Peerdeu!', '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}) | |
| } | |
| render() { | |
| return ( | |
| <View style={styles.container}> | |
| <View style={styles.board}> | |
| <MineField board={this.state.board} onOpenField={this.onOpenField} | |
| onSelectField={this.onSelectField} | |
| ></MineField> | |
| </View> | |
| </View> | |
| ); | |
| } | |
| } | |
| const styles = StyleSheet.create({ | |
| container: { | |
| flex: 1, | |
| justifyContent: 'flex-end' | |
| }, | |
| board: { | |
| alignItems: 'center', | |
| backgroundColor: '#AAA' | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment