0

This is my code.

var blank_left = 180;
var blank_top = 180;
var cardset = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
cardset.sort(function(){ return 0.5 - Math.random()});

$('#wrapper div').each(function(){
    $(this).css('left',$(this).attr('id')%4*60)
    .css('top',Math.floor($(this).attr('id')/4)*60)
    .html(cardset[$(this).attr('id')])
});

$('#wrapper div').click(function(){
    if(Math.abs(parseInt($(this).css('left'))-blank_left)+Math.abs(parseInt($(this).css('top'))-blank_top)==60){
        var old_left = blank_left;
        var old_top = blank_top;
        blank_left = parseInt($(this).css('left'));         
        blank_top = parseInt($(this).css('top'));
        $(this).animate({left: old_left, top: old_top}, 150);
    }
});

What it does is that it takes the array in cardset and makes them random then printing them to my divs in an random order. What I wanna do is when i move my divs I want the script to check if I have moved the divs in the right order. The right order being my cardset array. So it should check with the array cardset if all numbers are in the right order.

For example: if cardset = true then alert('Done!');

Hope you understand.

3
  • So you just want to check if all the numbers in the array are in the right order? And do the contents of the array ever change, or is it always 1 to 15? Commented Dec 19, 2014 at 15:02
  • @imdabestmanideedeet Yes! And the content is always 1 to 15. Commented Dec 19, 2014 at 15:05
  • My answer still stands, and it doesn't need the cardset array to check if the order is correct. Your question should be "how do I determine the order of my divs as they are on the screen right now". If you know the order, you can store their values in an array, and feed that array to my function. Good luck with your puzzle game! It looks nice already. Commented Dec 19, 2014 at 15:38

4 Answers 4

1

Use a for loop like this one. You can change the second array and compare it to the first and see what happens.

var cardset = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

var cardset2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

for (i = 0; i < cardset.length; i++) {
  if (cardset[i] !== cardset2[i]) {
    alert('No match.');
    break;
  }
}

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming the contents of your array are always numbers:

var checkArray = function(myArray) {
  for (var i = 0; i < myArray.length; i++) {
    if (i + 1 === myArray.length) {
      return true;
    }
    if (myArray[i] > myArray[i + 1]) {
      return false;
    }
  }
};

var myArray = [1, 2, 3, 4, 5, 6];
var myArray2 = [2, 1, 3, 4, 6, 5];

alert('myArray is in order: ' + checkArray(myArray));
alert('myArray2 is in order: ' + checkArray(myArray2));

Basically, just check if the next number in the array is a higher number than the current array index.

1 Comment

I like this one, it doesn't have to iterate over all the cells if it doesN't match.
0

I'd do this by iterating through the array, and returning false if n is greater than n+1. The trivial case is where the length is 1.

var checkOrder = function (a) {
  if (a.length === 1) return true;
  for (var i = 1; i < a.length; i++) {
    if (a[i] < a[i-1]) return false;
  }
  return true;
}

var in_order = [1,2,3,4];
var not_in_order = [1,3,2,4];

checkOrder(in_order); // true
checkOrder(not_in_order); // true

Comments

-1

Just join() the arrays and compare them as strings. This should work.

Html:

<div id="1" class="card"></div>
<div id="3" class="card"></div>
<div id="2" class="card"></div>
<div id="4" class="card"></div>

Javascript:

var cardset = [1,3,2,4];
var divs = document.querySelectorAll('.card');
var divNumbers = [];
for(var i = 0; i < divs.length; i++) {
  divNumbers.push(divs[i].id)
}

if (divNumbers.join() === cardset.join()) {
  console.log("right order!")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.