-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
179 lines (162 loc) · 4.1 KB
/
App.js
File metadata and controls
179 lines (162 loc) · 4.1 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
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
177
178
179
/**
* "Sequence" game
* Andrew Kahn
* January 2021
*
* @format
* @flow strict-local
*/
import React, {useState, useEffect} from 'react';
import {
Alert,
Pressable,
SafeAreaView,
StyleSheet,
StatusBar,
Text,
View,
} from 'react-native';
const App: () => React$Node = () => {
const Tile = (props) => {
return (
<Pressable
onPress={() => {
const res = props.match(props.number);
if (!res.matches) {
Alert.alert('Fail.', '', [
{text: 'TRY AGAIN', onPress: () => props.restart()},
]);
}
}}
style={{
flex: 1,
}}>
{({pressed}) => (
// <Text>id is {props.id}</Text>
<View
style={{
flex: 1,
backgroundColor: props.active || pressed ? 'white' : props.color,
}}
/>
)}
</Pressable>
);
};
const Tiles = (props) => {
const getRandom = () => Math.floor(Math.random() * 4);
// // TODO change data structure [color] -> {color, key, active}
// const colors = ['tomato', 'goldenrod', 'seagreen', 'steelblue'];
// return colors.map((color) => {});
// // todo make color nonstring so manipulations can be done - + for lighter etc
const [tiles, setTiles] = useState([getRandom()]); //[0, 1, 2, 3]); // todo make generator so no need for pos? - but need pos for checking.. could be in gen too
const [pos, setPos] = useState(0);
const [matchPos, setMatchPos] = useState(0);
const [activeTile, setActiveTile] = useState(-1);
const match = (input) => {
if (input === tiles[matchPos]) {
let newMatchPos = matchPos;
if (tiles.length > matchPos + 1) {
newMatchPos = matchPos + 1;
} else {
setTiles(tiles.concat([getRandom()]));
newMatchPos = 0;
setPos(0);
}
setMatchPos(newMatchPos);
return {
matches: true,
remain: tiles.length - newMatchPos,
input,
};
} else {
return {
matches: false,
reason: `input ${input} does not match in ${tiles.toString()} at pos ${matchPos.toString()}`,
};
}
};
useEffect(() => {
function tick() {
if (tiles.length > pos) {
setActiveTile(tiles[pos]);
setPos(pos + 1);
setTimeout(() => {
setActiveTile(-1);
}, 500);
} else {
setActiveTile(-1);
}
}
const timerID = setInterval(() => tick(), 600);
return function cleanup() {
clearInterval(timerID);
};
}, [pos, tiles]);
const resume = () => {
setPos(0);
};
const restart = () => {
setTiles([getRandom()]);
setPos(0);
setMatchPos(0);
setActiveTile(-1);
};
return (
<>
<Tile
number={0}
match={match}
resume={resume}
restart={restart}
color={'tomato'}
active={activeTile === 0}
/>
<Tile
number={1}
match={match}
resume={resume}
restart={restart}
color={'goldenrod'}
active={activeTile === 1}
/>
<Tile
number={2}
match={match}
resume={resume}
restart={restart}
color={'seagreen'}
active={activeTile === 2}
/>
<Tile
number={3}
match={match}
resume={resume}
restart={restart}
color={'steelblue'}
active={activeTile === 3}
/>
{/* <Text>active tile is {activeTile}</Text> */}
</>
);
};
// todo do playsequence one second after load
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.container}>
<Tiles />
</SafeAreaView>
</>
);
};
// todo generate palette randomly - make number of tiles configurable
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'stretch',
},
});
export default App;