Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
65
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.  
  4. import { tttButton } from './style';
  5. import { isndef } from './type';
  6.  
  7. var tictactoe = require('./games/tictactoe.js');
  8.  
  9. class Game extends React.Component {
  10.     constructor(props) {
  11.         super(props);
  12.  
  13.         this.checkEnd = this.checkEnd.bind(this);
  14.         this.handleClick = this.handleClick.bind(this);
  15.         this.reset = this.reset.bind(this);
  16.     }
  17.  
  18.     componentWillMount() {
  19.         this.reset();
  20.     }
  21.  
  22.     reset() {
  23.         tictactoe.newGame();
  24.  
  25.         this.setState({field: tictactoe.getField()});
  26.     }
  27.  
  28.     handleClick(row, col) {
  29.         if (tictactoe.play(row, col)) {
  30.             this.setState({field: tictactoe.getField()});
  31.  
  32.             setTimeout(this.checkEnd, 100);
  33.         }
  34.     }
  35.  
  36.     checkEnd() {
  37.         let gameEnd = tictactoe.checkEnd();
  38.  
  39.         if (isndef(gameEnd)) {
  40.             alert('Draw');
  41.  
  42.             this.reset();
  43.         }
  44.         else {
  45.             if (gameEnd === 0) {
  46.                 alert('First player won!');
  47.  
  48.                 this.reset();
  49.             }
  50.             else if (gameEnd === 1) {
  51.                 alert('Second player won!');
  52.  
  53.                 this.reset();
  54.             }
  55.         }
  56.     }
  57.  
  58.     makeSquare(row, col, val, key) {
  59.         return <Square value={val} row={row} col={col} key={key} handleClick={this.handleClick}/>;
  60.     }
  61.  
  62.     render() {
  63.         let rows = [];
  64.         for (let row of [0, 1, 2]) {
  65.             let cols = [];
  66.             for (let col of [0, 1, 2]) {
  67.                 let val = this.state.field[row][col];
  68.                 cols.push(this.makeSquare(row, col, val, col));
  69.             }
  70.             rows.push(<div key={row}> {cols} </div>);
  71.         }
  72.  
  73.         return <div> {rows} </div>;
  74.     }
  75. }
  76.  
  77. class Square extends React.Component {
  78.     constructor(props) {
  79.         super(props);
  80.  
  81.         this.onClick = this.onClick.bind(this);
  82.     }
  83.  
  84.     shouldComponentUpdate(newProps, newState) {
  85.         return this.props.value != newProps.value;
  86.     }
  87.  
  88.     onClick(event) {
  89.         this.props.handleClick(this.props.row, this.props.col);
  90.     }
  91.  
  92.     render() {
  93.         return <button style={tictactoe.buttonStyle(this.props.value)} onClick={this.onClick}/>
  94.     }
  95. }
  96.  
  97. class App extends React.Component {
  98.     render() {
  99.         return <Game/>;
  100.     }
  101. }
  102.  
  103. document.addEventListener('DOMContentLoaded', function() {
  104.     ReactDOM.render(<App/>, document.getElementById('app'));
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement