Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. "use strict";
  2.  
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4.  
  5. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  6.  
  7. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  8.  
  9. function Square(props) {
  10. return React.createElement(
  11. "button",
  12. { className: "square", onClick: props.onClick },
  13. props.value
  14. );
  15. }
  16.  
  17. var Board = function (_React$Component) {
  18. _inherits(Board, _React$Component);
  19.  
  20. function Board() {
  21. _classCallCheck(this, Board);
  22.  
  23. return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
  24. }
  25.  
  26. Board.prototype.renderSquare = function renderSquare(i) {
  27. var _this2 = this;
  28.  
  29. return React.createElement(Square, {
  30. value: this.props.squares[i],
  31. onClick: function onClick() {
  32. return _this2.props.onClick(i);
  33. }
  34. });
  35. };
  36.  
  37. Board.prototype.render = function render() {
  38. return React.createElement(
  39. "div",
  40. null,
  41. React.createElement(
  42. "div",
  43. { className: "board-row" },
  44. this.renderSquare(0),
  45. this.renderSquare(1),
  46. this.renderSquare(2)
  47. ),
  48. React.createElement(
  49. "div",
  50. { className: "board-row" },
  51. this.renderSquare(3),
  52. this.renderSquare(4),
  53. this.renderSquare(5)
  54. ),
  55. React.createElement(
  56. "div",
  57. { className: "board-row" },
  58. this.renderSquare(6),
  59. this.renderSquare(7),
  60. this.renderSquare(8)
  61. )
  62. );
  63. };
  64.  
  65. return Board;
  66. }(React.Component);
  67.  
  68. var Game = function (_React$Component2) {
  69. _inherits(Game, _React$Component2);
  70.  
  71. function Game() {
  72. _classCallCheck(this, Game);
  73.  
  74. var _this3 = _possibleConstructorReturn(this, _React$Component2.call(this));
  75.  
  76. _this3.state = {
  77. history: [{
  78. squares: Array(9).fill(null)
  79. }],
  80. stepNumber: 0,
  81. xIsNext: true
  82. };
  83. return _this3;
  84. }
  85.  
  86. Game.prototype.handleClick = function handleClick(i) {
  87. var history = this.state.history.slice(0, this.state.stepNumber + 1);
  88. var current = history[history.length - 1];
  89. var squares = current.squares.slice();
  90. if (calculateWinner(squares) || squares[i]) {
  91. return;
  92. }
  93. squares[i] = this.state.xIsNext ? "X" : "O";
  94. this.setState({
  95. history: history.concat([{
  96. squares: squares
  97. }]),
  98. stepNumber: history.length,
  99. xIsNext: !this.state.xIsNext
  100. });
  101. };
  102.  
  103. Game.prototype.jumpTo = function jumpTo(step) {
  104. this.setState({
  105. stepNumber: step,
  106. xIsNext: step % 2 === 0
  107. });
  108. };
  109.  
  110. Game.prototype.render = function render() {
  111. var _this4 = this;
  112.  
  113. var history = this.state.history;
  114. var current = history[this.state.stepNumber];
  115. var winner = calculateWinner(current.squares);
  116.  
  117. var moves = history.map(function (step, move) {
  118. var desc = move ? 'Go to move #' + move : 'Go to game start';
  119. return React.createElement(
  120. "li",
  121. { key: move },
  122. React.createElement(
  123. "button",
  124. { onClick: function onClick() {
  125. return _this4.jumpTo(move);
  126. } },
  127. desc
  128. )
  129. );
  130. });
  131.  
  132. var status = undefined;
  133. if (winner) {
  134. status = "Winner: " + winner;
  135. } else {
  136. status = "Next player: " + (this.state.xIsNext ? "X" : "O");
  137. }
  138.  
  139. return React.createElement(
  140. "div",
  141. { className: "game" },
  142. React.createElement(
  143. "div",
  144. { className: "game-board" },
  145. React.createElement(Board, {
  146. squares: current.squares,
  147. onClick: function onClick(i) {
  148. return _this4.handleClick(i);
  149. }
  150. })
  151. ),
  152. React.createElement(
  153. "div",
  154. { className: "game-info" },
  155. React.createElement(
  156. "div",
  157. null,
  158. status
  159. ),
  160. React.createElement(
  161. "ol",
  162. null,
  163. moves
  164. )
  165. )
  166. );
  167. };
  168.  
  169. return Game;
  170. }(React.Component);
  171.  
  172. // ========================================
  173.  
  174. ReactDOM.render(React.createElement(Game, null), document.getElementById("root"));
  175.  
  176. function calculateWinner(squares) {
  177. var lines = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]];
  178. for (var i = 0; i < lines.length; i++) {if (window.CP.shouldStopExecution(1)){break;}
  179. var _lines$i = lines[i];
  180. var a = _lines$i[0];
  181. var b = _lines$i[1];
  182. var c = _lines$i[2];
  183.  
  184. if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
  185. return squares[a];
  186. }
  187. }
  188. window.CP.exitedLoop(1);
  189.  
  190. return null;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement