Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                $color-light-gray: #ecf0f1;
$color-medium-gray: #bdc3c7;
$color-dark-gray: #34495e;
$color-light-green: #1abc9c;
$color-medium-green: #16a085;

html, body {
  width: 100%;
  height: 100%;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 900px;
  background-color: $color-light-gray;
  font-family: Roboto;
  overflow: hidden;
}

.game {
  position: relative;
  display: flex;
  width: 80vmin;
  height: 80vmin;
  transform: rotateY(0deg);
  transform-style: preserve-3d;
  transition: all 1s ease-out;
  
  &.rotated {
    transform: rotateY(180deg);
  }
  
  .line {
    position: absolute;
    width: 76%;
    height: 76%;
    transform: translateZ(6vmin);
    top: 12%;
    left: 12%;
    pointer-events: none;
    z-index: 1;
    
    &.visible {
      path {
        stroke-dashoffset: 0px;
      }
    }
    
    path {
      stroke-width: 10px;
      stroke-dashoffset: 141.42px; // The size of the diagonal
      stroke-dasharray: 141.42px;
      stroke: $color-light-gray;
      transition: stroke-dashoffset 1.5s cubic-bezier(0.4,0,0.2,1);
    }
  }
  
  .board {
    display: flex;
    flex-direction: column;
    flex: 1;
    transform-style: preserve-3d;
    
    .row {
      display: flex;
      flex: 1;
      margin-bottom: 2%;
      justify-content: space-between;
      transform-style: preserve-3d;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

.results {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  transform: translateZ(-6vmin) rotateY(-180deg);
  background-color: white;
  backface-visibility: hidden;
  transform-origin: bottom;
  
  .message {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 10vmin;
    
    .symbol {
      display: flex;
      height: 35vmin;
      
      svg {
        flex: 1;
      }
    }
    
    .text {
      font-size: 32px;
      text-transform: uppercase;
      flex: 0 0 10vmin;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  
  .replay {
    height: 15vmin;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: $color-dark-gray;
    color: white;
    font-size: 5vmin;
    text-transform: uppercase;
    cursor: pointer;
    
    &:hover {
      background-color: rgba($color-dark-gray, 0.7);
    }
  }
}

.pawn path {
  stroke: $color-dark-gray; 
  stroke-dasharray: 301.635; 
  stroke-dashoffset: 0;
  fill: none;
  stroke-width: 16px;
}

.cube {
  flex: 0 0 32%;
  position: relative;
  transform: rotateY(0deg);
  transition: all 0.5s ease-out;
  cursor: pointer;
  transform-style: preserve-3d;
  
  &.rotated {
    transform: rotateY(180deg);
  }
  
  div {
    position: absolute;
    background-color: $color-light-green;
    box-shadow: inset 0 0 0 1vmin rgba($color-dark-gray, 0.2);
    width: 100%;
    height: 100%;
    
    svg {
      width: 80%;
      height: 80%;
      
      path {
        stroke: $color-dark-gray; 
        stroke-dasharray: 301.635; 
        stroke-dashoffset: 0;
        fill: none;
        stroke-width: 16px;
        transition: stroke-dashoffset 1.225s cubic-bezier(0.4,0,0.2,1);
      }
    }
    
    &.back {
      transform: translateZ(-5vmin);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20vmin;
    } 
    
    &.front {
      transform: translateZ(5vmin) rotateY(180deg);
    }
    
    &.right {
      transform-origin: right;
      transform: translateZ(5vmin) rotateY(270deg);
      width: 10vmin;
      right: 0;
      background-color: $color-dark-gray;
    }
    
    &.left {
      transform-origin: left;
      transform: translateZ(5vmin) rotateY(90deg);
      width: 10vmin;
      background-color: $color-dark-gray;
    }
    
    &.bottom {
      transform-origin: bottom;
      transform: translateZ(5vmin) rotateX(90deg);
      height: 10vmin;
      bottom: 0;
      background-color: $color-dark-gray;
    }
    
    &.top {
      transform-origin: top;
      transform: translateZ(5vmin) rotateX(270deg);
      height: 10vmin;
      background-color: $color-dark-gray;
    }
  }
}
              
            
!

JS

              
                const X = 'X';
const O = 'O';

// The following array holds all the possible 
// winning combinations for the game. Using 
// bitwise arithmatic, each cell is represented 
// by 2 to the power of the cell's number 
// (2^0,2^1,2^2, ... , 2^8). The winning 
// combination is the sum of all the numbers 
// in the corresponding cells (can also be 
// achieved by OR-ing two numbers). For example,
// the winning combination for the first row is
// 1+2+4 = 7. These patterns are compared with
// each player's pattern by using bitwise AND.
// if the result of the AND operation is equal 
// to the winning pattern, that player is the winner.
const WINNING_PATTERNS = [
  7, 56, 448,	  // Horizontal
  73, 146, 292,	// Vertical
  273, 84			  // Cross
];

const Circle = () => (
  <svg className="pawn circle" viewBox="0 0 128 128">
    <path d="M64,16A48,48 0 1,0 64,112A48,48 0 1,0 64,16"/>
  </svg>
);

const Times = () => (
  <svg className="pawn times" viewBox="0 0 128 128">
    <path d="M16,16L112,112"/>
    <path d="M112,16L16,112"/>
  </svg>
);

class Line extends React.PureComponent {
  
  d() {
    const {pattern} = this.props;
    return {
      7: `M 0,5 H 100`,
      56: `M 0,50 H 100`,
      448: `M 0,95 H 100`,
      73: `M 5,0 V 100`,
      146: `M 50,0 V 100`,
      292: `M 95,0 V 100`,
      273: `M 0,0 L 100,100`,
      84: `M 100,0 L 0,100`,
    }[pattern];
  }
  
  render() {
    const {show} = this.props;
    return (
      <svg className={`line ${show ? 'visible' : ''}`} viewBox='0 0 100 100'>
        <path d={this.d()}/>
      </svg>
    );
  }
}

const Cube = ({value, onClick}) => (
  <div className={`cube ${value ? 'rotated' : ''}`} onClick={onClick} ref={ref => this.ref = ref}>
    {['top','bottom','left','right','front','back'].map(face => ( 
      <div className={face}>
        {face === 'back' && value === O && <Circle/>}
        {face === 'back' && value === X && <Times/>}
      </div>
    ))}
  </div>
);

const Row = ({children}) => <div className="row">{children}</div>

const Results = ({winner, draw, onPlayAgain}) => (
  <div className='results'>
    <div className='message'>
      <div className='symbol'>
        {winner === X && <Times/>}
        {winner === O && <Circle/>}
        {draw && <React.Fragment><Times/><Circle/></React.Fragment>}
      </div>
      <div className='text'>
        {winner ? 'Wins!' : 'Draw!'}
      </div>
    </div>
    <div className='replay' onClick={onPlayAgain}>Play Again</div>
  </div>
);

const Board = ({board, onClick}) => (
  <div className="board">
    {board.map((row, i) => (
      <Row>
        {row.map((col, j) => (
          <Cube value={col} onClick={() => onClick(i, j)}/>
        ))}
      </Row>
    ))}
  </div>
);

class Game extends React.PureComponent {
  
  constructor(props) {
    super(props);
    this.state = this.getInitialState();
  }
  
  getInitialState() {
    return {
      player: X,
      patterns: {[X]: 0, [O]: 0},
      winner: null,
      rotated: false,
      board: [
        [null,null,null],
        [null,null,null],
        [null,null,null]
      ]
    };
  }
  
  handleOnClick = (i, j) => {
    if(null === this.state.board[i][j]) {
      const {player, board, patterns} = this.state;
      const state = {
        board: [...board],
        player: player === X ? O : X,
        patterns: {...patterns}
      };
      
      // Set the value in the board
      state.board[i][j] = player;
      
      // Add the value to the player pattern using bitwise OR
      state.patterns[player] = state.patterns[player] |= Math.pow(2,(i*3+j));
      
      state.winner = this.checkForWin(state.patterns);
      
      this.setState(state);
      
      if(state.winner || this.isBoardFull(board)) {
        setTimeout(() => {
          this.setState({rotated: true});
        }, 1500);
      }
    }
  };

  checkForWin(patterns) {
    // Loop through all possible winning sets
		for(let i = 0; i < WINNING_PATTERNS.length; i++) {
			// Use bitwise AND to determind if the player's score
			// Holds a winning combination
			if((WINNING_PATTERNS[i] & patterns[X]) === WINNING_PATTERNS[i])
				return X;
			if((WINNING_PATTERNS[i] & patterns[O]) === WINNING_PATTERNS[i])
				return O;
		}
		// No winner
		return false;
  }

  isBoardFull(board) {
    return !this.state.board.some((row, i) => {
      return row.some((col, j) => null === col)
    })
  }

  handleOnPlayAgain = () => {
    this.setState({rotated: false});
    setTimeout(() => {
      this.setState(this.getInitialState());
    }, 1000);
  };

  getWinningPattern() {
    const {winner, patterns} = this.state;
    return WINNING_PATTERNS.find(pattern => (pattern & patterns[winner]) === pattern);
  }
  
  render() {
    const {board, winner, rotated} = this.state;
    return (
      <div className={`game ${rotated ? 'rotated' : ''}`}>
        <Results winner={winner} draw={!winner && this.isBoardFull(board)} onPlayAgain={this.handleOnPlayAgain}/>
        <Line show={winner} pattern={this.getWinningPattern()}/>
        <Board board={board} onClick={this.handleOnClick}/>
      </div>
    );
  }
}

ReactDOM.render(
  <Game/>,
  document.body
);
              
            
!
999px

Console