-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
190 lines (175 loc) · 4.5 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
import React, {
Component,
Text,
View,
TouchableHighlight
} from 'react-native';
import shuffle from 'knuth';
import HiraganaMap from './hiraganamap';
const pickRandomProperty = obj => {
let result;
let count = 0;
for (var prop in obj)
if (Math.random() < 1 / ++count) result = prop;
return result;
};
export default class hiragana extends Component {
constructor() {
super();
this.showChoices = this.showChoices.bind(this);
this.rightChoiceSelected = this.rightChoiceSelected.bind(this);
this.wrongChoiceSelected = this.wrongChoiceSelected.bind(this);
this.reset = this.reset.bind(this);
this.state = {
hiraganaToSolve: shuffle(Object.keys(HiraganaMap)),
correct: 0,
total: 0
};
}
reset() {
this.setState({
hiraganaToSolve: shuffle(Object.keys(HiraganaMap)),
correct: 0,
total: 0
});
}
showHiraganaToSolve() {
return (
<View><Text style={styles.hiragana}>{this.state.hiraganaToSolve[0]}</Text></View>
);
}
rightChoiceSelected() {
this.setState({
correct: this.state.correct + 1,
hiraganaToSolve: this.state.hiraganaToSolve.splice(1),
total: this.state.total + 1
});
}
wrongChoiceSelected() {
this.setState({
hiraganaToSolve: this.state.hiraganaToSolve.splice(1),
total: this.state.total + 1
});
}
showChoices() {
const HiraganaToSolve = this.state.hiraganaToSolve[0];
const CorrectAnswer = HiraganaMap[HiraganaToSolve];
// build some noise (wrong answers)
let wrongChoices = [];
while (wrongChoices.length < 3) {
let randomHiragana = pickRandomProperty(HiraganaMap);
if (randomHiragana !== HiraganaToSolve) {
wrongChoices.push(HiraganaMap[randomHiragana]);
}
}
// build the elements
let allChoices = wrongChoices.concat(CorrectAnswer);
shuffle(allChoices);
const renderChoices = () => {
return allChoices.map(choice => {
if (choice !== CorrectAnswer) {
return (
<TouchableHighlight underlayColor={styles.wrongChoice} style={styles.choice} onPress={this.wrongChoiceSelected}>
<Text>{choice}</Text>
</TouchableHighlight>
);
} else {
return (
<TouchableHighlight underlayColor={styles.rightChoice} style={styles.choice} onPress={this.rightChoiceSelected}>
<Text>{CorrectAnswer}</Text>
</TouchableHighlight>
);
}
});
};
return (
<View style={styles.choices}>
{renderChoices()}
</View>
);
}
render() {
const show = () => {
if (this.state.hiraganaToSolve.length > 0) {
return (
<View style={styles.main}>
{ this.showHiraganaToSolve() }
{ this.showChoices() }
</View>
);
} else {
let x = this.state.correct / this.state.total;
x = Math.round((x * 100 + 0.00001) * 100) / 100;
return (
<View style={styles.main}>
<Text>{ x }% correct!</Text>
<TouchableHighlight underlayColor='#00f' onPress={this.reset} style={styles.reset}>
<Text>Try again</Text>
</TouchableHighlight>
</View>
);
}
};
return (
<View style={styles.container}>
<View style={styles.info}>
<View><Text style={styles.infoText}>Correct: {this.state.correct} / {this.state.total}</Text></View>
<View><Text style={styles.infoText}>Remaining: {this.state.hiraganaToSolve.length}</Text></View>
</View>
{ show() }
</View>
);
}
}
const styles = {
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
hiragana: {
fontSize: 100,
color: 'black'
},
rightChoice: '#0c0',
wrongChoice: '#c00',
choices: {
flexDirection: 'column',
justifyContent: 'space-around',
alignItems: 'center'
},
choice: {
borderWidth: 2,
alignItems: 'center',
justifyContent: 'center',
width: 50,
height: 50,
borderRadius: 50,
margin: 2
},
reset: {
borderWidth: 3,
alignItems: 'center',
justifyContent: 'center',
width: 75,
height: 50,
borderRadius: 5
},
info: {
alignSelf: 'stretch',
alignItems: 'flex-end',
justifyContent: 'space-around',
flexDirection: 'row',
flex: 2
},
infoText: {
fontSize: 8
},
main: {
flex: 23,
alignItems: 'center',
justifyContent: 'center'
}
};