On the information you gave us, we are not able to answer you question properly, but however i might be able to help you go forward.
if you would make it 2x2 divs, give classes like this=
Every box would have box, then if box is orange, it would be orange, and if it is the empty one, it would have empty.
Then we would have id's. Maybe something like this:
left upper corner would be 11, right corner 12, second row 21 and 22.
Then when clicked, get that id, and check if div with $("#"+parseFloat($(this).id()) - 10).hasClass("empty"); (note that .id might not be true, but something like that with jQyery) So if that is true, the div above you is empty. Then you can do +1 to check next div in right, and -1 for div in the left, but in this case you need to check that you stay on the same row when checking left and right.
And then just play around with those classes you have. Give $(this).addClass("empty"); $(this).removeClass("orange") and give orange to the one you found and also remove empty from it.
Boom now you have a box game
jQuery(document).ready(function ($) {
$(".box").click(function () {
if ($(this).hasClass("empty")) {
return;
}
var thisId = parseFloat($(this).attr("id"));
console.log(thisId)
console.log($("#" + (thisId - 1)))
if ($("#" + (thisId - 10)).hasClass("empty")) {
$("#" + (thisId - 10)).addClass("orange");
$("#" + (thisId - 10)).removeClass("empty");
$(this).addClass("empty");
$(this).removeClass("orange");
} else if ($("#" + (thisId + 10)).hasClass("empty")) {
$("#" + (thisId + 10)).addClass("orange");
$("#" + (thisId + 10)).removeClass("empty");
$(this).addClass("empty");
$(this).removeClass("orange");
} else if ($("#" + (thisId - 1)).hasClass("empty")) {
$("#" + (thisId - 1)).addClass("orange");
$("#" + (thisId - 1)).removeClass("empty");
$(this).addClass("empty");
$(this).removeClass("orange");
} else if ($("#" + (thisId + 1)).hasClass("empty")) {
$("#" + (thisId + 1)).addClass("orange");
$("#" + (thisId + 1)).removeClass("empty");
$(this).addClass("empty");
$(this).removeClass("orange");
}
})
})
EDIT: here's jsFiddle https://jsfiddle.net/rL5ozLz4/