Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from "react"
  2.  
  3. interface Props {
  4. }
  5.  
  6. interface State {
  7. }
  8.  
  9. export class App extends React.Component<Props, State> {
  10.  
  11.     public render() {
  12.  
  13.         // te zmienne daj jako pola tej klasy
  14.         // czyli "public PLAYER1:string = 'fa-circle';
  15.         // itd...
  16.         const PLAYER1 = 'fa-circle';
  17.         const PLAYER2 = 'fa-times';
  18.         let round = 1;
  19.         let winner = '';
  20.  
  21.         // to też
  22.         var board = [
  23.             ['', '', ''],
  24.             ['', '', ''],
  25.             ['', '', '']
  26.         ];
  27.  
  28.         // to też
  29.         const combinations = [
  30.             [0, 1, 2],
  31.             [3, 4, 5],
  32.             [6, 7, 8],
  33.             [0, 3, 6],
  34.             [1, 4, 7],
  35.             [2, 5, 8],
  36.             [0, 4, 8],
  37.             [2, 4, 6]
  38.         ]
  39.  
  40.  
  41.         // to wypierdol wyżej. jako metoda:
  42.         // protected start(f) {...}
  43.         function start(f) {
  44.  
  45.             // tak się w JSX'ach nie dobieramy do elementów
  46.             // robimy to w propsach elemntu
  47.             // <div ref={(ref)=> this._ref = ref}>
  48.             // czyli jak chcesz pobrać referencję do wszystkich boxów, to poniżej, tam gdzie zwracasz DOM
  49.             // najpierw zrób pole:
  50.             // protected _boxes:any = [[null,null,null],[null,null,null],[null,null,null]];
  51.             // i jeszcze musisz zmodfikować każdy kafelek:
  52.             // <div className="box fa" ref={(ref)=> this._boxes[i][k] = ref}></div>
  53.             // gdzie 'i' to rząd, a 'k' to kolumna (czyli podmień 'i' i 'k')
  54.             const boxes = [... document.querySelectorAll('.box')];
  55.  
  56.             // podobnie "click"
  57.             // robimy propsem "onClick". czyli zmodyfikuj kafelki
  58.             // <div className="box fa" (...) onClick={()=>pick(i,k)}></div>
  59.             // gdzie 'i' to rząd, a 'k' to kolumna (czyli podmień 'i' i 'k')
  60.             boxes.forEach(box => box.addEventListener('click', pick));
  61.             console.log(document.querySelectorAll('.box'));
  62.  
  63.  
  64.             // funkcję pick przenieś jako metodę
  65.             // ale zrób ją jako arrow function
  66.             // protected pick = (row, col) => {...}
  67.             // no i zawartość będziesz musiał zmodyfikować
  68.             // bo teraz nie robisz na `event`, tylko dostaniesz "rząd" i "kolumnę", a nie "event"
  69.             // czyli mneij wiecej tak:
  70.             // protected pick = (row, col) => {
  71.             //      const turn = this.round % 2 === 0 ? this.PLAYER2 : this.PLAYER1;
  72.             //      if (this.board[row][column] !== '') return;
  73.             //      if (this.winner !== '') return;
  74.             //      this.board[row][column] = turn;
  75.             //      this.check()
  76.             // }
  77.             function pick(event) {
  78.                 const {row, column} = event.target.dataset;
  79.                 const turn = round % 2 === 0 ? PLAYER2 : PLAYER1;
  80.                 if (board[row][column] !== '') return;
  81.                 if (winner !== '') return;
  82.                 event.target.classList.add(turn);
  83.                 board[row][column] = turn;
  84.                 roundCounter()
  85.                 round++;
  86.  
  87.  
  88.                 console.log(check());
  89.             }
  90.  
  91.  
  92.             // i tutaj podobnie
  93.             function check() {
  94.                 const result = board.reduce((total, row) => total.concat(row));
  95.                 let moves = {
  96.                     'fa-times': [],
  97.                     'fa-circle': []
  98.                 };
  99.                 result.forEach((field, index) => moves[field] ? moves[field].push(index) : null);
  100.                 combinations.forEach(combination => {
  101.                     if (combination.every(index => moves[PLAYER1].indexOf(index) > -1)) {
  102.                         winner = 'Winner : Player 1';
  103.                     }
  104.                     if (combination.every(index => moves[PLAYER2].indexOf(index) > -1)) {
  105.                         winner = 'Winner : Player 2';
  106.                     }
  107.                 });
  108.  
  109.                 console.log(result);
  110.  
  111.                 return winner
  112.             }
  113.  
  114.  
  115.             if (f == 'restart') {
  116.  
  117.                 round = 1;
  118.                 winner = '';
  119.                 board = [
  120.                     ['', '', ''],
  121.                     ['', '', ''],
  122.                     ['', '', '']
  123.                 ];
  124.  
  125.                 boxes.forEach(box => box.className = "box fa");
  126.                 roundCounter()
  127.  
  128.             }
  129.  
  130.             function roundCounter() {
  131.                 document.getElementById("number").innerHTML = round.toString();
  132.             }
  133.         }
  134.  
  135.  
  136.         return (
  137.             <div className="App">
  138.                 <nav className="navigation">
  139.                     <button className="start button" onClick={start}>start</button>
  140.                     <div className='turns-container'>
  141.                         <div className="turn-number">Turn number: <div id="number">0</div></div>
  142.                     </div>
  143.                     <button className="restart button" onClick={() => start('restart')}>restart</button>
  144.                 </nav>
  145.                 <div className="board">
  146.                     <div className="box fa" data-row="0" data-column="0"></div>
  147.                     <div className="box fa" data-row="0" data-column="1"></div>
  148.                     <div className="box fa" data-row="0" data-column="2"></div>
  149.                     <div className="box fa" data-row="1" data-column="0"></div>
  150.                     <div className="box fa" data-row="1" data-column="1"></div>
  151.                     <div className="box fa" data-row="1" data-column="2"></div>
  152.                     <div className="box fa" data-row="2" data-column="0"></div>
  153.                     <div className="box fa" data-row="2" data-column="1"></div>
  154.                     <div className="box fa" data-row="2" data-column="2"></div>
  155.                 </div>
  156.  
  157.             </div>
  158.         );
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement