]> git.cworth.org Git - lmno.games/blobdiff - tictactoe/tictactoe.jsx
tictactoe: Don't let the user send an illegal move
[lmno.games] / tictactoe / tictactoe.jsx
index 61a828eb751d6662e63681aa6e313cb6ed95dd20..d92b698fe57b913e8d8625643b18376bf0262947 100644 (file)
@@ -36,18 +36,31 @@ events.addEventListener("game-state", event => {
 });
 
 function Square(props) {
 });
 
 function Square(props) {
+  let className = "square";
+
+  if (props.value) {
+    className += " occupied";
+  } else if (props.active) {
+    className += " open";
+  }
+
+  const onClick = props.active ? props.onClick : null;
+
   return (
   return (
-    <button className="square" onClick={props.onClick}>
+    <div className={className}
+         onClick={onClick}>
       {props.value}
       {props.value}
-    </button>
+    </div>
   );
 }
 
 class Board extends React.Component {
   renderSquare(i) {
   );
 }
 
 class Board extends React.Component {
   renderSquare(i) {
+    const value = this.props.squares[i];
     return (
       <Square
     return (
       <Square
-        value={this.props.squares[i]}
+        value={value}
+        active={! this.props.gameOver && ! value}
         onClick={() => this.props.onClick(i)}
       />
     );
         onClick={() => this.props.onClick(i)}
       />
     );
@@ -173,6 +186,7 @@ class Game extends React.Component {
         </div>
         <div className="game-board">
           <Board
         </div>
         <div className="game-board">
           <Board
+            gameOver={winner}
             squares={current.squares}
             onClick={i => this.handleClick(i)}
           />
             squares={current.squares}
             onClick={i => this.handleClick(i)}
           />