Fully playable prototype of “Plus+Plus” built in JavaScript using only jQuery

Read all posts about "" game
Did you play Plus+Plus? Described as an “hidden gem” by TouchArcade, it’s a simple and funny number puzzle. The game is played on a 5×5 grid, and the numbers one to six – but they will increase as you progress with the game – to fit into the grid making horizontal or vertical rows of the same number. You can stack numbers on top of each other, but only once. As usual, this is the kind of game which would take me an hour to explain, while you can understand in a minute by playing it on your own:
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. I made the entire game with no game oriented frameworks, using only JavaScript and jQuery. Moreover, I did not use arrays to store board information, I am checking tile contents by reading its inner HTML. The game features score, increases difficulty and shows you the three next numbers you have to place on the grid. All game elements are rendered with HTML, using good old tables and cascading style sheets like this one:
body{
    margin: 0;
    padding: 5px;
    background-color: #2c3e50;
    color: #ecf0f1;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
#tilecontainer{
    width:360px;
    margin: 0px auto;
}
.tile{
    width: 60px;
    height: 60px;
    border: 1px solid #7f8c8d;
    margin: 5px;
    cursor: pointer;
    text-align: center;
    font: bold 32px arial;
    line-height: 60px;
}
#nextTile1{
    width: 60px;
    height: 60px;
    border: 1px solid #16a085;
    margin: 5px;
    cursor: pointer;
    text-align: center;
    font: bold 32px arial;
    color: #16a085;
    line-height: 60px;
}
.smallTile{
    width:30px;
    height: 30px;
    border: 1px solid #7f8c8d;
    margin: 34px 5px 5px 5px;
    float: left;
    color: #7f8c8d;
    text-align: center;
    font: bold 16px arial;
    line-height: 30px;
}
#allowedNumbers{
    margin: 5px;
    font: normal 16px arial;
}
#score{
    margin: 5px;
    font: normal 24px arial;
    text-align: right;
}
.greenTile{
    color: #16a085;
    border-color: #16a085;
}
.redTile{
    color: #e74c3c;
    border-color: #e74c3c;
}
Then, the JavaScript part which also adds all game elements.
$(document).ready(function(){
    var maxNumber = 6;
    var score = 0;
    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;
        }
        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);
        }
    })
})

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 &gt; 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"&gt;';
    for(var i = -2; i < 6; i++){
        tableHTML += '<tr&gt;';
        for(var j = 0; j < 5; j++){
            switch(i){
                case -2:
                    switch(j){
                        case 0:
                        case 1:
                            tableHTML += '<td&gt;</td&gt;';
                            break;
                        case 2:
                            tableHTML += '<td&gt;<div id = "nextTile1"&gt;</div&gt;</td&gt;';
                            break;
                        case 3:
                            tableHTML += '<td colspan = "2"&gt;<div class = "smallTile" id = "nextTile2"&gt;</div&gt;<div class = "smallTile" id = "nextTile3"&gt;</div&gt;<div style = "clear:both"&gt;</div&gt;</td&gt;';
                            break;
                    }
                    break;
                case -1:
                    if(j == 1){
                        tableHTML += '<td colspan = "5"&gt;<div id = "allowedNumbers"&gt;</div&gt;</td&gt;';
                    }
                    break;
                case 5:
                    if(j == 1){
                        tableHTML += '<td colspan = "5"&gt;<div id = "score"&gt;</div&gt;</td&gt;';
                    }
                    break;
                default:
                    tableHTML += '<td&gt;<div class = "tile" data-row = "' + i + '" data-col = "' + j + '"&gt;</div&gt;</td&gt;';
            }
        }
        tableHTML += '</tr&gt;';
    }
    tableHTML += '</table&gt;';
    $("#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);
    }
}
The code is not commented because it’s just a jQuery experiment, but I’ll translate it into something more interesting using Phaser and Panda, meanwhile you can 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

214 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
// 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