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

              
                <html>
<head>
  <meta charset="utf-8">
  <title>Sliding Puzzle</title>
  <style>
  .picture {
    border: 1px solid black;
  }
  </style>
</head>
<body>
  <div id="title">
    <h2>Sliding Puzzle!</h2>
  </div>
  <div id="main" class="main">
    <canvas id="puzzle" width="400px" height="400px"></canvas>
  </div>
  <div id="score" style="border:1px solid #000; display:inline-block;">
    Score: 0
  </div>
    <div id="preview" style="border:1px solid #000; display:inline-block;">Image Preview
    <img src="http://horrornews.net/wp-   content/uploads/2011/11/cthulhu_christmas_calendar_icon-400x400.jpg"  width="150px" height="150px">    
    </div>
  <script src="sliding.js"></script>
</body>
</html>
              
            
!

CSS

              
                #puzzle {
  width: 400px;
  height: 400px;
  border: 1px solid gray;
  background-color: lightgrey; }
  #puzzle #windiv {
    display: none; }
  #puzzle .banner {
    width: 370px;
    font-size: 50px;
    background-color: #f5f5dc;
    position: relative;
    text-align: center;
    top: -60px;
    box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.55);
    left: -15px;
    z-index: 2; }
  #puzzle .innerSquare {
    width: 100px;
    height: 100px;
    float: left; }
    #puzzle .innerSquare.imageSquare {
      font-size: 24px;
      text-align: center;
      border: 1px outset  black; }
      #puzzle .innerSquare.imageSquare:hover {
        background-color: lightgray; }
    #puzzle .innerSquare.clickable:hover {
      opacity: 0.4;
      filter: alpha(opacity=40); }
    #puzzle .innerSquare.blank {
      border: 1px inset black; }

#score {
  width: 170px;
  height: 170px;
  background-color: lightgrey;
  overflow: scroll;
  padding-left: 30px;
  margin-top: 10px; }
  #score .mini {
    width: 90px;
    height: 90px;
    margin-top: 5px;
    margin-right: 5px; }

#preview {
  width: 170px;
  height: 170px;
  background-color: lightgrey;
  overflow: scroll;
  padding-left: 30px;
  margin-top: 10px; }
  #preview .mini {
    width: 90px;
    height: 90px;
    margin-top: 5px;
    margin-right: 5px; }
              
            
!

JS

              
                var context = document.getElementById('puzzle').getContext('2d');

var img = new Image();
img.src = 'http://horrornews.net/wp-content/uploads/2011/11/cthulhu_christmas_calendar_icon-400x400.jpg';
img.addEventListener('load', drawTiles, false);

var boardSize = document.getElementById('puzzle').width;
var tileCount = 4;
var tileSize = boardSize / tileCount;

// Track coordinates of user click
var clickLoc = new Object;
clickLoc.x = 0;
clickLoc.y = 0;

// Track coordinates of empty tile
var emptyLoc = new Object;
emptyLoc.x = 0;
emptyLoc.y = 0;

var solved = false;

var boardParts = new Object;
setBoard();

// Track which tile user clicks
document.getElementById('puzzle').onclick = function(e) {
  clickLoc.x = Math.floor((e.pageX - this.offsetLeft) / tileSize);
  clickLoc.y = Math.floor((e.pageY - this.offsetTop) / tileSize);
  if (distance(clickLoc.x, clickLoc.y, emptyLoc.x, emptyLoc.y) == 1) {
    slideTile(emptyLoc, clickLoc);
    drawTiles();
  }
  if (solved) {
    setTimeout(function() {alert("Woohoo!");}, 500);
  }
};

// Define and initialize the tile board
function setBoard() {
  boardParts = new Array(tileCount);
  for (var i = 0; i < tileCount; ++i) {
    boardParts[i] = new Array(tileCount);
    for (var j = 0; j < tileCount; ++j) {
      boardParts[i][j] = new Object;
      boardParts[i][j].x = (tileCount - 1) - i;
      boardParts[i][j].y = (tileCount - 1) - j;
    }
  }
  emptyLoc.x = boardParts[tileCount - 1][tileCount - 1].x;
  emptyLoc.y = boardParts[tileCount - 1][tileCount - 1].y;
  solved = false;
}

// Redraw the board with the clicked tile in its new position.
function drawTiles() {
  context.clearRect ( 0 , 0 , boardSize , boardSize );
  for (var i = 0; i < tileCount; ++i) {
    for (var j = 0; j < tileCount; ++j) {
      var x = boardParts[i][j].x;
      var y = boardParts[i][j].y;
      if(i != emptyLoc.x || j != emptyLoc.y || solved == true) {
        context.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize,
            i * tileSize, j * tileSize, tileSize, tileSize);
      }
    }
  }
}

// Called in getElementByID, determines whether tile is adjacent to the open square. True if difference of the x-coordinates of the clicked tile and the empty tile plus the difference of the y-coordinates of the clicked tile and the empty tile is 1
function distance(x1, y1, x2, y2) {
  return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}

// Slide tile into new position. Copy the tile coordinates for that board square into the empty square. Then copy the tile coordinates for the removed tile into the clicked tile.
function slideTile(toLoc, fromLoc) {
  if (!solved) {
    boardParts[toLoc.x][toLoc.y].x = boardParts[fromLoc.x][fromLoc.y].x;
    boardParts[toLoc.x][toLoc.y].y = boardParts[fromLoc.x][fromLoc.y].y;
    boardParts[fromLoc.x][fromLoc.y].x = tileCount - 1;
    boardParts[fromLoc.x][fromLoc.y].y = tileCount - 1;
    toLoc.x = fromLoc.x;
    toLoc.y = fromLoc.y;
    checkSolved();
  }
}

// After a tile is moved, check if tiles are in the correct squares.
function checkSolved() {
  var flag = true;
  for (var i = 0; i < tileCount; ++i) {
    for (var j = 0; j < tileCount; ++j) {
      if (boardParts[i][j].x != i || boardParts[i][j].y != j) {
        flag = false;
      }
    }
  }
  solved = flag;
}
              
            
!
999px

Console