cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              @import url('https://fonts.googleapis.com/css?family=Rock+Salt');
body {
  font: 14px "Rock Salt", Futura, sans-serif;
  justify-content: center;
  background-color: #6F866C;
  color: white;
  text-align: center;
  padding: 50px 0;
}

ol,
ul {
  padding-left: 30px;
}

.board-row:after {
  clear: both;
  content: "";
  display: table;
}

.status {
  margin: 10px 6em;
  padding: 0 10em;
}

.square {
  background: #6F866C;
  color: white;
  border: 1px solid #fff;
  //border-image: url('https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS6W7sLUTdYKiGCcQc38Y4Cqc2nffXdeh27SwxeSETBoOIMEOrShg') 27 27 27 27 round stretch;
  float: left;
  font-size: 10vh;
  font-weight: bold;
  line-height: 10vh;
  height: 20vh;
  margin-right: -1px;
  margin-top: -1px;
  padding: 0;
  text-align: center;
  justify-content: center;
  width: 15vw;
  font-family: "Rock Salt", Futura, sans-serif;
}

.board-row:first-child {
  .square {
    border-top-width: 0px;
  }
}

.board-row:last-child {
  .square {
    border-bottom-width: 0px;
  }
}

.square:last-child {
  border-right-width: 0px;
}

.square:first-child {
  border-left-width: 0px;
}

.square:focus {
  outline: none;
}

.board {
  margin-left: 20vw;
  text-align: center;
  justify-content: center;
}

.kbd-navigation .square:focus {
  background: #ddd;
}

.game {
  display: flex;
  flex-direction: row;
}

.game-info {
  margin: 0 auto;
}


/* The CSS */

select {
  padding: 3px;
  margin: 0;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  -webkit-box-shadow: 0 3px 0 #ccc, 0 -1px #fff inset;
  -moz-box-shadow: 0 3px 0 #ccc, 0 -1px #fff inset;
  //box-shadow: 0 3px 0 #ccc, 0 -1px #fff inset;
  border: none;
  outline: none;
  display: inline-block;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  font-family: "Rock Salt", monospace;
  line-height: 2em;
  font-weight: bold;
  cursor: pointer;
}


/* Targetting Webkit browsers only. FF will show the dropdown arrow with so much padding. */

  select {
    padding-right: 18px;
    font: 11px "Rock Salt", monospace;
    margin-bottom: 2em;
  }

form {
  position: relative;
  display: flex;
  flex-flow: column nowrap;
  max-width: 30em;
  align-content: center;
  max-height: 40em;
  margin: 0 auto;
}

label {
  display: flex;
  flex-flow: column nowrap;
  margin-bottom: 2em;
}

label:after {
  //content:'<>';
  font: 11px "Rock Salt", monospace;
  //color:#aaa;
  -webkit-transform: rotate(90deg);
  -moz-transform: rotate(90deg);
  -ms-transform: rotate(90deg);
  transform: rotate(90deg);
  right: 8px;
  top: 2px;
  padding: 0 0 2px;
  border-bottom: 1px solid #ddd;
  position: absolute;
  pointer-events: none;
}

label:before {
  //content:'';
  right: 6px;
  top: 0px;
  width: 20px;
  height: 20px;
  position: absolute;
  pointer-events: none;
  display: block;
}

.submit {
  color: #fff;
  background-color: #5F765C;
  text-shadow: -1px 1px #417cb8;
  padding: 1em 13.5em;
  border-radius: 4px;
  margin-top: 1em;
  border: none;
  text-decoration: none;
}

.submit:hover {
  background-color: #4F664C;
  text-shadow: -1px 1px #27496d;
}

