]> git.cworth.org Git - lmno.games/commitdiff
Add an enum (or an enum-like object) for Team values
authorCarl Worth <cworth@cworth.org>
Thu, 4 Jun 2020 23:15:42 +0000 (16:15 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 4 Jun 2020 23:15:42 +0000 (16:15 -0700)
We replace the existing xIsNext Boolean in the state with a new
next_to_play property takign a value from this enum.

And we will soon also be using this enum to track the team from whose
point of view the game is being shown.

tictactoe/tictactoe.jsx

index d92b698fe57b913e8d8625643b18376bf0262947..42c32ef29041345a03d2ca2ddb7ae5cbea5fd2c2 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";
 }
@@ -110,7 +119,7 @@ class Game extends React.Component {
         }
       ],
       stepNumber: 0,
-      xIsNext: true
+      next_to_play: Team.X
     };
   }
 
@@ -126,7 +135,7 @@ class Game extends React.Component {
         }
       ],
       stepNumber: 0,
-      xIsNext: true
+      next_to_play: Team.X
     });
   }
 
@@ -137,7 +146,12 @@ class Game extends React.Component {
     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([
         {
@@ -145,7 +159,7 @@ class Game extends React.Component {
         }
       ]),
       stepNumber: history.length,
-      xIsNext: !this.state.xIsNext
+      next_to_play: next_to_play
     });
   }
 
@@ -163,7 +177,7 @@ class Game extends React.Component {
   jumpTo(step) {
     this.setState({
       stepNumber: step,
-      xIsNext: (step % 2) === 0
+      next_to_play: (step % 2) === 0
     });
   }
 
@@ -176,7 +190,7 @@ class Game extends React.Component {
     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 (