Skip to content

Instantly share code, notes, and snippets.

@atomer
Created September 12, 2011 14:39
Show Gist options
  • Save atomer/1211423 to your computer and use it in GitHub Desktop.
Save atomer/1211423 to your computer and use it in GitHub Desktop.
GDD2011 DevQuiz スライドパズル(没)
function Board(w, h, data) {
var info = Board.create(w, h, data);
this._size = [h, w];
this._indexSize = [h - 1, w - 1];
this._board = info.board;
this._point = info.point;
this._answer = this._getAnswer(data);
this._counter = {
"up": 0,
"down": 0,
"left": 0,
"right": 0
};
}
Board.prototype = {
check: function() {
var result = [];
for (var i = 0, len = this._board.length; i < len; i++) {
result = result.concat(this._board[i]);
}
return result.join("") === this._answer.join("");
},
up: function() {
var i = this._point[0] - 1;
console.log("down");
if (this._control(i, this._point[1])) {
this._counter.up++;
return true;
} else {
return false;
}
},
down: function() {
var i = this._point[0] + 1;
console.log("down");
if (this._control(i, this._point[1])) {
this._counter.down++;
return true;
} else {
return false;
}
},
left: function() {
var j = this._point[1] - 1;
console.log("left");
if (this._control(this._point[0], j)) {
this._counter.left++;
return true;
} else {
return false;
}
},
right: function() {
var j = this._point[1] + 1;
console.log("right");
if (this._control(this._point[0], j)) {
this._counter.right++;
return true;
} else {
return false;
}
},
move: function(i, j) {
var x, y, c, cnt = 0;
this._cache = {
board: this._board,
point: this._point,
counter: this._counter
};
while (this._point.join("") !== i + "" + j) {
y = i - this._point[0];
x = j - this._point[1];
if (y !== 0) {
c = y < 0 ? "up" : "down";
y = Math.abs(y);
ym: for(;y--;) {
if (!this[c]()) {
break ym;
}
}
}
if (x !== 0) {
c = x < 0 ? "left" : "right";
x = Math.abs(x);
xm: for(;x--;) {
if (!this[c]()) {
break xm;
}
}
}
cnt++;
}
if (cnt > 9999) {
return false;
} else {
return true;
}
},
_control: function(i, j) {
if (i < 0 || i > this._indexSize[0] || j < 0 || j > this._indexSize[1]) {
this._error("out of index");
return false;
} else if (this._board[i][j] === "=") {
this._error("wall");
return false;
}
this._update(i, j);
return true;
},
_update: function(i, j) {
var tmp;
tmp = this._board[i][j];
this._board[i][j] = this.point();
this.point(tmp);
this.point(i, j);
},
_getAnswer: function(data) {
var tmp, i, j, ilen, len = data.length;
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
if (data[i] !== "=" && Board.VALUE[data[i]] > Board.VALUE[data[j]]) {
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
}
return data;
},
point: function(i, j) {
if (!arguments.length) {
return this._board[this._point[0]][this._point[1]];
} else if (typeof i === "string") {
this._board[this._point[0]][this._point[1]] = i;
return i;
} else {
this._point = [i, j];
return this._point;
}
},
show: function() {
console.log(this._board);
},
_error: function(s) {
console.error("[Error]" + s);
}
};
Board.create = function(w, h, data) {
var board = [],
cnt = 0,
point = [];
for (var i = 0; i < h; i++) {
board[i] = [];
for (var j = 0; j < w; j++) {
board[i][j] = data[cnt];
if (data[cnt] === "0") {
point = [i, j];
}
cnt++;
}
}
return {
board: board,
point: point
};
};
Board.VALUE = (function() {
var i, len,
alpha = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
value = {
"0": 9999
};
for (i = 1; i < 10; i++) {
value[i] = i;
}
for (i = 0, len = alpha.length; i < len; i++) {
value[alpha[i]] = i + 10;
}
return value;
})();
exports.Board = Board;
var Board = require("./Board").Board,
EFFECTIVE_VALUE = ["1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z"];
function parseSource(source) {
var datas = source.split(",");
return {
w: parseInt(datas[0]),
h: parseInt(datas[1]),
data: datas[2].split("")
};
}
var analytics = {
init: function(source) {
var datas = parseSource(source);
this.datas = datas;
this.board = new Board(datas.w, datas.h, datas.data);
this.answerBoard = Board.create(datas.w, datas.h, this.board._answer).board;
this.board.show();
this.loop();
},
getMinValue: function() {
var board = this.board._board,
i, j, ilen, jlen, key, value = 9999;
for (i = 0, ilen = board.length; i < ilen; i++) {
for (j = 0, jlen = board[i].length; j < jlen; j++) {
if (board[i][j] !== "=" && board[i][j] !== "0" && Board.VALUE[board[i][j]] < value) {
value = Board.VALUE[board[i][j]];
key = board[i][j];
}
}
}
return key;
},
getMaxValue: function() {
var board = this.board._board,
i, j, ilen, jlen, key, value = 0;
for (i = 0, ilen = board.length; i < ilen; i++) {
for (j = 0, jlen = board[i].length; j < jlen; j++) {
if (board[i][j] !== "=" && board[i][j] !== "0" && Board.VALUE[board[i][j]] > value) {
value = Board.VALUE[board[i][j]];
key = board[i][j];
}
}
}
return key;
},
getTargetPoint: function(value, board) {
var i, j, ilen, jlen;
board = board || this.board._board;
for (i = 0, ilen = board.length; i < ilen; i++) {
for (j = 0, jlen = board[i].length; j < jlen; j++) {
if (board[i][j] === value) {
return [i, j];
}
}
}
return false;
},
getTargetNext: function(value) {
},
loop: function() {
var target, coord, point, next, move, res;
for (var i = 0, len = EFFECTIVE_VALUE.length; i < len; i++) {
target = EFFECTIVE_VALUE[i];
coord = this.getTargetPoint(target);
if (coord) {
point = this.getTargetPoint(target, this.answerBoard);
while (coord.join("") !== point.join("")) {
this.move(coord, point);
coord = this.getTargetPoint(target);
if (target === "5") {
this.board.show();
//return;
}
}
this.answerBoard[point[0]][point[1]] = true;
}
}
},
move: function(coord, point) {
var move, res;
move = point[0] - coord[0];
if (coord[0] !== point[0]) {
move = point[0] - coord[0];
if (move > 0) {
res = this.board.move(point[0] + 1, point[1]);
this.board.top();
} else {
res = this.board.move(coord[0] - 1, coord[1]);
this.board.down();
}
}
if (coord[1] !== point[1]) {
move = point[1] - coord[1];
if (move > 0) {
res = this.board.move(point[0], point[1] + 1);
this.board.left();
} else {
res = this.board.move(coord[0], coord[1] - 1);
this.board.right();
}
}
}
};
analytics.init("5,6,12=E4D9HIF8=GN576LOABMTPKQSR0J");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment