123

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.

            
              <h1 class="text-center">Simon Game</h1>
<div class="container text-center">
  <h3 id="round">Round: </h3>
  <div class="circle">
    <div class="tile" id="green"></div>
    <div class="tile" id="red"></div>
    <div class="tile" id="yellow"></div>
    <div class="tile" id="blue"></div>
  </div>
  <a class="btn strict btn-default">Strict: On</a>
  <a class="btn start btn-default">Start</a>
  <a class="btn reset btn-default" onclick="reset()">Reset</a>

  <div style="position: relative">
    <p class="makenr text-ceter" style="position: fixed; z-index: -1;bottom: 0; width:100%; text-align: bottom;">Made with
      <i class="fa fa-heart" style="color: #cc0000"></i> by
      <a target="_blank" href="https://github.com/Teo03">Teo
                </a>
    </p>
  </div>

  <!-- container !-->
</div>
            
          
!
            
              html * {
  text-align: center;
  background-color: #d3d3d3;
}

h1 {
  font-size: 60px;
  padding-bottom: 10px;
}

h3 {
  padding-bottom: 30px;
}

.circle {
  background: #d3d3d3;
  width: 320px;
  height: 320px;
  border-radius: 50%;
  border: 10px solid gray;
  margin: auto;
}

.tile {
  height: 150px;
  width: 150px;
  float: left;
  cursor: pointer;
}

#red {
  background-color: red;
  border-top-right-radius: 100%;
}

#green {
  background-color: green;
  border-top-left-radius: 100%;
}

#blue {
  background-color: blue;
  border-bottom-right-radius: 100%;
}

#yellow {
  background-color: yellow;
  border-bottom-left-radius: 100%;
}

btn {
  padding-top: 40px;
}

.start,
.reset,
.strict {
  margin-top: 20px;
  padding: 10px;
}

            
          
!
            
              $(document).ready(function() {
  var greenBtn = document.getElementById("green");
  var redBtn = document.getElementById("red");
  var yellowBtn = document.getElementById("yellow");
  var blueBtn = document.getElementById("blue");
  var tile = document.getElementsByClassName("tile");
  //audio for every click
  var audio = [
    new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"),
    new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"),
    new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"),
    new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3")
  ];
  // record the round
  var round = 0;
  // record the random presses
  var compSeq = [];
  //count how many moves user does
  var playerIndex = 0;
  //check if game is running
  var running = false;
  //strict mode
  var strict = true;

  var green = 1;
  var red = 2;
  var blue = 3;
  var yellow = 4;

  $(".reset, #round").hide();

  //start
  $(".start").click(function() {
    running = true;
    round++;
    $("#round")
      .css("color", "#333")
      .html("Round: " + round)
      .show();
    newSeq();
    $(".start, .strict").hide();
  });

  function newSeq() {
    var addRandom = Math.floor(Math.random() * 4) + 1;
    compSeq.push(addRandom);
    compBlink();
    console.log("compSeq: " + compSeq);
  }
  //blink a tile
  function blink(tile) {
    switch (tile) {
      case 1:
        $(greenBtn).fadeOut(350, function() {
          audio[0].play();
          $(this).fadeIn(350, function() {
            return;
          });
        });
        break;

      case 2:
        $(redBtn).fadeOut(350, function() {
          audio[1].play();
          $(this).fadeIn(350, function() {
            return;
          });
        });
        break;

      case 3:
        $(blueBtn).fadeOut(350, function() {
          audio[2].play();
          $(this).fadeIn(350, function() {
            return;
          });
        });
        break;

      case 4:
        $(yellowBtn).fadeOut(350, function() {
          audio[3].play();
          $(this).fadeIn(350, function() {
            return;
          });
        });
        break;

      default:
        console.log("Something went wrong.");
    }
    //end
  }

  $(".strict").click(function() {
    if (!strict) {
      strict = true;
      $(".strict")
        .html("Strict: On")
        .css("background-color", "#3de816");
    } else {
      strict = false;
      $(".strict")
        .html("Strict: Off")
        .css("background-color", "#e86017");
    }
    console.log(strict);
  });

  function compBlink() {
    if (compSeq.length === 1) {
      blink(compSeq[0]);
    } else {
      //declare i after every iteration after 700 ms
      for (let i = 0; i < compSeq.length; i++) {
        setTimeout(function displayAfterDelay() {
          blink(compSeq[i]);
        }, i * 700);
      }
    }
  }

  //user input
  $(".tile").bind("click", function() {
    //only allow inputs if there is a random sequence
    if (compSeq.length >= 1) {
      if ($(this).is("#green")) {
        checkUserClicks(green);
        audio[0].play();
        blink(1);
      } else if ($(this).is("#red")) {
        checkUserClicks(red);
        audio[1].play();
        blink(2);
      } else if ($(this).is("#blue")) {
        checkUserClicks(blue);
        audio[2].play();
        blink(3);
      } else if ($(this).is("#yellow")) {
        checkUserClicks(yellow);
        audio[3].play();
        blink(4);
      }
    }
  });

  function checkUserClicks(color) {
    console.log("added: " + color);

    //if the player has inputted the first value right :
    if (color === compSeq[playerIndex]) {
      if (round >= 18) {
        $("#round")
          .html("Round: " + round)
          .css("color", "#990000");
      }
      //if the player inputted the full sequence of ONE round then new round starts
      if (playerIndex === compSeq.length - 1) {
        //round 20 is the end

        if (round === 20) {
          $("#round")
            .html("That was the last round, you win!!")
            .css("color", "#007300");
          reset();
        }
        console.log("full seq completed, starting new round");

        //don't show 'next' button at the 20 round
        if (round <= 19 && running) {
          $(".start")
            .html("Next")
            .show();
          playerIndex = 0;
        }
      } else {
        //if he is still inputting we need to check the next value by increasing his index(where he is now)
        playerIndex++;
      }
    } else {
      if (strict) {
        playerIndex = 0;
        compBlink();
      } else {
        running = false;
        $("#round")
          .html("Game Over. Try Again?")
          .css("color", "#cc0000");
        reset();
        //if the number he inputted is wrong game resets
      }
    }
  }

  function reset() {
    $(".reset").show();
    if (round === 20) {
      $(".reset").html("Play Again?");
    } else {
      $(".reset").html("Reset");
    }
  }
  $(".reset").click(function() {
    playerIndex = 0;
    compSeq = [];
    round = 0;
    $("#round").hide();
    $(".start")
      .html("Start")
      .show();
    $(".strict").show();
    $(".reset").hide();
  });
  //document.ready().
});

            
          
!
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.

Console