-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
128 lines (118 loc) · 3.89 KB
/
App.js
File metadata and controls
128 lines (118 loc) · 3.89 KB
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
import React, { Component } from 'react';
import { View, AsyncStorage } from 'react-native';
import {
addPropertiesToArray,
duplicateArray,
shuffleArray
} from "./src/helpers/arrayHelpers";
import Header from "./src/components/Header/Header";
import GameLayout from "./src/components/GameLayout/GameLayout";
export default class App extends Component {
state = {
playing: false,
turnNumber: 1,
pairsFound: 0,
numClicksWithinTurn: 0,
pokemonList: [],
firstPokemonSelected: "",
secondPokemonSelected: "",
}
handleStartGame = async () => {
const allPokemon = await AsyncStorage.getItem('allPokemon');
if (allPokemon) {
const allPokemonList = JSON.parse(await AsyncStorage.getItem('allPokemon'));
const pokemonList = this.prepareCards(this.selectFivePokemon(allPokemonList));
this.setState({ pokemonList, playing: true })
} else {
try {
const pokeapiResponse = await fetch("https://cors.now.sh/https://pokeapi.co/api/v2/pokemon/?limit=802");
const allPokemonData = await pokeapiResponse.json();
const allPokemonListString = await JSON.stringify(allPokemonData.results);
await AsyncStorage.setItem('allPokemon', allPokemonListString);
const allPokemonList = JSON.parse(await AsyncStorage.getItem('allPokemon'));
const pokemonList = this.prepareCards(this.selectFivePokemon(allPokemonList));
this.setState({ pokemonList, playing: true })
} catch (error) {
alert("There was an error fetching the pokemon list");
}
}
}
handleNewGame = () => {
this.setState({
playing: false,
turnNumber: 1,
pairsFound: 0,
numClicksWithinTurn: 0,
pokemonList: [],
firstPokemonSelected: "",
secondPokemonSelected: "",
})
}
selectFivePokemon = (array) => {
const randomNumber = Math.floor(Math.random() * (array.length - 5))
return array.slice(randomNumber, (randomNumber + 5));
}
prepareCards = (array) => {
return shuffleArray(addPropertiesToArray(duplicateArray(array)));
}
handleFlipCard = (pokemonCard) => {
this.setState((prevState) => {
if (prevState.numClicksWithinTurn === 0) {
return {
numClicksWithinTurn: 1,
firstPokemonSelected: pokemonCard.name,
pokemonList: prevState.pokemonList.map(pokemon => {
if (pokemonCard.id === pokemon.id) {
return { ...pokemon, imageUp: true }
} else {
return { ...pokemon, imageUp: false }
}
})
}
} else if (prevState.numClicksWithinTurn === 1) {
if (prevState.firstPokemonSelected === pokemonCard.name) {
return {
numClicksWithinTurn: 0,
turnNumber: prevState.turnNumber + 1,
pairsFound: prevState.pairsFound + 1,
pokemonList: prevState.pokemonList.map(pokemon => {
if (pokemon.name === pokemonCard.name) {
return { ...pokemon, imageUp: true, matched: true }
} else {
return pokemon;
}
})
}
} else {
return {
numClicksWithinTurn: 0,
turnNumber: prevState.turnNumber + 1,
pokemonList: prevState.pokemonList.map(pokemon => {
if (pokemonCard.id === pokemon.id) {
return { ...pokemon, imageUp: true }
} else {
return pokemon;
}
})
}
}
}
})
}
render() {
return (
<View>
<Header />
<GameLayout
playing={this.state.playing}
turnNumber={this.state.turnNumber}
pairsFound={this.state.pairsFound}
onStartGame={this.handleStartGame}
pokemonList={this.state.pokemonList}
onFlipCard={this.handleFlipCard}
onNewGame={this.handleNewGame}
/>
</View>
);
}
}