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.

            
              .arrow {
  display: inline-block;
  width: 9%;
  margin: 1% 1.6%;
  color: #3D9970;
  font-size: 30px;
}

.arrow:hover {
  color: darkgreen;
}

.arrow:hover {
  cursor: pointer;
}

@media screen and (max-width: 992px) and (min-width: 768px) {
  .arrow {
    width: 8.7%;
  }
}

.board-holder {
  height: 640px;
  border-left: 25px solid #0077BE;
  border-right: 25px solid #0077BE;
  border-bottom: none;
  position: relative;
}

.board {
  z-index: 2;
  background: #DFC12A;
}

@media screen and (max-width: 600px) {
  .container .board-holder {
    border-left: 15px solid #0077BE;
    border-right: 15px solid #0077BE;
  }
}

.space {
  height: 70px;
  background: #FFFFF6;
  border-radius: 50%;
  width: 9%;
  margin: 1% 2%;
  position: relative;
}

.chip {
  z-index: 1;
  border-radius: 50%;
  display: none;
  height: 70px;
  width: 100%;
  bottom: 0;
  left: 0;
  position: absolute;
  overflow: hidden;
}

.player-drop {
  display: block;
  animation: drop 1s linear;
}

@keyframes drop {
  0% { transform: translateY(-800px); }
  100% { transform: translateY(0px); }
}

@keyframes clearboard {
  0% { transform: translateY(0px); }
  100% { transform: translateY(700px); }
}

.container {
  width: 65%;
  max-width: 865px;
  margin: auto;
}

@media screen and (max-width: 767px) {
  .container {
    width: 100%;
  }
}

.header {
  color: #3D9970;
  display: flex;
  justify-content: space-around;
  margin: .2em auto auto 3em;
  font-size: 2em;
}

.game-over {
  color: #3D9970;
  text-transform: uppercase;
  text-align: center;
  animation: dropfromtop 1s linear;
}

.black {
  color: #000;
}

.red {
  color: red;
}

.title {
  font-size: 1.2em;
}

.player-change {
  font-size: 1em;
  color: #DFC12A;
  font-family: 'Francois One', sans-serif;
  text-align: right;
  animation: slideleft .5s linear;
  transform: translate(-100px);
  width: 50%;
}

.hide-player {
  display: none;
}

@keyframes slideleft {
  from { transform: translate(500px);}
  to { transform: translate(-100px);}
}

@keyframes dropfromtop {
  from { transform: translateY(-100px); }
  to { transform: translateY(0px); }
}

@media screen and (max-width: 768px) and (min-width: 200px) {
  .header {
    font-size: 1.3em;
    margin-left: 0;
  }

  .header .player-change {
    transform: translate(0px);
  }

  @keyframes slideleft {
    from { transform: translate(500px);}
    to { transform: translate(0px);}
  }
}

.reset-container {
  text-align: center;
}

.lever-container {
  position: absolute;
  left: -75px;
  bottom: 71px;
}

.lever-text {
  color: #3D9970;
  position: absolute;
  transform: translate(-80px);
  bottom: 0;
  font-family: 'Francois One', sans-serif;
  display: inline-block;
  animation: bounceright 2s infinite;
}

.lever {
  display: inline-block;
  width: 50px;
  height: 16px;
  background: #0077BE;
}

.lever:hover {
  background: red;
}

.arrow-bounce {
  font-size: 30px;
  vertical-align: bottom;
  margin-left: 10px;
}

.reset-button {
  display: none;
  margin: auto;
  width: 60px;
  height: 25px;
  border-radius: 5px;
  color: white;
  background: #0077BE;
  padding: auto 5px;
}

.refresh-icon {
  padding-right: 5px;
  font-size: 15px;
}

@keyframes bounceright {
  0% { transform: translate(-80px);}
  50% { transform: translate(-60px);}
  100% { transform: translate(-80px);}
}

@media screen and (max-width: 768px) {
  .lever-container {
    display: none;
  }

  .reset-button {
    display: inline-block;
  }
}

.row {
  height: 80px;
  width: 100%;
  z-index: 1;
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}

.row:first-child {
  padding-top: 20px;
}

.row:last-child {
  padding-bottom: 20px;
}

body {
  background: #FFFFF6;
  box-sizing: border-box;
}
            
          
!
            
              const { Component } = React;

const App = () => {
  return (
    <Container />
  );
}

class Container extends Component {
  constructor(props) {
    super(props);
    this.state = {
      board: this.createBoard(),
      playerOneTurn: true,
      lastColClicked: null,
      gameOver: false,
      reset: false
    };

    this.handleClick = this.handleClick.bind(this);
    this.updateBoard = this.updateBoard.bind(this);
    this.checkScore = this.checkScore.bind(this);
    this.arrows = this.arrows.bind(this);
    this.handleReset = this.handleReset.bind(this);
    this.resetState = this.resetState.bind(this);
  }

  createBoard() {
    return Array(6).fill().map(arr => Array(7).fill(null));
  }

  updateBoard(prevState, colIndex) {
    const board = prevState.board.slice();
    const row = board.slice().reverse().filter(row => row[colIndex] === null)[0];
    const rowIndex = board.indexOf(row);
    board[rowIndex][colIndex] = prevState.playerOneTurn === true ? 'playerone' : 'playertwo';
    return board;
  }

