Skip to content

Instantly share code, notes, and snippets.

@cyrilis
Created March 16, 2014 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyrilis/9587366 to your computer and use it in GitHub Desktop.
Save cyrilis/9587366 to your computer and use it in GitHub Desktop.
2048 game hacker
/**
* Created by Cyril on 14-3-16.
*/
GameManager.prototype.maxDirection = function (cb) {
var self = this;
var tile;
var maxValue = 0;
var maxKey = null;
var availabeDirection = 0;
var maxDeleteValue = 0;
var maxDirectionValue = 0;
for (var direction = 0; direction < 4; direction++) {
var maxDirectionTemp = 0;
var maxDelete = 0;
for (var x = 0; x < this.size; x++) {
for (var y = 0; y < this.size; y++) {
tile = this.grid.cellContent({ x: x, y: y });
if (tile) {
var vector = a.getVector(direction);
var cell = { x: x + vector.x, y: y + vector.y };
var other = self.grid.cellContent(cell);
if (other && other.value === tile.value){
maxDelete ++;
availabeDirection = direction;
// console.log('Got Direction:'+ direction, "value:"+ other.value);
if(tile.value*2 > maxValue){
maxValue = tile.value * 2;
maxDirectionTemp += maxValue;
// console.log("====================="+maxValue);
}
}
}
}
}
if(maxDelete > maxDeleteValue){
maxKey = direction;
maxDeleteValue = maxDelete;
}
if(maxDirectionTemp> maxDirectionValue){
maxKey = direction;
maxDirectionValue = maxDirectionTemp;
}
}
console.log(maxDeleteValue, maxDirectionValue);
cb(typeof maxKey == 'number'? maxKey : Math.round(Math.random()*3));
// return maxKey;
};
var a = new GameManager(5, KeyboardInputManager, HTMLActuator, LocalScoreManager);
function move (){
if(!a.won&&!a.over){
a.maxDirection(function(e){
a.move(e);
});
window.setTimeout(move,10)
}else{
alert('Game end because: '+ (a.won ? "Won!": "Failed"));
}
}
move();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment