-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
166 lines (162 loc) · 4.25 KB
/
Game.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
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
import * as React from 'react';
import PropTypes from 'prop-types';
import RandomNumber from './RandomNumber';
import { View, Text, TouchableOpacity, StyleSheet,Button } from 'react-native';
import shuffle from 'lodash.shuffle';
class Game extends React.Component {
//trzeba zdefiniować typ od rodzica
static propTypes = {
//required = wymagany do uruchomienia
randomNumberCount: PropTypes.number.isRequired,
initialSeconds: PropTypes.number.isRequired,
onPlayAgain:PropTypes.func.isRequired,
};
//status zaznaczinych elementow
state = {
selectedIds: [],
remainingSeconds: this.props.initialSeconds,
};
//definicja statusu gry
gameStatus = 'PLAYING';
//wylosuj pare liczb
randomNumbers = Array.from({ length: this.props.randomNumberCount }).map(
() => 1 + Math.floor(10 * Math.random())
);
//zsumuj czesc z nich
target = this.randomNumbers
.slice(0, this.props.randomNumberCount - 2)
.reduce((acc, curr) => acc + curr, 0);
//losowanie miejsc dla odpowiedzi
randomNumbers = shuffle(this.randomNumbers);
//kiedy komponent sie zaladowal
componentDidMount() {
this.intervalId = setInterval(() => {
this.setState(
prevState => {
return { remainingSeconds: prevState.remainingSeconds - 1 };
},
() => {
if (this.state.remainingSeconds === 0) {
clearInterval(this.intervalId);
}
}
);
}, 1000);
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
//wskaz zaznaczone
isNumberSelected = numberIndex => {
return this.state.selectedIds.indexOf(numberIndex) >= 0;
};
//zaznacz obiekt, kopiuje caly array i wpisuje nowy
selectNumber = numberIndex => {
this.setState(prevState => ({
selectedIds: [...prevState.selectedIds, numberIndex],
}));
};
componentWillUpdate(nextProps, nextState) {
//kiedy przeliczamy status gry?
if (
nextState.selectedIds !== this.state.selectedIds ||
nextState.remainingSeconds === 0
) {
this.gameStatus = this.calcGameStaus(nextState);
if (this.gameStatus !== 'PLAYING') {
clearInterval(this.intervalId);
}
}
}
//status gry:WIN/LOSE
calcGameStaus = nextState => {
const sumSelected = nextState.selectedIds.reduce((acc, curr) => {
return acc + this.randomNumbers[curr];
}, 0);
if (nextState.remainingSeconds === 0) {
return 'LOST';
}
if (sumSelected < this.target) {
return 'PLAYING';
}
if (sumSelected === this.target) {
return 'WON';
}
if (sumSelected > this.target) {
return 'LOST';
}
};
//konstruktor
render() {
const gameStatus = this.gameStatus;
return (
<View style={styles.container}>
<Text style={[styles.target, styles[`STATUS_${gameStatus}`]]}>
{this.target}
</Text>
<View style={styles.randomContainer}>
{this.randomNumbers.map(
(randomNumber, index) => (
<RandomNumber
key={index}
id={index}
number={randomNumber}
isDisabled={
this.isNumberSelected(index) || gameStatus !== 'PLAYING'
}
onPress={this.selectNumber}
/>
)
)}
</View>
<Button title="Play again" onPress={this.props.onPlayAgain}/>
<Text
style={[
styles.STATUS_LOST,
styles.STATUS_WON,
styles.STATUS_PLAYING,
]}>
{this.props.remainingSeconds}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: '#ddd',
flex: 1,
paddingTop: 30,
},
target: {
fontSize: 50,
backgroundColor: '#bbb',
margin: 50,
textAlign: 'center',
},
randomContainer: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-around',
},
/*random:{
backgroundColor:'#999',
width:100,
marginHorizontal:15,
marginVertical:25,
fontSize:35,
textAlign:'center'
},*/
STATUS_PLAYING: {
backgroundColor: '#bbb',
textAlign: 'center',
},
STATUS_WON: {
backgroundColor: 'green',
},
STATUS_LOST: {
backgroundColor: 'red',
},
});
export default Game;