]> git.cworth.org Git - lmno.games/blobdiff - tictactoe/tictactoe.jsx
Rename stepNumber to step_number
[lmno.games] / tictactoe / tictactoe.jsx
index d92b698fe57b913e8d8625643b18376bf0262947..d462ae14f164f34b74037d4820f919beaa60d3f1 100644 (file)
@@ -1,3 +1,12 @@
+const Team = {
+  X: 0,
+  O: 1,
+  properties: {
+    0: {name: "X"},
+    1: {name: "O"}
+  }
+};
+
 function undisplay(element) {
   element.style.display="none";
 }
@@ -109,8 +118,8 @@ class Game extends React.Component {
           squares: Array(9).fill(null)
         }
       ],
-      stepNumber: 0,
-      xIsNext: true
+      step_number: 0,
+      next_to_play: Team.X
     };
   }
 
@@ -125,27 +134,32 @@ class Game extends React.Component {
           squares: Array(9).fill(null)
         }
       ],
-      stepNumber: 0,
-      xIsNext: true
+      step_number: 0,
+      next_to_play: Team.X
     });
   }
 
   receiveMove(i) {
-    const history = this.state.history.slice(0, this.state.stepNumber + 1);
+    const history = this.state.history.slice(0, this.state.step_number + 1);
     const current = history[history.length - 1];
     const squares = current.squares.slice();
     if (calculateWinner(squares) || squares[i]) {
       return;
     }
-    squares[i] = this.state.xIsNext ? "X" : "O";
+    squares[i] = Team.properties[this.state.next_to_play].name;
+    let next_to_play;
+    if (this.state.next_to_play === Team.X)
+      next_to_play = Team.O;
+    else
+      next_to_play = Team.X;
     this.setState({
       history: history.concat([
         {
           squares: squares
         }
       ]),
-      stepNumber: history.length,
-      xIsNext: !this.state.xIsNext
+      step_number: history.length,
+      next_to_play: next_to_play
     });
   }
 
@@ -160,23 +174,16 @@ class Game extends React.Component {
     }
   }
 
-  jumpTo(step) {
-    this.setState({
-      stepNumber: step,
-      xIsNext: (step % 2) === 0
-    });
-  }
-
   render() {
     const history = this.state.history;
-    const current = history[this.state.stepNumber];
+    const current = history[this.state.step_number];
     const winner = calculateWinner(current.squares);
 
     let status;
     if (winner) {
       status = "Winner: " + winner;
     } else {
-      status = "Next player: " + (this.state.xIsNext ? "X" : "O");
+      status = "Next player: " + (Team.properties[this.state.next_to_play].name);
     }
 
     return (