a {
  color: white;
  text-decoration: none;
}
@media (max-width: 700px) {
  .square {
    
  border: 2px solid #999;
  border-image: linear-gradient(white, white);
  }
}
            
          
!
            
              function Square(props) {
  return (
    <button className="square" onClick={() => props.onClick()} >
        {props.value}
      </button>
  );

}
class Board extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      playerOneTurn: props.start === props.marker,
      playerTwoMark: props.marker === 'X' ? 'O' : 'X'
    };

    this.computerPlay = this.computerPlay.bind(this);
    this.computerPlay();
  }
  handleClick(i, isPlayer) {
    const squares = this.state.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    if (!(isPlayer && !this.state.playerOneTurn && this.props.value == 'one')) {
      squares[i] = this.state.playerOneTurn ? this.props.marker : this.state.playerTwoMark;

      this.setState({
        squares: squares,
        playerOneTurn: !this.state.playerOneTurn
      });

    }

  }
  renderSquare(i) {
    return <Square value = {this.state.squares[i]} onClick={() => this.handleClick(i, true)} />;
  }

  computerPlay() {
    var self = this;
    self.set = setInterval(function() {
      if (self.props.value === 'one' && !self.state.playerOneTurn && !calculateWinner(self.state.squares)) {
        self.handleClick(optimalMove(self.state.squares, self.props.marker, self.state.playerTwoMark), false);
      }
    }, 1000);
  }

  render() {
    const winner = calculateWinner(this.state.squares);
    let status;
    if (winner) {
      status = 'Winner: ' + winner;
    } else {
      status = 'Next player: ' + (this.state.playerOneTurn ? this.props.marker : this.state.playerTwoMark);
    }

    return (
      <div>
        {winner&&<input type='submit' onClick={(e) => this.props.restart(e)} className="submit status" value=' Click Here to Start Over' />}
        <div className="status"> {status} </div>
        <div className="board">
        <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 Game extends React.Component {
  constructor() {
    super();
    this.state = {
      startMark: Math.random() < .5 ? 'X' : 'O',
      value: '',
      marker: '',
      ready: false
    };
    this.start = this.start.bind(this);
    this.restart = this.restart.bind(this);
  }
  start(val, mark, ready) {

    this.setState({
      value: val,
      marker: mark,
      ready: ready
    });
  }

  restart(event) {
    this.setState({
      ready: false,
      startMark: Math.random() < .5 ? 'X' : 'O'
    });
    event.preventDefault();
  }
  render() {
    if (this.state.ready === true) {
      return (
        <div className="game">
        <div className="game-board">
          <Board restart= {this.restart} value={this.state.value} marker={this.state.marker} start={this.state.startMark} />
        </div>
        <div className="game-info">
          <div></div>
          <ol>{/* TODO */}</ol>
        </div>
      </div>
      );
    } else {
      return (
        <Prompt start= {this.start} />
      );
    }
  }
}

class Prompt extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: 'one',
      marker: 'X'
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleMarkerChange = this.handleMarkerChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({
      value: event.target.value
    });
  }

  handleMarkerChange(event) {
    this.setState({
      marker: event.target.marker
    });
  }

  handleSubmit(event) {
    this.props.start(this.state.value, this.state.marker, true);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
        <label>
          How would you like to play today?
          <select value={this.state.value} onChange={this.handleChange}>
            <option value='one'>One Player</option>
            <option value='two'>Two Players</option>
          </select>
        </label>
        <label>
          Which side are you on?
          <select value={this.state.marker} onChange={this.handleMarkerChange}>
            <option value='X'>X</option>
            <option value='O'>O</option>
          </select>
        </label>
        </div>
        <input className = 'submit' type='Submit' value='Submit' readOnly onClick={this.handleSubmit} />
      </form>
    );
  }

}

/* ==========
{console.log(window.ReactRouter)}
        <Game value="two" marker="x" />
  ==============================*/

ReactDOM.render(
  <Game />,
  document.getElementById('container')
);

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];
    }
  }
  if (noMovesLeft(squares)) {
    return 'nobody :(';
  }
  return null;
}

function noMovesLeft(squares) {
  return squares.join('').length === 9;
}

function evaluate(squares, comp, user) {
  if (calculateWinner(squares) == comp)
    return 10;
  else if (calculateWinner(squares) == user)
    return -10;
  return 0;
}

// This is the minimax function. It considers all
// the possible ways the game can go and returns
// the value of the board
function minimax(squares, user, comp, depth, isCompTurn) {
  let score = evaluate(squares, comp, user);

  // If comp has won the game return score
  if (score === 10)
    return score;

  // If user has won the game return score
  if (score == -10)
    return score;

  // If no moves available and no winner then tie
  if (noMovesLeft(squares))
    return 0;
  //is computer's turn
  if (isCompTurn) {
    let best = -1000;
    //traverse all squares
    for (let i = 0; i < 9; i++) {
      if (squares[i] === null) {
        squares[i] = comp;
        best = Math.max(best, minimax(squares, user, comp, depth + 1, !isCompTurn));
        squares[i] = null;
      }
    }
    return best;
    //is user's turn
  } else {
    let best = 1000;
    //traverse all squares
    for (let i = 0; i < 9; i++) {
      if (squares[i] === null) {
        squares[i] = user;
        best = Math.min(best, minimax(squares, user, comp, depth + 1, !isCompTurn));
        squares[i] = null;
      }
    }
    return best;

  }
}

function optimalMove(squares, user, comp) {
  let best = -1000;
  let square = -1;
  let val;
  if (squares.join('').length == 0) {
    return 4;
  }
  //traverse all squares
  for (let i = 0; i < 9; i++) {
    if (squares[i] === null) {
      squares[i] = comp;
      val = minimax(squares, user, comp, 0, false);
      squares[i] = null;

      if (val > best) {
        best = val;
        square = i;
      }
    }
  }
  return square
}
            
          
!
999px
Loading ..................

Console