patternjavascriptModerate
2048 (game) solver
Viewed 0 times
solvergame2048
Problem
The code contains an AI function, that searches breadth-first all possible moves and get the score of their grids, a depth of 6 is used which makes at most \$4+4^2+...+4^6\$ computed grids. The highest scores (and their associated moves) are kept for each level of depth, the most redundant one is chosen.
This function uses a 4x4 grid, and other functions to make it move in the 4 directions, so it can run in the background and return the final grid (when no more moves are possible). You also see a visualization of it as an HTML table, whose cells are in the
Some remarks on the code either on the form, or on the design of the game solver would be great.
border: 1px solid black;
}
td {
width: 35px;
height: 35px;
text-align: center;
}
initrun AIhint`
jsFiddle
edit: final version
This function uses a 4x4 grid, and other functions to make it move in the 4 directions, so it can run in the background and return the final grid (when no more moves are possible). You also see a visualization of it as an HTML table, whose cells are in the
cells variable and updated after each turn. You can also play directly with no solver, with keyboard's arrow keys.Some remarks on the code either on the form, or on the design of the game solver would be great.
var n= 4; // grid size
var demoAI = new AI({maxDepth:6, minDepth:3, monoWeight:20, smoothWeight:0, freeWeight:40, maxWeight:60})
function AI(o){
this.init=function(){
this.grid = [];
for(var i=0;i=o.minDepth; i--){ // avoid votes of low depth
votes[best[i]]++;
votedBest.push(maxIndex(votes))
}
//pick the latest non-null winner
for (var i=votedBest.length-1; i>=0; i--)
if (votedBest[i]!=null)
return votedBest[i]
return null;
}
this.run=function(){
var p = this.predict();
if (p!=null) {
this.move(p);
this.steps++;
this.run()
}
}
this.test=function(){//run 10 times and average
var s=0;
for (var i=0; imax.score)
max = node;
return max;
}
function mv(k, grid){
var tgrid = transform(ig, k, grid);
for (var i=0;i
table, th, td {border: 1px solid black;
}
td {
width: 35px;
height: 35px;
text-align: center;
}
initrun AIhint`
jsFiddle
edit: final version
Solution
First, you are usually good about naming your variables, but you still have a few single-letter variables in here,
Second, your code would be a bit more readable if you put spaces around your operators, like
Third, I think your algorithm can be improved. The very highest number that can be created is 2^18, which can only happen when you have 2^17 in one of the corner squares, and 2^16, 2^15, and 2^14 along one of the edges. Then, you need to 2^13 above 2^14, 2^12 next to 2^13, and so on:
Given the nature of the game, the odds of this happening are extremely low, but your algorithm does not seem to weight the squares to follow this pattern.
Finally, once the board gets filled when playing with the keyboard, nothing happens. When you ask for a hint, it returns "undefined". Perhaps you should implement win/loss situations to cover this scenario.
o, p, and v, to name a few. These should be more descriptive of what they are.Second, your code would be a bit more readable if you put spaces around your operators, like
for(var i = 0; i < n; i++) {. JSLint says you should do this too.Third, I think your algorithm can be improved. The very highest number that can be created is 2^18, which can only happen when you have 2^17 in one of the corner squares, and 2^16, 2^15, and 2^14 along one of the edges. Then, you need to 2^13 above 2^14, 2^12 next to 2^13, and so on:
Given the nature of the game, the odds of this happening are extremely low, but your algorithm does not seem to weight the squares to follow this pattern.
Finally, once the board gets filled when playing with the keyboard, nothing happens. When you ask for a hint, it returns "undefined". Perhaps you should implement win/loss situations to cover this scenario.
Context
StackExchange Code Review Q#80468, answer score: 11
Revisions (0)
No revisions yet.