css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .container {
  padding: 10px;
  margin: 10px;
}

.square {
  background: #fff;
  border: 1px solid #999;
  float: left;
  font-size: 24px;
  font-weight: bold;
  line-height: 34px;
  margin-right: 1px;
  margin-top: 1px;
  padding: 0;
  text-align: center;
  width: 40px;
  height: 40px;
  color: black;
}

.square:hover {
  cursor: pointer;  
}

.square:focus {
  outline: none;
}

.game {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.game-info {
  margin: 20px;
  text-align: center;
}

.game-history {
  margin: 20px;
  text-align: center;
}

.status {
  margin-bottom: 10px;
}

.game-title {
  margin: 20px;
  text-align: center;
}

            
          
!
            
              // I extended a basic tic tac toe done following tutorial at
// https://facebook.github.io/react/tutorial/tutorial.html
// with all the requirements needed

const INIT_HISTORY = [{
        squares: Array(9).fill(null),
      }];

const INIT_STATE = {
      history: INIT_HISTORY,
      playerIsNext: true,
      stepNumber: 0,
      player: null,
      winner: null
    };

const GameInfo = (props) => {
  const { 
    player, 
    playerIsNext, 
    history,
    winner,
    onClick,
    stepNumber,    
  } = props;
  
  let status;
  if (winner) {
    status = 'Winner: ' + winner;
  } else if (player) {
    
    const opponent = (player === 'X') ? 'O' : 'X';
    status = 'Next player: ' + (playerIsNext ? player : opponent);
  } else {
    status = 'Select X or O';
  }

  const moves = (history.length <= 1 || winner) ? [] : history.map((step, move) => {
    let moveText = 'Move #' + move;
    moveText = (stepNumber === move) ? <b>{moveText}</b> : moveText;
        
    const desc = move ? moveText : <b>Game start</b>;

    return (
        <div key={move}>
          <a href="#" onClick={() => onClick(move)}>{desc}</a>
        </div>
      );
  });

  return (
    <div className="game-info">
      <div>{status}</div>
      <div className="game-history">
        {moves}
      </div>
    </div>
  );
}

const Square = (props) => {
  const { onClick, value } = props;
  return (
    <button 
      className="btn btn-primary square"
      onClick={onClick}>
      {value}
    </button>
  );
}

const ChoosePlayer = (props) => {
  const { onClick, player } = props;  

  if (player) {
    return null;
  }
      
  return (
    <div className="game">
      <div className="board-row">
        <Square 
          value="X"
          onClick={() => onClick("X")}
          />
        <Square 
          value="O"
          onClick={() => onClick("O")}
          />
      </div>
    </div>
  );
}

class Board extends React.Component {
  
  renderSquare(i) {
    return (<Square 
              value={this.props.squares[i]}
              onClick={() => this.props.onClick(i)}
              />);
  }

  render() {
 
    if (!this.props.player) {
      return null;
    }
    
    return (
      <div className="game">
      <div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </div>
      </div>
      </div>
    );
  }
}

class TicTacToeGame extends React.Component {
  constructor() {
    super();
    this.state = INIT_STATE;
  }
 
  jumpTo(step) {
    this.setState({
      stepNumber: step,
      playerIsNext: (step % 2) ? false : true,
      winner: null,
    });
  }
  
  resetGame(player) {
    this.setState({ ...INIT_STATE, player: player });
  }

  setPlayer(player) {
    this.setState({
      player: player,
    });
  }
  
  setMove(i) {
    const { stepNumber, 
           player, 
           playerIsNext,
           winner
    } = this.state;
    
    const history = this.state.history.slice(0, stepNumber + 1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();

    if (winner || squares[i]) {
      return;
    }
    
    const opponent = (player === 'X') ? 'O' : 'X';
    squares[i] = playerIsNext ? player : opponent;

    const hasEmptyIndex = squares.some( (s) => { return (s != "O" && s != "X") } );
    
    // was there a winner or an even?
    let nextWinner = calculateWinner(squares);
    
    if (!nextWinner && !hasEmptyIndex) {
      nextWinner = "-";
    }
    
    this.setState({
      history: history.concat([{
        squares: squares
      }]),
      stepNumber: history.length,
      playerIsNext: !playerIsNext,
      winner: nextWinner,
    });
    
  }
  
  handleClick(i) {
    const { 
      playerIsNext,
      winner
    } = this.state;

    if (playerIsNext && !winner) {
      this.setMove(i);
    }
    
  }
    
  render() {
    const { 
      history, 
      stepNumber,
      playerIsNext,
      player,
      winner
    } = this.state;

    const current = history[stepNumber];  
    
    if (winner) {
      // there was a winner, reset the game
      // or no more moves! game over!
      // restart game anyway
      setTimeout(() => {this.resetGame(player)}, 2000);      
    } else if (!playerIsNext) {
      
      // now the computer has to play in a sec
      let emptyIndex = 0;
      const hasEmptyIndex = current.squares.some( (s,idx) => {
        if (s != "O" && s != "X") { 
          emptyIndex = idx; 
          return true;
        };
      } );
      if (!hasEmptyIndex) {
        // no more moves! game over!
        console.log('kind of error, no more moves! game over!');
      } else {
        // computer's move in 2 secs
        const computer = player==="X"?"O":"X";
        const bestMove = calcMinimax(current.squares, computer, player, computer);

        setTimeout(() => {this.setMove(bestMove)}, 500);  
      }
      
    }
    
    return (
      <div className="container">
        <div className="row">
          <div className="game-title">
          <h3>Tic Tac Toe
          </h3>
            <small>A React Game, not unbeatable</small>
        </div>
        </div>
        <div className="row">
            <ChoosePlayer 
              player={player}
              squares={current.squares}
              onClick={(i) => this.setPlayer(i)}
            />
            <Board 
              player={player}
              squares={current.squares}
              onClick={(i) => this.handleClick(i)}
            />

        </div>
        <div className="row">
            <GameInfo
              stepNumber={stepNumber}
              player={player}
              playerIsNext={playerIsNext}
              history={history}
              winner={winner}
              onClick={(move) => this.jumpTo(move)}
              />
        </div>
      </div>
    )
  }
}

ReactDOM.render(
    <TicTacToeGame />, 
    document.getElementById('app')
);

// returns list of the indexes of empty spots on the board
function emptyIndexies(squares) {
  return  squares.reduce( (acc, val, idx) => {
		if (!val) {
			acc.push(idx);
		}
		return acc;
	}, [] );
}

function calculateWinner(squares) {
  const 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],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}

function isWinner(squares, player) {
	return calculateWinner(squares) === player;
}

function calcMinimax(squares, playerToHelp, human, computer) {

	function calcScores(squares, availSpots, depth) {

		if (isWinner(squares, computer)) { // player
			// computer would win
			if (computer === playerToHelp) {
				return 10 - depth;			
			} else {
				return depth - 10;
			}
		} else if (isWinner(squares, human)) { // opponent
			// human would win
			if (human === playerToHelp) {
				return 10 - depth;			
			} else {
				return depth - 10;
			}
		} else if (availSpots.length === 0) {
			// no more moves available, it is a draw
			return 0;
		} 
		
		return null;
	}

  // the main minimax function
	function minimax(squares, player, depth) {

    // available spots
		const availSpots = emptyIndexies(squares);

		//
		const result = calcScores(squares, availSpots, depth);

		if (result) {
			return result; // terminate
		}

		const scores = [];
		// loop through available spots
		// fill moves and scores
		const moves = availSpots.map((val, idx) => {

      const nextPlayer = (player === human) ? computer : human;
			
			//move to test with current player
			squares[val] = player; // e.g. set "X" at the idx place
			//recursive with depth+1
						
			const score = minimax(squares, nextPlayer, depth + 1);
			scores.push(score);
			//reverse move
			squares[val] = null;			
			return val; // moves.push move
		});
		
		let scoreIndex = 0;
		if (player === playerToHelp) {
			// playerToHelp has to maximize its points: max calc
			scoreIndex = scores.reduce(
				(iMax, x, i, arr) => x > arr[iMax] ? i : iMax, 0);
		} else {
			// opponents wants to minimize playerToHelp points: min calc
			scoreIndex = scores.reduce(
				(iMin, x, i, arr) => x < arr[iMin] ? i : iMin, 0);			
		}
		
		if (depth === 0) {
			return moves[scoreIndex]; // best move
		}
		return scores[scoreIndex];
	}
	
	const res = minimax(squares, playerToHelp, 0);
	return res;
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console