Advertisement
Guest User

Untitled

a guest
Feb 13th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { store } from "./store";
  4. import { useEffect } from "react";
  5. import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
  6.  
  7.  
  8. const render = ReactDOM.render(<App />, document.getElementById("root"));
  9. store.suscribe(render);
  10. render();
  11.  
  12. // App.js
  13.  
  14. function App() {
  15.   useEffect(() => {
  16.     //this is printed on every click on the "shuffle" from the Game component button.
  17.     console.log("heyyy from App");
  18.   });
  19.   return (
  20.     <div className="App">
  21.       <Router>
  22.         <Switch>
  23.           <Route path="/game">
  24.             <Game />
  25.           </Route>
  26.           <Route path="/">
  27.             <Main />
  28.           </Route>
  29.         </Switch>
  30.       </Router>
  31.     </div>
  32.   );
  33. }
  34.  
  35.  
  36.  
  37. // Game.js
  38.  
  39. const NUM_OF_CARDS = 52;
  40. const MAX_CARD_VALUE = 13;
  41.  
  42. function Game() {
  43.   //shuffle cards with Fisher Yates algorithm
  44.   const shuffle = (array) => {
  45.     for (let i = array.length - 1; i > 0; i--) {
  46.       let j = Math.floor(Math.random() * (i + 1));
  47.       [array[i], array[j]] = [array[j], array[i]];
  48.     }
  49.   };
  50.  
  51.   const initGame = {
  52.     type: "INIT_GAME",
  53.     payload: (state) => {
  54.       // creates an array of size 52 filled with 1..13 four times
  55.       const cards = Array(NUM_OF_CARDS / MAX_CARD_VALUE)
  56.         .fill(
  57.           Array(13)
  58.             .fill()
  59.             .map((_, i) => i + 1)
  60.         )
  61.         .flat();
  62.       shuffle(cards);
  63.       return {
  64.         ...state,
  65.         cards,
  66.         // distributes cards evenly
  67.         computer: {
  68.           ...state.computer,
  69.           cards: cards.slice(0, NUM_OF_CARDS / 2),
  70.         },
  71.         player: { ...state.player, cards: cards.slice(NUM_OF_CARDS / 2) },
  72.       };
  73.     },
  74.   };
  75.  
  76.   useEffect(() => {
  77.     store.dispatch(initGame);
  78.   }, []);
  79.  
  80.   const shuffleCards = () => {
  81.     const cards = [...store.getState().player.cards];
  82.     shuffle(cards);
  83.     store.dispatch({
  84.       type: "SHUFFLE",
  85.       payload: (state) => {
  86.         return {
  87.           ...state,
  88.           player: { ...state.player, cards },
  89.         };
  90.       },
  91.     });
  92.   };
  93.  
  94.   return (
  95.     <div>
  96.       {store.getState().player.cards?.map((card) => (
  97.         <div>{card}</div>
  98.       ))}
  99.       <div>----------------</div>
  100.       {store.getState().computer.cards?.map((card) => (
  101.         <div>{card}</div>
  102.       ))}
  103.  
  104.       <button onClick={shuffleCards}>shuffle</button>
  105.     </div>
  106.   );
  107. }
  108.  
  109. export default Game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement