Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2048 game SCORING algorith question. So there is this game I made for my class c

ID: 3708341 • Letter: 2

Question

2048 game SCORING algorith question. So there is this game I made for my class called 2048 (there are a bunch of free versions on the app store) where you have a 4x4 grid of randomly generated blocks and you swipe left/right/up/down to move ALL of the blocks in that direction. Everytime you swipe, a new 2 block appears. If 2 blocks collide into eachother, and they have the same number, then they combine and form a single tile with the value 2x the original of each tile. Here is a video: https://www.youtube.com/watch?v=jO6vclv7eGI)

I need help creating the scoring part. Everytime 2 tiles "merge" the new value is added to your score. EX ([8] [8] [4] [4]) Score: 0............ <-----............([16] [8] [ ] [ ]) Score: 24. How i'm doing it is making each new tile's value += to an updated_score = 0 (AFTER A MERGE HAPPENS). Then in the end i use a double for loop (row/column) to analyze each tile's updated_score value in order to create the score. I have no idea how to do it so that it matches the score on my phone though.

WHAT I THOUGHT WAS HOW IT IS DONE:

[t32, t8 , t256, t4 ]
[ t8 , t2, t128, t16]
[t16, t64, t8 , t4 ]
[ t2 , t8 , t32 , t2 ]

I was expecting a score of 1116 since I thought

(since I thought the actual scoring looked like this):


t4 = 4
t8 = 4+8
t16 = 4+8+16
t32 = 4+8+16+32
t64 = 4+8+16+32+64
etc...
(take the sums of the grid's tile's)

However, on my phone it says my score is 3192... Please help.

Explanation / Answer

(function () {

var __restart = GameManager.prototype.restart;

var GM;

GameManager.prototype.restart = function () {

GM = this;

__restart.bind(this)();

var strategy = new Strategy(GM);

console.log('hi');

setTimeout(function () {

strategy.play();

}, 0);

};

function Strategy(gm) {

this.gm = gm;

this.UP = 0;

this.RIGHT = 1;

this.DOWN = 2;

this.LEFT = 3;

this.delay = window.DELAY || 100;

}

Strategy.prototype.cloneGrid = function () {

function cloneTile(tile) {

var t = new Tile({x: tile.x, y: tile.y}, tile.value);

t.previousPosition = tile.previousPosition;

t.mergedFrom = tile.mergedFrom;

return t;

}

var arr = [], i, j, len, len2;

for (i = 0, len = this.gm.grid.size; i < len; i++) {

arr[i] = this.gm.grid.cells[i].slice();

for (j = 0, len2 = arr[i].length; j < len2; j++) {

if (arr[i][j] !== null) {

arr[i][j] = cloneTile(arr[i][j]);

}

}

}

var g = new Grid(this.gm.grid.size);

g.cells = arr;

return g;

};

Strategy.prototype.freeze = function () {

this._grid = this.gm.grid;

this._act = this.gm.actuator;

this._score = this.gm.score;

this._over = this.gm.over;

this._won = this.gm.won;

this.gm.actuator = undefined;

this.gm.grid = this.cloneGrid();

};

Strategy.prototype.restore = function () {

this.gm.grid = this._grid;

this.gm.actuator = this._act;

this.gm.score = this._score;

this.gm.won = this._won;

this.gm.over = this._over;

};

Strategy.prototype.isMovePossible = function(i) {

var movePossible = false;

this.freeze();

try {

this.gm.move(i);

} catch (e) {

movePossible = true;

}

this.restore();

return movePossible;

}

Strategy.prototype.simulateMove = function(i) {

var score = -1;

this.freeze();

try {

this.gm.move(i);

} catch (e) {

score = this.gm.score;

}

this.restore();

console.log(score)

return score

}

Strategy.prototype.doMainMovement = function doMainMovement() {

// Main movement: goal is to stack to the down-right of the board

var blocked = false,

self = this;

while (! blocked) {

debugger

// Choose the best move between right and down, if possible

if (this.isMovePossible(this.RIGHT) && this.isMovePossible(this.LEFT)) {

if (this.simulateMove(this.DOWN) > this.simulateMove(this.RIGHT)) {

console.log('Moving down');

this.gm.move(this.DOWN);

} else { // redondant condition but quicker to code

console.log('Moving right');

this.gm.move(this.RIGHT);

}

} else if (this.isMovePossible(this.RIGHT)) {

console.log('Moving right');

this.gm.move(this.RIGHT);

} else if (this.isMovePossible(this.DOWN)) {

console.log('Moving down');

this.gm.move(this.DOWN);

} else {

blocked = true;

}

}

window.requestAnimationFrame(function () {

if (blocked) {

setTimeout(function(){self.doUnblock()}, self.delay);

} else {

setTimeout(function(){self.doMainMovement()}, self.delay);

}

});

};

Strategy.prototype.doUnblock = function() {

var self = this;

// When main movement is impossible: unblock

if (this.isMovePossible(this.UP)) {

console.log('Moving up');

this.gm.move(this.UP);

}

while (true) {

if (this.isMovePossible(this.RIGHT)) {

console.log('Moving right');

this.gm.move(this.RIGHT);

} else {

break;

}

}

if (this.isMovePossible(this.DOWN)) {

console.log('Moving down');

this.gm.move(this.DOWN);

}

var i, ascending = true, line = this.gm.grid.cells[0];

// Test if first line is correctly ascending, if not we want to redo the "unblock" part

for (i = 0; i < this.gm.grid.cells[0].length - 1; i++) {

// Improve to test even with empty values

if (line[i] && line[i + 1] && line[i].value > line[i + 1].value) {

ascending = false;

break;

}

}

window.requestAnimationFrame(function () {

if (ascending) {

setTimeout(function(){self.doMainMovement()}, self.delay);

} else {

setTimeout(function(){self.doUnblock()}, self.delay);

}

});

}

Strategy.prototype.play = function () {

var self = this, next;

this.doMainMovement();

};

new GameManager(4, KeyboardInputManager, HTMLActuator);

})();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote