-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
107 lines (95 loc) · 2.62 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
import React, {useState} from 'react';
import {StyleSheet, View, Text, Button, Image} from 'react-native';
import logo from './imgs/jokenpo.png';
import Pedra from './imgs/Pedra.png';
import Papel from './imgs/Papel.png';
import Tesoura from './imgs/Tesoura.png';
const App = () => {
const [choiceUser, setChoiceUser] = useState('');
const [choiceCPU, setChoiceCPU] = useState('');
const [result, setResult] = useState('');
function playGame(user, cpu) {
let winner = '';
if (user === cpu) {
winner = 'Empate';
} else if (user === 'Pedra') {
winner = cpu === 'Papel' ? 'Você perdeu' : 'Você ganhou';
} else if (user === 'Papel') {
winner = cpu === 'Tesoura' ? 'Você perdeu' : 'Você ganhou';
} else if (user === 'Tesoura') {
winner = cpu === 'Pedra' ? 'Você perdeu' : 'Você ganhou';
}
setResult(winner);
}
function jokenpo(choice) {
const random_number = Math.floor(Math.random() * 3);
const choicesOfGame = ['Pedra', 'Papel', 'Tesoura'];
setChoiceCPU(choicesOfGame[random_number]);
setChoiceUser(choice);
playGame(choice, choicesOfGame[random_number]);
}
function renderChoice(player, action) {
if (action === 'Pedra') {
return (
<>
<Text>{player}</Text>
<Image source={Pedra} />
</>
);
} else if (action === 'Papel') {
return (
<>
<Text>{player}</Text>
<Image source={Papel} />
</>
);
} else if (action === 'Tesoura') {
return (
<>
<Text>{player}</Text>
<Image source={Tesoura} />
</>
);
} else {
return false;
}
}
return (
<View style={{flex: 1}}>
<View>
<Image source={logo} />
</View>
<View style={styles.containerButtons}>
<Button title="Pedra" onPress={() => jokenpo('Pedra')} />
<Button title="Papel" onPress={() => jokenpo('Papel')} />
<Button title="Tesoura" onPress={() => jokenpo('Tesoura')} />
</View>
<View style={styles.containerResults}>
<Text style={styles.result}>{result}</Text>
{renderChoice('Usuário', choiceUser)}
{renderChoice('Computador', choiceCPU)}
</View>
</View>
);
};
const styles = StyleSheet.create({
containerButtons: {
marginTop: 10,
marginBottom: 10,
flexDirection: 'row',
justifyContent: 'space-around',
},
containerResults: {
flex: 1,
padding: 40,
justifyContent: 'space-around',
alignItems: 'center',
},
result: {
height: 40,
fontSize: 25,
color: 'red',
fontWeight: 'bold',
},
});
export default App;