  checkScore(board, playerOneTurn) {
    const player = playerOneTurn  ? 'playerone' : 'playertwo';

    for (let c=0; c < (board[0].length - 3); c++) {
      for (let r=0; r < board.length; r++) {
        if (board[r][c] === player && board[r][c+1] === player && board[r][c+2] === player && board[r][c+3] === player) {
          return `${player} wins across!!!`;
        }
      }
    }

    for (let c=0; c < board[0].length; c++) {
      for (let r=0; r < (board.length - 3); r++) {
        if (board[r][c] === player && board[r+1][c] === player && board[r+2][c] === player && board[r+3][c] === player) {
          return `${player} wins vertically!!!`;
        }
      }
    }  

    for (let c=0; c < (board[0].length - 3); c++) {
      for (let r=0; r < (board.length - 3); r++) { 
        if (board[r][c] === player && board[r+1][c+1] === player && board[r+2][c+2] === player && board[r+3][c+3] === player) {
          return `${player} wins diagonally!!!`;
        }
      }
    }

    for (let c=0; c < (board[0].length - 3); c++) {
      for (let r=3; r < board.length; r++) {
        if (board[r][c] === player && board[r-1][c+1] === player && board[r-2][c+2] === player && board[r-3][c+3] === player) {
          return `${player} wins diagonally!!!`;
        }
      }
    }  

    return false;
  }

  handleClick(colIndex) {
    this.setState(prevState => {
      const board = this.updateBoard(prevState, colIndex);

      const gameOver = this.checkScore(board, prevState.playerOneTurn);

      return {
        board,
        playerOneTurn: !this.state.playerOneTurn,
        lastColClicked: colIndex,
        gameOver
      }
    });
  }

  arrows() {
    return Array(7).fill().map((_, i) => {
      const colFull = !this.state.board.map((row) => row[i]).some(item => item === null);

      return <Arrow 
              key={i} 
              colIndex={i} 
              handleClick={this.handleClick} 
              gameOver={this.state.gameOver}
              colFull={colFull}
              reset={this.state.reset}
             />
    })
  }

  resetState() {
    this.setState({
      board: this.createBoard(),
      playerOneTurn: true,
      lastColClicked: null,
      gameOver: false,
      reset: false
    });
  }

  handleReset() {
    this.setState({reset: true});
    setTimeout(this.resetState, 1400);
  }

  render() {
    return (
      <div>
        <Header playerOneTurn={this.state.playerOneTurn} gameOver={this.state.gameOver} handleReset={this.handleReset}/>
        <div className="container">
          <Row style={{height: '20px', marginBottom: '40px', marginLeft: '20px'}}>
            {this.arrows()}
          </Row>
          <Board {...this.state} handleReset={this.handleReset} />
        </div>
      </div>
    );
  }
}

const Header = ({playerOneTurn, gameOver, handleReset}) => {
  return(
    <div>
      {!gameOver ? (
        <div className="header">
          <h1 className="title">Connect Four</h1>
          <h1 className={playerOneTurn ? 'player-change' : 'hide-player'} >Player <span className="red">One</span> Go!</h1>
          <h1 className={!playerOneTurn ? 'player-change' : 'hide-player'}>Player <span className="black">Two</span> Go!</h1>
        </div>
      ) : (
        <h1 className="game-over">{gameOver.split('player').join('player ')}</h1>
      )}
      <Reset handleReset={handleReset} lever={false} />
    </div>
  );
}

const Board  = ({board, reset, handleReset}) => {
  const rows = (_, i) => {
    return (
      <Row key={i}>
        {rowOutput(spaces(i), 7)}
      </Row>
    );
  }

  const spaces = (rowIndex) => {
    return (_, i) => {
      return <Space key={i} player={board[rowIndex][i]} reset={reset} />;
    };
  }

  const rowOutput = (row, num) => {
    return Array(num).fill().map(row)
  }

  return (
    <div className="board-holder">
      <div className="board">
        {rowOutput(rows, 6)}
      </div>
      <Reset handleReset={handleReset} lever={true} />
    </div>
  );
}

const Space = ({player, reset}) => {
  const background = () => {
    const style = {}
    style.background = player === 'playerone' ? 'red' : 'black';

    console.log(reset);
    if (reset) {
      style.animation = 'clearboard 1.5s linear';
    }
    return style;
  }

  return (
    <div className="space">
      { player !== null &&
        <div className="chip player-drop" style={background()} ></div>
      }
    </div>
  );
}

const Arrow = ({colIndex, handleClick, gameOver, colFull, reset}) => {
  function sendKey() {
    return handleClick(colIndex);
  }

  function columnFull() {
    return colFull ? {visibility: 'hidden'} : {};
  }

  if (!gameOver && !reset) {
    return <i className="fa fa-arrow-circle-down arrow" style={columnFull()} onClick={sendKey}></i>
  } else {
    return null;
  }
}

const Reset = ({handleReset, lever}) => {
  return (
    <div className="reset-container">
      {lever ? (
        <div className="lever-container">
          <div className="lever-text">
            Reset <i class="fa fa-arrow-right" aria-hidden="true"></i>
          </div>    
          <div className="lever" onClick={handleReset}></div>
        </div>
      ) : (
        <button className="reset-button" onClick={handleReset}>
          <i className="fa fa-refresh"></i>
        </button>
      )}
    </div>
  );
}

const Row = (props) => {
  return (
    <div className="row" style={props.style}>
      {props.children}
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('root'));
            
          
!
999px
Loading ..................

Console