Talking about Plus+Plus game, Game development and Javascript.
There’s an update to the Plus+Plus prototype built in JavaScript using only jQuery. In the original game there is an option to remove a number by clicking on a special tile. We are going to add these “remove” tiles, you can find them in the bottom left corner of the game, to remove a number click on the “remove” tile – marked with a R – then click on a number. Look at the game: Click on a tile to place a number or stack a number on top of another number. You can’t stack on top of red numbers. Rows or columns filled with the same numbers will disappear. If the board becomes too crowded, you can remove a number by clicking or a “Remove” tile then clicking on the number you want to remove. “Remove” tiles also work only using their class names and handle various cases, such as: * “Remove” tile is highlighted when selected * “Remove” tile disappears once it has been used * You can choose any of the two “Remove” tiles * You can deselect a triggered “Remove” tile by clicking again on it, or by clicking on an empty tile to add a new number Here is the source code:
$(document).ready(function(){
var maxNumber = 6;
var score = 0;
var destroyTile = false;
drawBoard();
updateGUI(maxNumber, score);
drawNumbers(maxNumber, true);
$('.tile').click(function(){
var legalMove = false;
var row = $(this).data('row');
var col = $(this).data('col');
if($(this).html() == ''){
$(this).html($('#nextTile1').html()).addClass('greenTile');
legalMove = true;
destroyTile = false;
$('.bomb').removeClass('greenTile');
}
else{
if(destroyTile){
$(this).html('').removeClass('greenTile').removeClass('redTile');
destroyTile = false;
$('.bomb.greenTile').remove();
}
else{
if($(this).hasClass('greenTile')){
$(this).html(parseInt($(this).html()) + parseInt($('#nextTile1').html())).removeClass('greenTile').addClass('redTile');
legalMove = true;
}
}
}
if(legalMove){
score += parseInt($('#nextTile1').html());
score += removeTiles(row, col);
maxNumber = updateGUI(maxNumber, score);
$('#nextTile1').html($('#nextTile2').html());
$('#nextTile2').html($('#nextTile3').html());
drawNumbers(maxNumber, false);
}
});
$('.bomb').click(function(){
if($(this).hasClass('greenTile')){
$(this).removeClass('greenTile');
destroyTile = false;
}
else{
$('.bomb').removeClass('greenTile');
$(this).addClass('greenTile');
destroyTile = true;
}
})
})
function removeTiles(row, col){
var score = 0;
if(removeRow(row)){
for(i = 0; i < 5; i++){
score += parseInt($('.tile[data-row = "' + row + '"][data-col = "' + i + '"]').html());
$('.tile[data-row = "' + row + '"][data-col = "' + i + '"]').html("").removeClass('greenTile').removeClass('redTile');
}
}
if(removeCol(col)){
for(i = 0; i < 5; i++){
score += parseInt($('.tile[data-row = "' + i + '"][data-col = "' + col + '"]').html());
$('.tile[data-row = "' + i + '"][data-col = "' + col + '"]').html("").removeClass('greenTile').removeClass('redTile');
}
}
return score;
}
function removeRow(row){
var value = $('.tile[data-row = "' + row + '"][data-col = "' + 0 + '"]').html();
if(value == ""){
return false
}
for(var i = 1; i < 5; i++){
if($('.tile[data-row = "' + row + '"][data-col = "' + i + '"]').html() != value){
return false;
}
}
return true;
}
function removeCol(col){
var value = $('.tile[data-row = "' + 0 + '"][data-col = "' + col + '"]').html();
if(value == ""){
return false;
}
for(var i = 1; i < 5; i++){
if($('.tile[data-row = "' + i + '"][data-col = "' + col + '"]').html() != value){
return false;
}
}
return true;
}
function updateGUI(n, s){
if(s > n * (n - 1) * (n - 2)){
n ++;
}
var html = "";
for(var i = 1; i <= n; i++){
html += " " + i;
}
$('#allowedNumbers').html(html);
$('#score').html(s);
return n;
}
function drawBoard(){
var tableHTML = '<table cellspacing = "0" cellpadding = "0" border = "0">';
for(var i = -2; i < 6; i++){
tableHTML += '<tr>';
for(var j = 0; j < 5; j++){
switch(i){
case -2:
switch(j){
case 0:
case 1:
tableHTML += '<td></td>';
break;
case 2:
tableHTML += '<td><div id = "nextTile1"></div></td>';
break;
case 3:
tableHTML += '<td colspan = "2"><div class = "smallTile" id = "nextTile2"></div><div class = "smallTile" id = "nextTile3"></div><div style = "clear:both"></div></td>';
break;
}
break;
case -1:
if(j == 1){
tableHTML += '<td colspan = "5"><div id = "allowedNumbers"></div></td>';
}
break;
case 5:
if(j == 1){
tableHTML += '<td colspan = "2"><div class = "bomb">R</div><div class = "bomb">R</div><div style = "clear:both"></div></td><td colspan = "3"><div id = "score"></div></td>';
}
break;
default:
tableHTML += '<td><div class = "tile" data-row = "' + i + '" data-col = "' + j + '"></div></td>';
}
}
tableHTML += '</tr>';
}
tableHTML += '</table>';
$("#tilecontainer").html(tableHTML);
}
function drawNumbers(max, firstRun){
var n = Math.floor(Math.random() * max) + 1;
$('#nextTile3').html(n);
if(firstRun){
n = Math.floor(Math.random() * max) + 1;
$('#nextTile2').html(n);
n = Math.floor(Math.random() * max) + 1;
$('#nextTile1').html(n);
}
}
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.