Fully playable prototype of “Plus+Plus” built in JavaScript using only jQuery part 2 – removing numbers

Read all posts about "" game
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);
    }
}
When dealing with tile based games, sometimes jQuery can be enough, do you agree? Download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers