-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
189 lines (179 loc) · 4.65 KB
/
App.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
import React, { useState } from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import OptionChoosed from './src/components/OptionChoosed';
import ButtonAction from './src/components/ButtonAction';
import SummaryResult from './src/components/SummaryResult';
const ROCK = 'Rock';
const PAPER = 'Paper';
const SCISSORS = 'Scissors';
const listOption = [
{
label: ROCK,
uri: 'http://pngimg.com/uploads/stone/stone_PNG13622.png',
},
{
label: PAPER,
uri: 'https://www.stickpng.com/assets/images/5887c26cbc2fc2ef3a186046.png',
},
{
label: SCISSORS,
uri:
'http://pluspng.com/img-png/png-hairdressing-scissors-beauty-salon-scissors-clipart-4704.png',
},
];
export default function App() {
const [resultGame, setResultGame] = useState({
label: 'Choose an option!',
color: 'black',
});
const [listPlayer, setListPlayer] = useState([
{
name: 'Player',
optionIndex: null,
},
{
name: 'Computer',
optionIndex: null,
},
]);
const [summaryResultData, setSummaryResultData] = useState({
numWon: 0,
numLose: 0,
numTied: 0,
});
function handleClickButton(item) {
listPlayer[0].optionIndex = listOption.indexOf(item);
// computer choose
const index = Math.floor(Math.random() * listOption.length);
listPlayer[1].optionIndex = index;
//
setListPlayer([...listPlayer]);
setResultGame(getResultGame(item.label, listOption[index].label));
}
function getResultGame(optionPlayer, optionComputer) {
if (optionPlayer === optionComputer) {
setSummaryResultData((state) => {
++state.numTied;
return {
...state,
};
});
return { label: 'Tie game!', color: 'black' };
}
let isWin = true;
if (optionPlayer === ROCK) {
isWin = optionComputer === SCISSORS;
}
if (optionPlayer === PAPER) {
isWin = optionComputer === ROCK;
}
if (optionPlayer === SCISSORS) {
isWin = optionComputer === PAPER;
}
if (isWin) {
setSummaryResultData((state) => {
++state.numWon;
return {
...state,
};
});
return { label: 'Victory!', color: 'green' };
} else {
setSummaryResultData((state) => {
++state.numLose;
return {
...state,
};
});
return { label: 'Defeat!', color: 'red' };
}
}
function handleResetGame() {
setResultGame({
label: 'Choose an option!',
color: 'black',
});
listPlayer[0].optionIndex = null;
listPlayer[1].optionIndex = null;
setListPlayer([...listPlayer]);
setSummaryResultData({
numWon: 0,
numLose: 0,
numTied: 0,
});
}
return (
<SafeAreaView>
<ScrollView>
<View style={styles.container}>
<Text style={[{ color: resultGame.color }, styles.resultGameLabel]}>
{resultGame.label}
</Text>
<View style={styles.gameContainer}>
<OptionChoosed
namePlayer={listPlayer[0].name}
option={
listPlayer[0].optionIndex !== null
? listOption[listPlayer[0].optionIndex]
: {}
}
/>
<Text>vs</Text>
<OptionChoosed
namePlayer={listPlayer[1].name}
option={
listPlayer[1].optionIndex !== null
? listOption[listPlayer[1].optionIndex]
: {}
}
/>
</View>
<View style={styles.listButtonWrapper}>
{listOption.map((item) => {
return (
<ButtonAction
key={item.label}
item={item}
onPress={handleClickButton}
/>
);
})}
</View>
<SummaryResult
summaryResultData={summaryResultData}
onResetGame={handleResetGame}
/>
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
padding: 20,
},
resultGameLabel: {
fontSize: 30,
fontWeight: 'bold',
},
gameContainer: {
margin: 10,
borderWidth: 2,
shadowRadius: 5,
paddingVertical: 30,
paddingHorizontal: 10,
borderColor: 'grey',
shadowOpacity: 0.9,
flexDirection: 'row',
backgroundColor: 'white',
shadowColor: 'rgba(0,0,0,0.2)',
shadowOffset: { height: 5, width: 5 },
width: '100%',
alignItems: 'center',
},
});