Pen Settings

HTML

CSS

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

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

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.

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

              
                <div id="message">
	
</div>

<div id="container"></div>

<audio > 
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mp3" id='audio1'>
</audio>
              
            
!

CSS

              
                import:url(https://fonts.googleapis.com/css?family=Titan+One)

$red-color: #ee0000
$green-color: #228b22
$blue-color: 	#1e90ff
$yellow-color: #ffd700
$size: 500px
$radius: 100%

 
.flex-container  
  margin: 100px auto
  display: flex
  width: $size
  height: $size
  background: #222
  flex-wrap: wrap
  border-radius: 50%
  padding: 15px
  
.colors
  margin: auto
  height: 240px
  width: 240px
  
.t-l
  background: $green-color
  border-top-left-radius : $radius 
.t-r
  background: $red-color    
  border-top-right-radius : $radius
.b-l
  background: $yellow-color
  border-bottom-left-radius : $radius
.b-r
  background: $blue-color
  border-bottom-right-radius : $radius
	
.center
  position: absolute
  margin: 150px 0 0 150px
  background: white  
  width: 200px
  height: 200px
  border: 12px solid #333
	
.anim
  opacity: 0.2
	
.count
  font-family: 'Titan One', cursive
  color : #DC0D29
  font-size : 2em
  width : 50px
  background-color: #32050C
  border : 4px solid #222
  border-radius : 10px 
  margin-left: 10px
  margin-top: -2px
  margin-right: 5px
  padding-left: 5px


.round-btn
  width : 20px
  height : 20px
  position: relative
  background-color : yellow
  border-radius : 100%
  border : 4px solid #444
  box-shadow : 0px 2px 3px #222
  margin:auto
  margin-left: 5px
  top : -5px


.round-btn:active
  box-shadow : 0 1px 1px #292929
  top : -4px

.led
  width: 16px
  height: 16px
  background-color: #32050C
  border-radius : 100%   
  border: 2px solid #222
  margin-left: 5px
 

.led-on
  background-color: #DC0D29
  margin-left: 5px
	
h2
  font-family: 'Titan One', cursive
  font-size: 25px
  text-align: center
	
h5
  text-align: center
  font-family: 'Titan One', cursive
  padding-right: 5px
  margin-top: 10px

.alert
  text-align: center
	
.btn-start
  color: #fff
  background-color: #088DA5
  border: 1px solid #088DA5
  font-family: 'Titan One', cursive
  padding: 10px
  border-radius: 25%
  float: left
	
.btn-restart
  color: #000
  background-color: #FF0000
  border: 1px solid #FF0000
  font-family: 'Titan One', cursive
  padding: 10px
  border-radius: 25%
  float: left
  margin-left: 37px
  margin-top: 10px
	
#message
  color: #31708f
  background-color: #d9edf7
  border-color: #bce8f1
  padding: 30px
  width: 50%
  text-align: center
  margin: auto
  margin-top: 10px
  font-weight: bold
  
              
            
!

JS

              
                const { Component } = React;
const { createStore } = Redux;

//All the Sound Files
var sound1 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var sound2 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var sound3 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var sound4 = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
var soundBoard = [sound1, sound2, sound3, sound4];

//Functions
function getRandomIntInclusive(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getSeries() {
	var series = [];
	for (var i = 0; i < 24; i++) {
		series[i] = getRandomIntInclusive(0, 3);
	}
	return series;
}
function lightUp(tile) {
	var toAnimate = $("#tile" + tile);

	toAnimate.addClass("anim");

	soundBoard[tile].playbackRate = 0.7;
	soundBoard[tile].play();

	setTimeout(function() {
		toAnimate.removeClass("anim");
	}, 500);
}
function checkEquality(partialSeries, lengthController, playerInput) {
	//checkEquality(state.partialSeries, state.lengthController, state.playerInputs );
	return partialSeries[lengthController] == playerInput[lengthController]
		? true
		: false;
}

//Reduces
const initialStore = {
	series: getSeries(),
	partialSeries: [],
	playerInputs: [],
	//the mode will be used for setting strict mode or free mode
	strictMode: false,
	// the turn it will be used for playng the automated sequence
	turn: "",
	//the status it will be used for making the divs clickable and the button unclickable
	status: "Beginning",
	clickable: false,
	lightUp: lightUp,
	lengthController: -1
};
function reducer(state = initialStore, { type, payload }) {
	switch (type) {
		case "t.start": {
			$("#message").hide(400);
			return { ...state, status: payload };
		}

		case "t.changeTurn": {
			$("#message").hide(400);
			return { ...state, turn: payload, clickable: true };
		}

		//assign partial series
		case "t.partialS": {
			$("#message").hide(400);
			return { ...state, partialSeries: payload };
		}

		case "t.updateController": {
			$("#message").hide(400);
			return { ...state, lengthController: payload };
		}

		case "t.setStrict": {
			$("#message").hide(400);
			return { ...state, strictMode: !state.strictMode };
		}

		case "t.restart": {
			$("#message").hide(400);
			return {
				...initialStore,
				series: getSeries(),
				strictMode: state.strictMode
			};
		}

		case "t.updateInputs": {
			$("#message").hide(400);
			//used for checking equality and incrementing partial series when needed
			state.lengthController++;
			//we add the input each time with the ... spread operator
			state.playerInputs = [...state.playerInputs, payload];

			var nextTurn = checkEquality(
				state.partialSeries,
				state.lengthController,
				state.playerInputs
			);

			//check condition for completing all 24 series
			if (state.partialSeries.length == state.series.length) {
				$("#message").show(400);
				$("#message").html("You Won YAY ;)")
				return { ...initialStore, series: getSeries() };
			}

			//check condition for incrementing partialSeries
			if (state.playerInputs.length == state.partialSeries.length && nextTurn) {
				state.lightUp(payload);
				let incremented = state.series.slice(0, state.playerInputs.length + 1);

				return {
					...state,
					partialSeries: incremented,
					turn: "PlaySeq",
					playerInputs: []
				};
			}

			if (nextTurn) {
				state.lightUp(payload);
				return { ...state };
			} else if (!state.strictMode) {
				$("#message").show(400);
				$("#message").html("WrongInput, Here is the Sequence Again")
				//we use 'NextSeq' to play our sequence again
				return { ...state, turn: "PlaySeq", playerInputs: [] };
			} else {
				$("#message").show(400);
				$("#message").html("WrongInput, Strict Mode Will Reset Everything");
				//we reset everithing back to origin
				return {
					...initialStore,
					series: getSeries(),
					strictMode: state.strictMode
				};
			}
		}

		default:
			$("#message").hide(400);
			return state;
	}
}

//Actions
function updateInputs(num) {
	return {
		type: 't.updateInputs',
		payload: num
	};
}
function updateController() {
	return {
		type: 't.updateController',
		payload: -1
	};
}
function start() {
	return {
		type: 't.start',
		payload: 'Running'
	};
}
function partialS(arr) {
  return {
    type: 't.partialS',
    payload: arr
  }
}
function changeTurnToPlayer() {
  return {
    type: 't.changeTurn',
    payload: 'player'
  }
}
function toggleStrict() {
  return {
    type: 't.setStrict',
    payload: null
  }
}
function restart() {
  return {
    type: 't.restart',
    payload: null
  }
}

//Other Stuff
const store = createStore( reducer );

class App extends Component{
  
 componentDidMount () { 
    this.unsubscribe = store.subscribe(() =>
    //each time the store will update we will rerender
      this.forceUpdate()
    );     
  }

 componentWillUnmount () {   
     this.unsubscribe();   
  }
  
  render() {   
    let state = store.getState();
    
    console.log(state);
    return(
      <div className={'flex-container'}>
        <Color1 name='colors t-l'  id='tile0' clickable={state.clickable}/>
        <Color2 name='colors t-r'  id='tile1' clickable={state.clickable}/>
        <Color3 name='colors b-l'  id='tile2' clickable={state.clickable}/>
        <Color4 name='colors b-r'  id='tile3' clickable={state.clickable}/>
        <Control
          name='center'
          series={state.series}
          lightUp={state.lightUp} 
          turn={state.turn}
          status={state.status}
          partialSeries={state.partialSeries}
          mode={state.strictMode}
          />
      </div>
    )
  }
}

class Control extends Component {  

  startGame = () => {    
        
     let first = this.props.series.slice(0,1); 
    
   //plays the animation with sound sequnces
     this.playSeq(first); 
    
    //disables clicking on button and on colors
     store.dispatch( start() ); 
    //assign the partialSeries the first time we press the button
     store.dispatch( partialS(first) ); 
  };

	toggle = () => {
  store.dispatch( toggleStrict() );
};

	restart = () => {
  store.dispatch( restart() );
};
  
 	playSeq = (sequence) =>{
  let i = 0;   
  var interval = setInterval( () =>{
    this.props.lightUp(sequence[i]);
    i++;
    if(i >= sequence.length){
      clearInterval(interval);     
      store.dispatch( changeTurnToPlayer() ); 
      store.dispatch( updateController() ); 
    }
  }, 1200)
};  

  render() {    
    if(this.props.turn === 'PlaySeq') {
      //if player inputs are correct we go to next phaze
      //or 
      //if strict mode is not on we replay the same sequnce
      this.playSeq(this.props.partialSeries);    
    }
    
    return(
      <div className={this.props.name}>
        <h2>Simon Game</h2>
        
				<div>         
           <h1 style={{float:'left'}} className='count'>{this.props.partialSeries.length}</h1>
 
             <button 
            className={this.props.status != 'Beginning' ? 'btn btn-start disabled' : 'btn btn-start  active'}               onClick={this.props.status == 'Beginning' ? this.startGame : null}> Start </button>
        </div>
				
         <div style={{float: 'right'}}>
            <button className={ this.props.mode ? 'led led-on' : 'led' }></button>
           
             <button className='round-btn' style={{ top: 5 }}
                onClick={ this.toggle }>                
           	 </button>
					 
            <h5 >STRICT</h5>
         </div>
        
				<button className='btn btn-restart' onClick={this.restart}> Restart Game </button>
      </div>
    )
  }
}

class Color1 extends Component {  

  onClick = () =>{
    if(this.props.clickable) {
      store.dispatch( updateInputs(0) );
    }
  };
  
  render() {     
    return(
      <div className={this.props.name} onClick={this.onClick} id={this.props.id} >   
      </div>
    )
  }  
}
class Color2 extends Component {
  
  onClick = (item) =>{
       if(this.props.clickable) {
      store.dispatch( updateInputs(1) );
    }
  };
 
  render() { 
    var item = this.props.item
    return(
      <div className={this.props.name} onClick={this.onClick} id={this.props.id}>   
      </div>
    )
  }  
}
class Color3 extends Component {
  
  onClick = (item) =>{
       if(this.props.clickable) {      
      store.dispatch( updateInputs(2) );
    }
  };
  
  render() { 
    var item = this.props.item
    return(
      <div className={this.props.name} onClick={this.onClick} id={this.props.id}>   
      </div>
    )
  }  
}
class Color4 extends Component {  
  
  onClick = (item) =>{
       if(this.props.clickable) {        
      store.dispatch( updateInputs(3) );
    }
  };
  
  render() { 
    var item = this.props.item
    return(
      <div className={this.props.name} onClick={this.onClick} id={this.props.id}>   
      </div>
    )
  }  
}

ReactDOM.render(<App />, document.getElementById("container"));

              
            
!
999px

Console