-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
160 lines (133 loc) · 3.73 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Button
} from 'react-native';
import Top from './Components/Top';
import Icon from './Components/Icon';
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
userChoice: '',
computerChoice: '',
winner: ''
};
}
computerChoice() {
const randomNumber = Math.floor(Math.random() * 3);
let computerChoice = '';
switch (randomNumber) {
case 0: computerChoice = 'Pedra'; break;
case 1: computerChoice = 'Papel'; break;
case 2: computerChoice = 'Tesoura'; break;
default: computerChoice = 'Desisto'; break;
}
this.setState({ computerChoice });
}
gameWinner() {
const {
userChoice,
computerChoice
} = this.state;
let winner;
switch (computerChoice) {
case 'Pedra':
switch (userChoice) {
case 'Pedra': winner = 'Empate'; break;
case 'Papel': winner = 'Você ganhou'; break;
case 'Tesoura': winner = 'Você perdeu'; break;
default: winner = 'Empate'; break;
}
break;
case 'Papel':
switch (userChoice) {
case 'Papel': winner = 'Empate'; break;
case 'Tesoura': winner = 'Você ganhou'; break;
case 'Pedra': winner = 'Você perdeu'; break;
default: winner = 'Empate'; break;
}
break;
case 'Tesoura':
switch (userChoice) {
case 'Tesoura': winner = 'Empate'; break;
case 'Pedra': winner = 'Você ganhou'; break;
case 'Papel': winner = 'Você perdeu'; break;
default: winner = 'Empate'; break;
}
break;
default: this.setState({ winner: 'Ocorreu um erro' }); break;
}
this.setState({ winner });
}
makeMyChoice(userChoice) {
this.setState({ userChoice });
this.computerChoice();
this.gameWinner();
}
render() {
const {
userChoice,
computerChoice,
winner
} = this.state;
return (
<View>
<Top></Top>
<View style={styles.PanelActions}>
<View style={styles.btnChoice}>
<Button title="Pedra" onPress={ () => { this.makeMyChoice('Pedra')}} />
</View>
<View style={styles.btnChoice}>
<Button title="Papel" onPress={ () => { this.makeMyChoice('Papel')}} />
</View>
<View style={styles.btnChoice}>
<Button title="Tesoura" onPress={ () => { this.makeMyChoice('Tesoura')}} />
</View>
</View>
<View
style={styles.stage}>
<Text style={styles.winner}>{winner}</Text>
<Icon
style={styles.stage}
player={'Escolha do computador: ' + computerChoice}
choice={computerChoice}
></Icon>
<Icon
style={styles.stage}
player={'Sua escolha: ' + userChoice}
choice={userChoice}
></Icon>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
btnChoice: {
width: 90
},
PanelActions: {
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 10
},
stage: {
alignItems: 'center',
marginTop: 10
},
winner: {
fontSize: 25,
fontWeight: 'bold',
color: 'red',
height: 60
}
});