-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyOfApp.js
123 lines (100 loc) · 2.49 KB
/
CopyOfApp.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
import React from 'react';
import {
StyleSheet,
Text,
View ,
Button,
TextInput
} from 'react-native';
import Root from './src';
export default class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
suits: ['spades', 'hearts', 'diamonds', 'clubs'],
cards: ['X'],
cardsToDistribute: []
};
this.distributeCards = this.distributeCards.bind(this);
this.generateCardsInitially = this.generateCardsInitially.bind(this);
this.onChangeText = this.onChangeText.bind(this);
this.calculatePoints = this.calculatePoints.bind(this);
this.generateName = this.generateName.bind(this);
}
componentWillMount() {
this.generateCardsInitially();
}
generateCardsInitially() {
const {
suits,
cards
} = this.state;
for (var i = 0; i < suits.length; i++) {
for (let j = 1; j <= 13; j++) {
cards.push({
suit: suits[i],
value: j,
name: this.generateName(j),
location: null,
isClosed: false,
points: this.calculatePoints(suits[i], j),
isSelected: false
});
}
}
this.setState({
cards
});
}
calculatePoints(suit, value) {
if (suit !== 'spades' && value === 1)
return 1;
if (suit === 'diamonds' && value === 10)
return 2;
if (suit === 'spades')
return value;
return 0;
}
generateName(value) {
if (value === 1)
return 'A';
if (value === 11)
return 'J';
if (value === 12)
return 'Q';
if (value === 13)
return 'K';
return value;
}
distributeCards(numberOfCardsToDistribute) {
if (numberOfCardsToDistribute !== 0) {
console.log(this.state.cards.length);
const index = Math.ceil((Math.random()) * (this.state.cards.length - 1));
console.log(numberOfCardsToDistribute, index);
this.setState({
cardsToDistribute: this.state.cardsToDistribute.concat([
this.state.cards[index]
]),
cards: this.state.cards.filter((item, i) => i !== index)
}, () => this.distributeCards(numberOfCardsToDistribute - 1));
}
}
onChangeText(numberOfCardsToDistribute) {
this.setState({
numberOfCardsToDistribute
});
}
render() {
// const {
// container,
// textField,
// button
// } = styles;
const {
numberOfCardsToDistribute,
cardsToDistribute,
cards
} = this.state;
return <Root/>;
}
}