Skip to content

Instantly share code, notes, and snippets.

@rnaud
Created February 20, 2011 13:32
Show Gist options
  • Save rnaud/835967 to your computer and use it in GitHub Desktop.
Save rnaud/835967 to your computer and use it in GitHub Desktop.
Logic problem
var unshift = Array.prototype.unshift;
var splice = Array.prototype.splice;
var grid = [];
var used = [];
var new_grid = [];
grid.push([{type:"p", head : 1},{type:"c", head : 1},{type:"a", head : 2},{type:"o", head : 2}]);
grid.push([{type:"o", head : 1},{type:"a", head : 2},{type:"c", head : 2},{type:"p", head : 1}]);
grid.push([{type:"c", head : 1},{type:"p", head : 2},{type:"o", head : 2},{type:"a", head : 1}]);
grid.push([{type:"p", head : 1},{type:"c", head : 2},{type:"o", head : 2},{type:"a", head : 1}]);
grid.push([{type:"p", head : 2},{type:"c", head : 2},{type:"a", head : 1},{type:"c", head : 2}]);
grid.push([{type:"a", head : 1},{type:"p", head : 2},{type:"o", head : 2},{type:"p", head : 1}]);
grid.push([{type:"a", head : 1},{type:"o", head : 1},{type:"a", head : 2},{type:"c", head : 2}]);
grid.push([{type:"o", head : 1},{type:"a", head : 2},{type:"c", head : 2},{type:"p", head : 1}]);
grid.push([{type:"p", head : 1},{type:"c", head : 2},{type:"o", head : 2},{type:"a", head : 1}]);
Array.prototype.shuffle = function() {
var i = this.length;
while (--i) {
var j = Math.floor(Math.random() * (i + 1))
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this; // for convenience, in case we want a reference to the array
};
Array.prototype.rotate = (function() {
return function(count) {
var len = this.length;
unshift.apply(this, splice.call(this, count % len, len));
return this;
};
})();
function checkValidity(try_card, position, orientation) {
// since I'm adding from top left to bottom, I only need to check top and left
var valid = true;
// top
if (typeof new_grid[position-3] != "undefined") {
if (try_card[0].type != new_grid[position-3][2].type) valid = false;
if (try_card[0].head == new_grid[position-3][2].head) valid = false;
}
// left
if (typeof new_grid[position-1] != "undefined") {
if (try_card[3].type != new_grid[position-1][1].type) valid = false;
if (try_card[3].head == new_grid[position-1][1].head) valid = false;
}
return valid;
}
function addCard(whichPos) {
for (card = 0; card < 9; card++) {
if (!used[card]) {
for (orientation = 0; orientation < 4; orientation++) {
try_card = temp_grid[card].rotate(orientation);
if (checkValidity(try_card, whichPos, orientation)) {
used[card] = true;
new_grid.push(try_card);
if (whichPos < 8) {
addCard(whichPos + 1);
break;
} else {
console.log("I WON");
console.log(JSON.stringify(used));
console.log(new_grid.length);
console.log(JSON.stringify(new_grid));
}
// } else {
// if (orientation == 3 && card == 8) console.log("lost");
}
}
}
}
}
for (j = 0; j<100000000; j++) {
// randomize array first
var temp_grid = grid.shuffle();
// randomize rotations
for (i = 0; i<9; i++) {
var random = Math.floor(Math.random()*4);
temp_grid[i].rotate(random);
}
// reset new_grid
new_grid = [];
// reset used cards
used = [false, false, false,false, false, false,false, false, false];
//start try
addCard(0);
}
@rnaud
Copy link
Author

rnaud commented Feb 20, 2011

Et pour check les values rendues : https://gist.github.com/836019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment