Skip to content

Instantly share code, notes, and snippets.

@jwill
Created November 28, 2010 23:31
Show Gist options
  • Save jwill/719396 to your computer and use it in GitHub Desktop.
Save jwill/719396 to your computer and use it in GitHub Desktop.
(function(){
var GameBoard;
// GameBoard class for Match 3 game
// @author: jwill
GameBoard = function(x, y) {
var _a, _b, _c, _d, array, i, j;
// create grid
this.grid = new Array();
this.maxX = x;
this.maxY = y;
_a = 0; _b = x;
for (i = _a; (_a <= _b ? i < _b : i > _b); (_a <= _b ? i += 1 : i -= 1)) {
array = new Array();
_c = 0; _d = y;
for (j = _c; (_c <= _d ? j < _d : j > _d); (_c <= _d ? j += 1 : j -= 1)) {
array.push(new Cell(i, j));
}
this.grid.push(array);
}
// for use in checkMatches function
this.isChecking = false;
console.log(this);
return this;
};
GameBoard.prototype.findAdjacentCells = function(x, y) {
var _a, _b, _c, cell, cells, center, ll, lm, lr, ml, mr, uc, ul, ur;
if (this.masterCell === undefined) {
this.masterCell = this.grid[x][y];
this.masterCell.alreadyCheckedNeighbors = true;
}
center = (typeof center !== "undefined" && center !== null) ? center : this.grid[x][y];
if (!(center === undefined)) {
center.alreadyCheckedNeighbors = true;
}
console.log("Calling adjcells at x:" + x + " y:" + y);
// CCC Given X is the center point,
// CXC we need to find all the surrounding points
// CCC
ul = (typeof ul !== "undefined" && ul !== null) ? ul : this.grid[x - 1][y - 1];
uc = (typeof uc !== "undefined" && uc !== null) ? uc : this.grid[x][y - 1];
ur = (typeof ur !== "undefined" && ur !== null) ? ur : this.grid[x + 1][y - 1];
ml = (typeof ml !== "undefined" && ml !== null) ? ml : this.grid[x - 1][y];
mr = (typeof mr !== "undefined" && mr !== null) ? mr : this.grid[x + 1][y];
ll = (typeof ll !== "undefined" && ll !== null) ? ll : this.grid[x - 1][y + 1];
lm = (typeof lm !== "undefined" && lm !== null) ? lm : this.grid[x][y + 1];
lr = (typeof lr !== "undefined" && lr !== null) ? lr : this.grid[x + 1][y + 1];
cells = [ul, uc, ur, ml, mr, ll, lm, lr];
_b = cells;
for (_a = 0, _c = _b.length; _a < _c; _a++) {
cell = _b[_a];
if (((typeof cell !== "undefined" && cell !== null))) {
if (!(this.checkedPoints.contains(cell))) {
this.checkedPoints.add(cell);
}
if ((this.isValidCell(cell) && this.masterCell.compare(cell))) {
cell.alreadyCheckedNeighbors = true;
cell.matchesMasterCell = true;
this.allCells.add(cell);
if (!(cell.alreadyCheckedNeighbors === true)) {
this.findAdjacentCells(cell.x, cell.y);
}
}
}
}
return null;
};
GameBoard.prototype.isValidCell = function(cell) {
var validX, validY;
console.log(cell);
validX = (this.maxX > cell.x) && (cell.x >= 0);
validY = (this.maxY > cell.y) && (cell.y >= 0);
return validX && validY;
};
GameBoard.prototype.checkMatches = function(x, y) {
var _a, _b, _c, cell;
// if process is starting, clear arrays and set isChecking to true
if (!(this.isChecking === true)) {
this.allCells = new Set();
this.isChecking = true;
}
this.findAdjacentCells(x, y);
console.log(this.allCells);
_b = this.allCells;
for (_a = 0, _c = _b.length; _a < _c; _a++) {
cell = _b[_a];
// remove
console.log(cell.color);
//# todo figure this out
}
this.isChecking = false;
return resetGrid();
};
GameBoard.prototype.resetGrid = function() {
var _a, _b, _c, _d, _e, _f, cell, i, j;
_a = []; _b = 0; _c = this.maxX;
for (i = _b; (_b <= _c ? i < _c : i > _c); (_b <= _c ? i += 1 : i -= 1)) {
_a.push((function() {
_d = []; _e = 0; _f = this.maxY;
for (j = _e; (_e <= _f ? j < _f : j > _f); (_e <= _f ? j += 1 : j -= 1)) {
_d.push((function() {
if (this.grid[i][j] !== undefined) {
cell = this.grid[i][j];
return cell.reset();
}
}).call(this));
}
return _d;
}).call(this));
}
return _a;
};
GameBoard.prototype.drawBoard = function() {
var _a, _b, _c, _d, _e, _f, cell, i, j;
_a = []; _b = 0; _c = this.maxX;
for (i = _b; (_b <= _c ? i < _c : i > _c); (_b <= _c ? i += 1 : i -= 1)) {
_a.push((function() {
_d = []; _e = 0; _f = this.maxY;
for (j = _e; (_e <= _f ? j < _f : j > _f); (_e <= _f ? j += 1 : j -= 1)) {
_d.push((function() {
if (this.grid[i][j] !== undefined) {
cell = this.grid[i][j];
return cell.drawCell();
}
}).call(this));
}
return _d;
}).call(this));
}
return _a;
};
window.GameBoard = GameBoard;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment