Skip to content

Commit 9f4d073

Browse files
committed
Fix behavioural difference from original game (cell overload++)
It is possible when resolving chain reactions that a cell will have atoms added to it by multiple neighbouring explosions before its own explosion is resolved. This means that a central cell could have more than 4 atoms in it (potentially up to 7!), a side cell could have more than 3 (up to 5), and a corner cell more than 2 (up to 3). Our implementation originally discarded any atoms over the explosion threshold, essentially causing us to lose matter. It was determined through use of the debug mode and comparing with the original game that the original does not do this. The fix here is to allow a cell to be overloaded further, and when resolving an explosion in a super-overloaded cell, leave behind any excess atoms in the exploding cell. Interestingly this reveals a display bug in the original game. Viewing the game state at ?debug=84 and comparing to https://youtu.be/kEDjZyG2g-g?t=1m47s the original game is failing to display the green atom present at (9, 4) after that cell has exploded. This is however resolved by the next turn when 1 red atom gets added to the cell, turning it into a 2-atom red cell (showing that it is a display bug rather than a lost atom). This would indicate the the original programmer also struggled somewhat with these edge cases. :)
1 parent 1aeb7d0 commit 9f4d073

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

game.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,20 @@ function nextPlayer() {
256256
}
257257

258258
function addAtom(cell, playerId, postAdd) {
259-
if(cell.dataset.atoms <= cell.dataset.maxAtoms) {
260-
if(cell.dataset.playerId != -1 && cell.dataset.playerId != playerId) {
261-
players[cell.dataset.playerId].atoms -= parseInt(cell.dataset.atoms);
262-
players[playerId].atoms += parseInt(cell.dataset.atoms);
263-
if(players[cell.dataset.playerId].atoms == 0) {
264-
players[cell.dataset.playerId].alive = false;
265-
document.getElementById('playerList').getElementsByClassName('player' + cell.dataset.playerId)[0].className += ' dead';
266-
}
267-
}
268-
cell.dataset.playerId = playerId;
269-
cell.dataset.atoms++;
270-
players[playerId].atoms++;
271-
if(cell.dataset.atoms > cell.dataset.maxAtoms) {
272-
overloaded.push(cell);
259+
if(cell.dataset.playerId != -1 && cell.dataset.playerId != playerId) {
260+
players[cell.dataset.playerId].atoms -= parseInt(cell.dataset.atoms);
261+
players[playerId].atoms += parseInt(cell.dataset.atoms);
262+
if(players[cell.dataset.playerId].atoms == 0) {
263+
players[cell.dataset.playerId].alive = false;
264+
document.getElementById('playerList').getElementsByClassName('player' + cell.dataset.playerId)[0].className += ' dead';
273265
}
274266
}
267+
cell.dataset.playerId = playerId;
268+
cell.dataset.atoms++;
269+
players[playerId].atoms++;
270+
if(parseInt(cell.dataset.atoms) == parseInt(cell.dataset.maxAtoms) + 1) {
271+
overloaded.push(cell);
272+
}
275273
drawAtoms(cell, function postDraw(e) {
276274
this.removeEventListener('transitionend', postDraw);
277275
postAdd();
@@ -292,9 +290,19 @@ function explodeCell(cell, postAdd) {
292290
for(var atom; atom = atoms[0];) {
293291
cell.removeChild(atom);
294292
}
295-
players[cell.dataset.playerId].atoms -= parseInt(cell.dataset.atoms);
293+
players[cell.dataset.playerId].atoms -= parseInt(cell.dataset.maxAtoms) + 1;
294+
var overflowAtoms = parseInt(cell.dataset.atoms) - (parseInt(cell.dataset.maxAtoms) + 1);
296295
cell.dataset.atoms = 0;
297-
cell.dataset.playerId = -1;
296+
if(overflowAtoms == 0) {
297+
cell.dataset.playerId = -1;
298+
} else {
299+
for(var i = 1; i <= overflowAtoms; i++) {
300+
cell.dataset.atoms++;
301+
var newAtom = document.createElement('div');
302+
newAtom.className = 'atom atom' + i + ' player' + playerId + ' pos' + (overflowAtoms - i + 1);
303+
cell.appendChild(newAtom);
304+
}
305+
}
298306
var x = parseInt(cell.style.gridColumn, 10);
299307
var y = parseInt(cell.style.gridRow, 10);
300308
var neighbour;
@@ -352,7 +360,7 @@ function drawAtoms(cell, postTransition) {
352360
newAtom.className = 'atom atom3 player' + cell.dataset.playerId + ' pos1';
353361
});
354362
});
355-
} else if(cell.dataset.atoms >= 4) {
363+
} else if(cell.dataset.atoms == 4) {
356364
cell.getElementsByClassName('atom1')[0].className = 'atom atom1 player' + cell.dataset.playerId + ' pos4';
357365
cell.getElementsByClassName('atom2')[0].className = 'atom atom2 player' + cell.dataset.playerId + ' pos3';
358366
cell.getElementsByClassName('atom3')[0].className = 'atom atom3 player' + cell.dataset.playerId + ' pos2';
@@ -363,6 +371,15 @@ function drawAtoms(cell, postTransition) {
363371
newAtom.className = 'atom atom4 player' + cell.dataset.playerId + ' pos1';
364372
});
365373
});
374+
} else if(cell.dataset.atoms >= 5) {
375+
console.log(cell.dataset.atoms + " at " + cell.id);
376+
newAtom.className = 'atom atom5 player' + cell.dataset.playerId + ' pos1 size0';
377+
cell.appendChild(newAtom);
378+
window.requestAnimationFrame(function() {
379+
window.requestAnimationFrame(function() {
380+
newAtom.className = 'atom atom5 player' + cell.dataset.playerId + ' pos1';
381+
});
382+
});
366383
}
367384
}
368385

0 commit comments

Comments
 (0)