cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - 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.

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

            
              <link href='https://fonts.googleapis.com/css?family=Arvo:400,700' rel='stylesheet' type='text/css'>
<div class="container">
  <div class="how-to-play">
    <h1>How to Play</h1>
    <p>Sumon is a math/addition game-- click the tiles to add up to the number displayed in the upper right.</p>
    <button id="got-it">OK, got it!</button>
  </div>
  <div class="board">
  </div>
  <div class="right">
    <span class="sum-display"></span>
    <span class="score-display"></span>
    <button id="restart">Start Over</button>
    <a href="#!" class="how-to-play">How to Play</a>
  </div>
</div>

<small>Made with love by <a href="https://twitter.com/gracenut96">@gracenut96</a></small>
            
          
!
            
              $main-color: #5dbc8e; // minty green
$neutral-color: #dfdfdf; // light gray
$secondary-color: #9EC7B3; // lighter minty
$darker: #303C36;

body {
  font-family: "Arvo";
}

small {
  display: block;
  width: 700px;
  margin: 10px auto;
  text-align: center;
  color: $secondary-color;
}

a {
  color: $secondary-color;
  &:hover {
    color: $main-color;
  }
}

span {
  display: inline-block;
  width: 130px;
  text-align: center;
  border-radius: 2px;
}

button {
  display: inline-block;
  width: 140px;
  height: 40px;
  margin-top: 10px;
  padding: 10px;
  text-align: center;
  font-family: "Arvo";
  font-weight: 400;
  font-size: 120%;
  color: #fff;
  border: none;
  outline: none;
  border-radius: 2px;
  background-color: $secondary-color;
  &:hover {
    background-color: $main-color;
    cursor: pointer;
  }
}

.container {
  width: 700px;
  height: 540px;
  margin: 0 auto;
  padding: 20px;
  background-color: $neutral-color;
  border-radius: 2px;
}

.right {
  width: 150px;
  height: 528px;
  float: right;
  text-align: center;
}

.sum-display {
  height: 70px;
  padding: 5px;
  font-weight: 700;
  font-size: 3.5em;
  color: #fff;
  background-color: $darker;
}

.score-display {
  height: 40px;
  margin-top: 10px;
  padding: 15px 5px 0 5px;
  background-color: #fff;
  color: $darker;
  &::before {
    content: "Score: ";
    font-weight: 700;
  }
}

.board {
  width: 528px;
  height: 528px;
  float: left;
  padding: 5px;
  background-color: $main-color;
  border-radius: 2px;
  &:hover {
    cursor: pointer;
  }
}

.board div {
  display: block;
  height: 40px;
  width: 40px;
  float: left;
  margin: 2px;
  padding: 15px 10px 5px 10px;
  text-align: center;
  font-size: 150%;
  font-weight: 700;
  color: $main-color;
  border: 1px solid $main-color;
  border-radius: 2px;
  background-color: #fff;
  &:hover {
    background-color: $neutral-color;
  }
}

.board .selected {
  background-color: $darker;
  &:hover {
    background-color: $darker;
  }
}

.board .dead {
  background-color: $secondary-color;
  &:hover {
    cursor: default;
    background-color: $secondary-color;
  }
}

div.how-to-play {
  display: none;
  position: absolute;
  width: 700px;
  height: 540px;
  margin: 0 auto;
  padding: 10px;
  background-color: rgba(255, 255, 255, 0.8);
  color: $darker;
  z-index: 2;
  &.displayed {
    display: block;
  }
  p {
    width: 60%;
  }
}

a.how-to-play {
  display: block;
  margin-top: 10px;
}

            
          
!
            
              var $tCount = 64;
var $totalVal = 316;
var $level = 1;
var $score = 0;
var $targetVal = 0;

var trackTotalVal = function(val) {
  $totalVal -= val;
  return $totalVal;
};

// gameboard setup
var $tiles = function() {
  return [ 1, 1, 1, 1, 1, 1, 1, 1,
                2, 2, 2, 2, 2, 2, 2,
                3, 3, 3, 3, 3, 3, 3,
                4, 4, 4, 4, 4, 4, 4,
                5, 5, 5, 5, 5, 5, 5,
                6, 6, 6, 6, 6, 6, 6,
                7, 7, 7, 7, 7, 7, 7,
                8, 8, 8, 8, 8, 8, 8,
                9, 9, 9, 9, 9, 9, 9 ];
};

var $tilesShuffle = function(arr) {
  var $newArr = [];
  var $randomIndex;
  var $element;
  while (arr.length) {
    $randomIndex = Math.floor(Math.random() * arr.length);
    $element = arr.splice($randomIndex, 1);
    $newArr.push($element[0]);       
  }
  return $newArr;
};

var $makePieces = function(arr) {
  var $pieces = [];
  var $piece;
  while (arr.length) {
    $piece = '<div>' + arr.pop().toString() + '</div>' ;
    $pieces.push($piece);
  }
  return $pieces;
};

var $makeBoard = function() {
  var $gameTiles = $makePieces($tilesShuffle($tiles()));
  while ($gameTiles.length) {
    $('div.board').append($gameTiles.pop());
  }
};

var $clearBoard = function() {
  $('.board').empty();
};

// generates # for player to make
var $genSum = function(level) {
  var $maxVal = (level * 5) + 10;
  var $minVal = 10;
  if ($totalVal > $maxVal && $tCount > 10) {
    return Math.floor(Math.random()*($maxVal - $minVal + 1) + $minVal);
  }
  else if ($tCount <= 10 && $totalVal > $maxVal) {
    return $genSumFailsafe($maxVal);
  }
  else if ($totalVal <= $maxVal) {
    return $totalVal;
  }
};

// fixes the '$genSum generates # too big or not possible w/ available tiles' bug.
var $genSumFailsafe = function(max) {
  var $max = max;
  var $liveTiles = [];
  var $l = 0;
  $('.board div').not('.dead').each(function() { 
    $liveTiles.push(parseInt($(this).text())); 
  });
  $liveTiles.sort(function(a, b) {
    return b-a;
  });
  for (i = 0; i < $liveTiles.length; i++) {
    for (j = 1; j < $liveTiles.length; j++) {
      $l = $liveTiles[i] + $liveTiles[j];
      if ($l <= $max) {
        return $l;
      }
    }
  }
};

// displays expected # to player
var $displaySum = function(val) {
  $('.sum-display').text(val.toString());
};

// checks whether player exceeded or equaled the expected sum
var $checkSum = function(val, targetVal) {
  if (val === targetVal) {
    return "=";
  }
  else if (val > targetVal) {
    return ">";
  }
  else {
    return "<";
  }
};

// adds to and displays player's score
var $displayScore = function(val) {
  $score += val * 2;
  $('.score-display').text($score.toString());
};

// set up playing board
var $setupBoard = function() {
  $clearBoard();
  $makeBoard();
  $tCount = 64;
  $totalVal = 316;
  $targetVal = $genSum($level);
  $displaySum($targetVal);
  $displayScore(0);
};

// start game
var $initGame = function() {
  $level = 1; // game initiates @ level one, score 0
  $score = 0;
  $setupBoard();
};

$(function() {
  var $selectedTotal = 0;
  var $r; // variable to hold value of checkSum call
  var $t = 0; // variable for tracking # of live tiles
  var $this;
  $initGame();
  $(document).on('click', '.board div', function() { // activates when player clicks piece
    $this = $(this);
    if (!($this.hasClass('dead'))) {
      $this.toggleClass('selected');
      if ($this.hasClass('selected')) {
        $selectedTotal += parseInt($this.text());
        $r = $checkSum($selectedTotal, $targetVal);
        $t++;
        if ($r === "=") {
          $('.selected').empty().addClass('dead').removeClass('selected');
          $displayScore($selectedTotal);
          // tracking available tiles & pts left
          $tCount -= $t; // subtracts # of used tiles from $tCount
          $totalVal -= $selectedTotal;
          // reset and init for next move
          $t = 0;
          $selectedTotal = 0;
          // check to see whether player levels up
          if ($tCount === 0) {
            $setupBoard();
          }
          else {
            $targetVal = $genSum($level);
            $displaySum($targetVal);
          }
        }
        else if ($r === ">") {
          $('.selected').removeClass('selected');
          $selectedTotal = 0;
          $t = 0;
        }
      }
      else {
        $selectedTotal -= parseInt($this.html());
        $tCount++;
      }
    }
  });
  $('#restart').click(function() {
    $initGame();
  });
  $('a.how-to-play').click(function() {
    $('div.how-to-play').addClass('displayed');
  });
  $('#got-it').click(function() {
    $('.how-to-play').removeClass('displayed');
  });
});

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

Console