Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. export default class CardBox extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. selected_cards: [],
  6. rnd_card_list: [],
  7.  
  8. cards: [
  9. {
  10. id: 1,
  11. name: "star",
  12. color: "#ffeaa5",
  13. show: false
  14. },
  15. {
  16. id: 2,
  17. name: "heart",
  18. color: "#f3c1c6",
  19. show: false
  20. },
  21. {
  22. id: 3,
  23. name: "headphones",
  24. color: "#f58b54",
  25. show: false
  26. },
  27. {
  28. id: 4,
  29. name: "bell-o",
  30. color: "#107595",
  31. show: false
  32. },
  33. {
  34. id: 5,
  35. name: "video-camera",
  36. color: "#ff0b55",
  37. show: false
  38. },
  39. {
  40. id: 6,
  41. name: "pencil",
  42. color: "#a34a28",
  43. show: false
  44. },
  45. {
  46. id: 7,
  47. name: "star",
  48. color: "#ffeaa5",
  49. show: false
  50. },
  51. {
  52. id: 8,
  53. name: "heart",
  54. color: "#f3c1c6",
  55. show: false
  56. },
  57. {
  58. id: 9,
  59. name: "headphones",
  60. color: "#f58b54",
  61. show: false
  62. },
  63. {
  64. id: 10,
  65. name: "bell-o",
  66. color: "#107595",
  67. show: false
  68. },
  69. {
  70. id: 11,
  71. name: "video-camera",
  72. color: "#ff0b55",
  73. show: false
  74. },
  75. {
  76. id: 12,
  77. name: "pencil",
  78. color: "#a34a28",
  79. show: false
  80. }
  81. ]
  82. };
  83. }
  84.  
  85. handlePress = s_card => {
  86. let rnd_list = this.state.rnd_card_list;
  87. let x = rnd_list.indexOf(s_card);
  88.  
  89. var sCard = rnd_list.filter(card => card.id === s_card.id);
  90. sCard[0].show = true;
  91.  
  92. rnd_list[x] = sCard[0];
  93.  
  94. this.setState({ rnd_list });
  95.  
  96. // if (this.state.selected_cards.length === 2) {
  97. // if (
  98. // this.state.selected_cards[0].name === this.state.selected_cards[1].name
  99. // ) {
  100. // alert("Same");
  101. // } else {
  102. // alert("Not Same");
  103. // }
  104. // this.state.selected_cards.pop();
  105. // this.state.selected_cards.pop();
  106. // }
  107. };
  108.  
  109. shuffle = arr => {
  110. for (var i = arr.length - 1; i > 0; i--) {
  111. var j = Math.floor(Math.random() * (i + 1));
  112. var temp = arr[i];
  113. arr[i] = arr[j];
  114. arr[j] = temp;
  115. }
  116. };
  117.  
  118. initGame = cards => {
  119. let rndGrid = [];
  120.  
  121. for (var i = 0; i < 12; i++) {
  122. var randomInd = Math.floor(Math.random() * Math.floor(cards.length - 1));
  123.  
  124. var card = cards[randomInd];
  125.  
  126. this.state.rnd_card_list.push(card);
  127.  
  128. cards.splice(randomInd, 1);
  129. }
  130. this.shuffle(this.state.rnd_card_list);
  131.  
  132. for (var i = 0; i < 12; i++) {
  133. rndGrid.push(
  134. <Card
  135. key={i}
  136. handlePress={this.handlePress}
  137. card={this.state.rnd_card_list[i]}
  138. />
  139. );
  140. }
  141. return rndGrid;
  142. };
  143.  
  144. render() {
  145. return (
  146. <StyledCardContainer>
  147. {this.initGame(this.state.cards)}
  148. </StyledCardContainer>
  149. );
  150. }
  151. }
  152.  
  153. -------------------------------------------------------
  154.  
  155. //card component
  156.  
  157. export default class Card extends React.Component {
  158. render() {
  159. let icon_name = "question";
  160. let icon_color = "black";
  161. if (this.props.card.show) {
  162. icon_name = this.props.card.name;
  163. icon_color = this.props.card.color;
  164. }
  165.  
  166. return (
  167. <TouchableOpacity onPress={() => this.props.handlePress(this.props.card)}>
  168. <StyledCard>
  169. <FontAwesome name={icon_name} size={40} color={icon_color} />
  170. </StyledCard>
  171. </TouchableOpacity>
  172. );
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement