Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Board from './Board';
  3.  
  4. class Game extends Component {
  5. constructor() {
  6. super();
  7. this.state = {
  8. stepNumber: 0,
  9. history: [{
  10. squares: Array(9).fill({ value: null, highlighted: false }),
  11. location: null
  12. }],
  13. xIsNext: true,
  14. ascending: true
  15. };
  16. }
  17.  
  18. handleClick(i) {
  19. const history = this.state.history.slice(0, this.state.stepNumber + 1);
  20. const current = history[this.state.stepNumber];
  21. const squares = current.squares.slice();
  22. if (calculateWinner(squares) || squares[i].value) {
  23. return;
  24. }
  25.  
  26. squares[i] = { value: this.state.xIsNext ? 'X' : 'O', highlighted: false };
  27. const winLine = calculateWinner(squares);
  28. if (winLine) {
  29. for (let i in winLine) {
  30. squares[winLine[i]] = { value: squares[winLine[i]].value, highlighted: true };
  31. }
  32. }
  33. const x = (i % 3) + 1;
  34. const y = Math.floor(i / 3) + 1;
  35. this.setState({
  36. stepNumber: history.length,
  37. history: history.concat([{
  38. squares: squares,
  39. location: {
  40. x: x,
  41. y: y
  42. }
  43. }]),
  44. xIsNext: !this.state.xIsNext
  45. });
  46. }
  47.  
  48. jumpTo(step) {
  49. this.setState({
  50. stepNumber: step,
  51. xIsNext: (step % 2) ? false : true
  52. });
  53. }
  54.  
  55. toggleOrder() {
  56. this.setState({
  57. ascending: !this.state.ascending
  58. });
  59. }
  60.  
  61. render() {
  62. const history = this.state.history;
  63. const current = history[this.state.stepNumber];
  64. const winLine = calculateWinner(current.squares);
  65. const descending = !this.state.ascending;
  66.  
  67. let status;
  68. if (winLine) {
  69. status = 'Winner: ' + current.squares[winLine[0]].value;
  70. } else {
  71. status = 'Next player: ' + (this.state.xIsNext? 'X' : 'O');
  72. }
  73.  
  74. let moves = history.map((step, move) => {
  75. let desc = step.location ? 'Move (' + step.location.x + ', ' + step.location.y + ')' : 'Game start';
  76. if (move === this.state.stepNumber) {
  77. desc = <b>{desc}</b>;
  78. }
  79. return (
  80. <li key={move}>
  81. <a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
  82. </li>
  83. );
  84. });
  85. if (descending) {
  86. moves.sort((a, b) => { return b.key - a.key; });
  87. }
  88.  
  89. return (
  90. <div className="game">
  91. <div className="game-board">
  92. <Board
  93. squares={current.squares}
  94. onClick={(i) => this.handleClick(i)}
  95. />
  96. </div>
  97. <div className="game-info">
  98. <div>{status}</div>
  99. <div><button onClick={() => this.toggleOrder()}>Toggle order</button></div>
  100. <ol>{moves}</ol>
  101. </div>
  102. </div>
  103. );
  104. }
  105. }
  106.  
  107. function calculateWinner(squares) {
  108. const lines = [
  109. [0, 1, 2],
  110. [3, 4, 5],
  111. [6, 7, 8],
  112. [0, 3, 6],
  113. [1, 4, 7],
  114. [2, 5, 8],
  115. [0, 4, 8],
  116. [2, 4, 6],
  117. ];
  118. for (let i = 0; i < lines.length; i++) {
  119. const [a, b, c] = lines[i];
  120. if (squares[a].value && squares[a].value === squares[b].value && squares[a].value === squares[c].value) {
  121. return [a, b, c];
  122. }
  123. }
  124. return null;
  125. }
  126.  
  127. export default Game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement