{"id":588,"date":"2020-04-12T18:59:48","date_gmt":"2020-04-12T17:59:48","guid":{"rendered":"https:\/\/www.maxcanvas.org\/?page_id=588"},"modified":"2026-10-25T22:13:25","modified_gmt":"2026-10-25T22:13:25","slug":"mah-jong","status":"publish","type":"page","link":"https:\/\/www.maxcanvas.org\/?page_id=588","title":{"rendered":"Mah Jong"},"content":{"rendered":"\n<figure><iframe loading=\"lazy\" width=\"920px\" height=\"720px\" src=\"https:\/\/www.maxcanvas.org\/MaxCanvas\/Examples\/MC%20-%2011-%20Mah%20Jong.html\"><\/iframe><\/figure>\n\n\n\n<p>I used to play this, when the Internet was not invented.  So it&#8217;s about time I created my own version.  It&#8217;s the most complex example of MaxCanvas todate, and I&#8217;ll readily admit to not having tidied the code up (or commenting it) too much.  The data structures are a bit clunky and hard to follow, but &#8230; it works, is playable and I&#8217;m happy.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ MaxCanvas Bases Maj-Jong Tile match implementation\n\/\/ Started March 2020 - whenever 8-)\n\/\/\n\/\/ Required to drag MC.js into Intellisense\n\/\/ thanks to https:\/\/visualstudiomagazine.com\/blogs\/tool-tracker\/2018\/07\/use-javascript-code.aspx (Dec 2018)\n\/\/\/ &lt;reference path=\"MC.js\" \/>\n\n\/\/ Gamecode is wrapped in .. window.onload = (function () { -- ALL CODE HERE --}); .. so game will start AFTER everything else is loaded\n\nwindow.onload = (function() {\n\n    \/\/ AQUIRE HTML DOM CANVAS REFERENCE\n    \/\/     HTML has to have a HTML5 canvas element tagged as \"canvas\"\n    var c=document.getElementById(\"canvas\");\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ INITIALISE MC Engine \/\/\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ CHOOSE a canvas size option \/\/ \n\n    \/\/ CHOICE 1: Canvas Size fixed by HTML\n    MC.init(c);\n\n    \/\/ CHOICE 2: Full Window Option (Canvas will be resized to fit window)\n    \/\/MC.init(c,\"fullWindow\");\n\n    \/\/ Configure .game\n    MC.game.gravity.set(0,100);        \/\/ gravity this game\n    MC.game.physicsUpdates = 1;       \/\/ Simple example, no need for multiple physics Updates\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ END OF Initialisation \/\/\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/-----------------------------------------------------------------------------\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ VARIABLE DECLARATION \/\/\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    var tilesheet = new MC.Picture(\"Images\/MahjongTiles.png\");\n    var effectSheet = new MC.Picture(\"Images\/Freeze.png\");\n    var backdrop = new MC.Picture(\"Images\/blue-grunge-starburst.jpg\")\n    var shadow = new MC.Color(121, 121, 121, 0.7);\n\n    var Effects = new MC.SpriteBin();\n    EffectsFactory = function (position, duration) {\n        var Effect = new MC.Sprite({ type: \"anim\", picture: effectSheet, scale: 0.75, pos: position, angle: MC.maths.randBetween(0,360), angleVel: MC.maths.randBetween(-360,360) });\n        Effect.setAnimation(10, 10, 0, 0, 5, 8, 20);\n        Effect.kill(duration);\n        Effects.push(Effect);\n    }\n\n    var Scales = new Object({ dx: 48, dy: 60, dyy: 6 });\n\n    function TileData( x, y, match, number, name) {\n        this.x = x;\n        this.y = y;\n        this.match = match;\n        this.number = number;\n        this.name = name;  };\n\n    var AllTileData = new Array (\n        new TileData(0,0,1,1,\"Flower-Plum\"),\n        new TileData(1,0,1,1,\"Flower-Orchid\"),\n        new TileData(2,0,1,1,\"Flower-Chrys\"),\n        new TileData(3,0,1,1,\"Flower-Bamboo\"),\n        new TileData(4,0,2,1,\"Flower-Summer\"),\n        new TileData(5,0,2,1,\"Flower-Winter\"),\n        new TileData(6,0,2,1,\"Flower-Fall\"),\n        new TileData(7,0,2,1,\"Flower-Spring\"),\n\n        new TileData(0,1,3,4,\"Dragon-Red\"),\n        new TileData(1,1,4,4,\"Dragon-Green\"),\n        new TileData(2,1,5,4,\"Dragon-White\"),\n        new TileData(3,1,6,4,\"Wind-North\"),\n        new TileData(4,1,7,4,\"Wind-South\"),\n        new TileData(5,1,8,4,\"Wind-East\"),\n        new TileData(6,1,9,4,\"Wind-West\"),\n        new TileData(7,1,10,4,\"Crak-One\"),\n\n        new TileData(0, 2, 11, 4, \"Crak-Two\"),\n        new TileData(1, 2, 12, 4, \"Crak-Three\"),\n        new TileData(2, 2, 13, 4, \"Crak-Four\"),\n        new TileData(3, 2, 14, 4, \"Crak-Five\"),\n        new TileData(4, 2, 15, 4, \"Crak-Six\"),\n        new TileData(5, 2, 16, 4, \"Crak-Seven\"),\n        new TileData(6, 2, 17, 4, \"Crak-Eight\"),\n        new TileData(7, 2, 18, 4, \"Crak-Nine\"),\n\n        new TileData(0,3,19,4,\"Bamboo-One\"),\n        new TileData(1,3,20,4,\"Bamboo-Two\"),\n        new TileData(2,3,21,4,\"Bamboo-Three\"),\n        new TileData(3,3,22,4,\"Bamboo-Four\"),\n        new TileData(4,3,23,4,\"Bamboo-Five\"),\n        new TileData(5,3,24,4,\"Bamboo-Six\"),\n        new TileData(6,3,25,4,\"Bamboo-Seven\"),\n        new TileData(7,3,26,4,\"Bamboo-Eight\"),\n\n        new TileData(0,4,27,4,\"Bamboo-Nine\"),\n        new TileData(1,4,28,4,\"Coin-One\"),\n        new TileData(2,4,29,4,\"Coin-Two\"),\n        new TileData(3,4,30,4,\"Coin-Three\"),\n        new TileData(4,4,31,4,\"Coin-Four\"),\n        new TileData(5,4,32,4,\"Coin-Five\"),\n        new TileData(6,4,33,4,\"Coin-Six\"),\n        new TileData(7,4,34,4,\"Coin-Seven\"),\n\n        new TileData(0,5,35,4,\"Coin-Eight\"),\n        new TileData(1,5,36,4,\"Coin-Nine\"),\n        new TileData(2,5,37,0,\"Blank\"),\n        new TileData(3,5,38,0,\"Red\"),\n        new TileData(4,5,38,0,\"Green\"),\n        new TileData(5,5,39,0,\"Blue\"),\n        new TileData(6,5,40,0,\"Yellow\"),\n        new TileData(7,5,41,0,\"Mask\")   );\n\n\n    var Deck = new Array();\n    for (var i = 0; i &lt; AllTileData.length; i++) {\n        for (var j = 0; j &lt; AllTileData[i].number; j++) {\n            Deck.push(i);\n        }\n    }\n\n    ShuffleDeck = function () {\n        Deck = MC.utils.shuffle(Deck);\n    }\n\n    \/\/    |-0-|-1-|-2-|-3-|-4-|-5-|-6-|-7-|-8-|-9-|10-|11-|\n    \/\/            |12-|13-|14-|15-|16-|17-|18-|19-|\n    \/\/        |20-|21-|22-|23-|24-|25-|26-|27-|28-|29-|\n    \/\/    |30-|31-|32-|33-|34-|35-|36-|37-|38-|39-|40-|41-|\n    \/\/|42-|                                               |43-|44-|\n    \/\/    |45-|46-|47-|48-|49-|50-|51-|52-|53-|54-|55-|56-|\n    \/\/        |57-|58-|59-|60-|61-|62-|63-|64-|65-|66-|\n    \/\/            |67-|68-|69-|70-|71-|72-|73-|74-|\n    \/\/    |75-|76-|77-|78-|79-|80-|81-|82-|83-|84-|85-|86-|\n    \/\/\n    \/\/                |87-|88-|89-|90-|91-|92-|\n    \/\/                |93-|94-|95-|96-|97-|98-|\n    \/\/                |99-|100|101|102|103|104|\n    \/\/                |105|106|107|108|109|110|\n    \/\/                |111|112|113|114|115|116|\n    \/\/                |117|118|119|120|121|122|\n    \/\/\n    \/\/                    |123|124|125|126|\n    \/\/                    |127|128|129|130|\n    \/\/                    |131|132|133|134|\n    \/\/                    |135|136|137|138|\n    \/\/\n    \/\/                        |139|140|\n    \/\/                        |141|142|\n    \/\/\n    \/\/                          |143|\n\n    \/\/ we need to draw these in ordr of tiers (in all shadow then all tiles) order\n    var DrawOrder = [\n        [0,86],\n        [87,122],\n        [123,138],\n        [139,142],\n        [143,143] ];\n\n\n    var grid1 = new Array(\n        \/\/ TIER ONE\n        [-5.5, 3.5, 1, -1, 1, -1], [-4.5, 3.5, 1, 0, 2, -1], [-3.5, 3.5, 1, 1, 3, -1], [-2.5, 3.5, 1, 2, 4, -1], [-1.5, 3.5, 1, 3, 5, -1], [-0.5, 3.5, 1, 4, 6, -1],\n        [ 0.5, 3.5, 1, 5, 7, -1], [1.5, 3.5, 1, 6, 8, -1], [2.5, 3.5, 1, 7, 9, -1], [3.5, 3.5, 1, 8, 10, -1], [4.5, 3.5, 1, 9, 11, -1], [5.5, 3.5, 1, 10, -1, -1],\n    \n        [-3.5,2.5,1,-1,12,-1],[-2.5,2.5,1,12,14,87],[-1.5,2.5,1,13,15,88],[-0.5,2.5,1,14,16,89],\n        [0.5,2.5,1,15,17,90],[1.5,2.5,1,16,18,91],[2.5,2.5,1,17,19,92],[3.5,2.5,1,18,-1,-1],\n\n        [-4.5,1.5,1,-1,21,-1],[-3.5,1.5,1,20,22,-1],[-2.5,1.5,1,21,23,93],[-1.5,1.5,1,22,24,94],[-0.5,1.5,1,23,25,95],\n        [0.5,1.5,1,24,26,96],[1.5,1.5,1,25,27,97],[2.5,1.5,1,26,28,98],[3.5,1.5,1,27,29,-1],[4.5,1.5,1,28,-1,-1],\n\n        [-5.5,0.5,1,42,31,-1],[-4.5,0.5,1,30,32,-1],[-3.5,0.5,1,31,33,-1],[-2.5,0.5,1,32,34,99],[-1.5,0.5,1,33,35,100],[-0.5,0.5,1,34,36,101],\n        [0.5,0.5,1,35,37,102],[1.5,0.5,1,36,38,103],[2.5,0.5,1,37,39,104],[3.5,0.5,1,38,40,-1],[4.5,0.5,1,39,41,-1],[5.5,0.5,1,40,43,-1],\n\n        [-6.5,0,1,-1,30,45],[6.5,0,1,41,44,56],[7.5,0,1,43,-1,-1],\n\n        [-5.5,-0.5,1,42,46,-1],[-4.5,-0.5,1,45,47,-1],[-3.5,-0.5,1,46,48,-1],[-2.5,-0.5,1,47,49,105],[-1.5,-0.5,1,48,50,106],[-0.5,-0.5,1,49,51,107],\n        [0.5,-0.5,1,50,52,108],[1.5,-0.5,1,51,53,109],[2.5,-0.5,1,52,54,110],[3.5,-0.5,1,53,55,-1],[4.5,-0.5,1,54,56,-1],[5.5,-0.5,1,55,43,-1],\n\n        [-4.5,-1.5,1,-1,58,-1],[-3.5,-1.5,1,57,59,-1],[-2.5,-1.5,1,58,60,111],[-1.5,-1.5,1,59,61,112],[-0.5,-1.5,1,60,62,113],\n        [0.5,-1.5,1,61,63,114],[1.5,-1.5,1,62,64,115],[2.5,-1.5,1,63,65,116],[3.5,-1.5,1,64,66,-1],[4.5,-1.5,1,65,-1,-1],\n\n        [-3.5,-2.5,1,-1,68,-1],[-2.5,-2.5,1,67,69,117],[-1.5,-2.5,1,68,70,118],[-0.5,-2.5,1,69,71,119],\n        [0.5,-2.5,1,70,72,120],[1.5,-2.5,1,71,73,121],[2.5,-2.5,1,72,74,122],[3.5,-2.5,1,73,-1,-1],\n\n        [-5.5, -3.5, 1, -1, 76, -1], [-4.5, -3.5, 1, 75, 77, -1], [-3.5, -3.5, 1, 76, 78, -1], [-2.5, -3.5, 1, 77, 79, -1], [-1.5, -3.5, 1, 78, 80, -1], [-0.5, -3.5, 1, 79, 81, -1],\n        [ 0.5, -3.5, 1, 80, 82, -1], [1.5, -3.5, 1, 81, 83, -1], [2.5, -3.5, 1, 82, 84, -1], [3.5, -3.5, 1, 83, 85, -1], [4.5, -3.5, 1, 84, 86, -1], [5.5, -3.5, 1, 85, -1, -1],\n       \/\/  TIER TWO\n        [-2.5, 2.5, 2, -1, 88, -1], [-1.5, 2.5, 2, 87, 89, -1],[-0.5, 2.5, 2, 88, 90, -1], [0.5, 2.5, 2, 89, 91, -1], [1.5, 2.5, 2, 90, 92, -1],[2.5, 2.5, 2, 91, -1, -1],\n        [-2.5, 1.5, 2, -1, 94, -1], [-1.5, 1.5, 2, 93, 95, 123],[-0.5, 1.5, 2, 94, 96, 124], [0.5, 1.5, 2, 95, 97, 125], [1.5, 1.5, 2, 96, 98, 126],[2.5, 1.5, 2, 97, -1, -1],\n        [-2.5, 0.5, 2, -1, 100, -1], [-1.5, 0.5, 2, 99, 101, 127],[-0.5, 0.5, 2, 100, 102, 128], [0.5, 0.5, 2, 101, 103, 129], [1.5, 0.5, 2, 102, 104, 130],[2.5, 0.5, 2, 103, -1, -1],\n        [-2.5, -0.5, 2, -1, 106, -1], [-1.5, -0.5, 2, 105, 107, 131],[-0.5, -0.5, 2, 106, 108, 132], [0.5, -0.5, 2, 107, 109, 133], [1.5, -0.5, 2, 108, 110, 134],[2.5, -0.5, 2, 109, -1, -1],\n        [-2.5, -1.5, 2, -1, 112, -1], [-1.5, -1.5, 2, 111, 113, 135],[-0.5, -1.5, 2, 112, 114, 136], [0.5, -1.5, 2, 113, 115, 137], [1.5, -1.5, 2, 114, 116, 138],[2.5, -1.5, 2, 115, -1, -1],\n        [-2.5, -2.5, 2, -1, 118, -1], [-1.5, -2.5, 2, 117, 119, -1],[-0.5, -2.5, 2, 118, 120, -1], [0.5, -2.5, 2, 119, 121, -1], [1.5, -2.5, 2, 120, 122, -1],[2.5, -2.5, 2, 121, -1, -1],\n        \/\/  TIER THREE\n        [-1.5,1.5,3,-1,124,-1],[-0.5,1.5,3,123,125,-1],[0.5,1.5,3,124,126,-1],[1.5,1.5,3,125,-1,-1],\n        [-1.5,0.5,3,-1,128,-1],[-0.5,0.5,3,127,129,139],[0.5,0.5,3,128,130,140],[1.5,0.5,3,129,-1,-1],\n        [-1.5,-0.5,3,-1,132,-1],[-0.5,-0.5,3,131,133,141],[0.5,-0.5,3,132,134,142],[1.5,-0.5,3,133,-1,-1],\n        [-1.5,-1.5,3,-1,136,-1],[-0.5,-1.5,3,135,137,-1],[0.5,-1.5,3,136,138,-1],[1.5,-1.5,3,137,-1,-1],\n        \/\/ TIER FOUR\n        [-0.5,0.5,4,-1,140,143],[0.5,0.5,4,139,-1,143],\n        [-0.5,-0.5,4,-1,142,143],[0.5,-0.5,4,141,-1,143],\n        \/\/  TIER FIVE\n        [0,0,5,-1,-1,-1]\n        );\n\n    function GridData(x,y, tier,left,right,upper) {\n        this.offX = x;\n        this.offY = y;\n        this.tier = tier;\n        this.left = left;\n        this.right = right;\n        this.upper = upper;\n    };\n\n\n\n    TileFactory = function (TileDataIndex) {\n        T = new MC.Sprite({\n            type: \"anim\",\n            picture: tilesheet,\n            pos: new MC.Point(MC.canvas.midX, MC.canvas.midY),\n            scale: 0.75\n        });\n        T.data = AllTileData[TileDataIndex];\n        T.setAnimation(8, 6, T.data.x, T.data.y, T.data.x, T.data.y, 99999);\n        return T;\n    };\n\n    var GameGrid = new Array();\n\n    GameGrid.update = function () {\n        for (var i = 0; i &lt; 144; i++) {\n            GameGrid[i].Tile.update();\n        }\n    };\n\n    GameGrid.render = function () {\n        for (var Tier = 0; Tier &lt; DrawOrder.length; Tier++) {\n            \/\/ draw shadows\n            for (var i = DrawOrder[Tier][0]; i &lt;= DrawOrder[Tier][1]; i++) {\n                if(GameGrid[i].alive)\n                    MC.draw.rectBasic(GameGrid[i].Tile.pos.clone().add(-7, 7), GameGrid[i].Tile.size1 \/ 8 * GameGrid[i].Tile.scale, GameGrid[i].Tile.size2 \/ 6 * GameGrid[i].Tile.scale, Tier > 0 ? shadow : \"black\");\n            }\n            \/\/ draw tiles\n            for (var i = DrawOrder[Tier][0]; i &lt;= DrawOrder[Tier][1]; i++) {\n                if (GameGrid[i].alive)\n                    GameGrid[i].Tile.render();\n            }\n        }\n\n        if (GameGrid.Selected != null) {\n            GameGrid[GameGrid.Selected].Tile.drawBB(\"orange\");\n            \/*\n            \/\/ CHEAT FUNCTION.  Saved for dev purposes (highlights tile matches to the currently selected one\n            for (var i = 0; i &lt; 144; i++) {\n                if (GameGrid.Selected != i &amp;&amp; GameGrid[i].alive &amp;&amp; GameGrid[i].free() &amp;&amp; GameGrid[i].type == GameGrid[GameGrid.Selected].type) {\n                    GameGrid[i].Tile.drawBB(\"red\");\n                }\n            }\n            *\/\n        }\n    };\n\n    GameGrid.clear = function () {\n        GameGrid.length = 0;\n    }\n\n    GameGrid.Selected = null;\n\n    GameGrid.OnClick = function () {\n        var m = MC.mouse.pos.clone();\n        var onGrid = false;\n        for (var i = 0; i &lt; 144; i++) {\n            if (GameGrid[i].Tile.hit(m)) {\n                if (GameGrid[i].alive) onGrid = true;\n                var free = GameGrid[i].free();\n\n                \/\/ Null, select if legal\n                if (GameGrid.Selected == null &amp;&amp; free == true) {\n                    GameGrid.Selected = i;\n                }\n                    \/\/ Deselect if required\n                else if (GameGrid.Selected == i) {\n                    GameGrid.Selected = null;\n                }\n                    \/\/ maka a match\n                else if (i != GameGrid.Selected &amp;&amp; free &amp;&amp; GameGrid[i].type == GameGrid[GameGrid.Selected].type) {\n                    EffectsFactory(GameGrid[i].Tile.pos, 0.3);\n                    EffectsFactory(GameGrid[GameGrid.Selected].Tile.pos, 0.3);\n                    GameGrid[i].alive = GameGrid[GameGrid.Selected].alive = false;\n                    Moves.addToHistory( i, GameGrid.Selected );\n                    GameGrid.Selected = null;\n                    Moves.Calculate();\n                }\n                    \/\/ Deselect AND Reselect\n                else if (i != GameGrid.Selected &amp;&amp; free == true) {\n                    GameGrid.Selected = i;\n                }\n            }\n        }\n        if (!onGrid) GameGrid.Selected = null;\n    };\n\n    GameGrid.Shuffle = function () {\n        var positions = [];\n        var tileNumbers = [];\n        for (var i = 0; i &lt; 144; i++) {\n            if (GameGrid[i].alive) {\n                positions.push(i);\n                tileNumbers.push(GameGrid[i].tileNumber);\n            }\n        }\n        tileNumbers = MC.utils.shuffle(tileNumbers);\n        var len = positions.length;\n        for (var i = 0; i &lt; len; i++) {\n            GameGrid[positions[i]] = ConfiguredTileFactory( positions[i], tileNumbers[i] );\n        }\n        Moves.Calculate();\n        Moves.newGame();\n        GOCon.reset();\n    };\n    \n    ConfiguredTileFactory = function ( GridPosition, TileNumber ) {\n        var g = new Object();\n        g.Tile = TileFactory(TileNumber);\n        g.tileNumber = TileNumber;\n        g.gridPosition = GridPosition;\n        g.type = g.Tile.data.match;\n        g.name = g.Tile.data.name;\n        g.offX = grid1[GridPosition][0];\n        g.offY = grid1[GridPosition][1];\n        g.tier = grid1[GridPosition][2];\n        g.Tile.pos.x = MC.canvas.midX + (g.offX * Scales.dx);\n        g.Tile.pos.y = MC.canvas.midY + (g.offY * Scales.dy * -1) - (g.tier * Scales.dyy);\n        g.leftTile = grid1[GridPosition][3];\n        g.rightTile = grid1[GridPosition][4];\n        g.upperTile = grid1[GridPosition][5];\n        g.selected = false;\n        g.alive = true;\n        g.free = function () {\n            if (!this.alive) return false;\n            switch (this.gridPosition) {\n                \/\/ offX,offY, tier, left, right, upper\n                \/\/  42 - [-6.5,0,1,-1,30,45],  43 -  [6.5,0,1,41,44,56],   44 -  [7.5,0,1,43,-1,-1],\n                case 42:\n                    return true;\n\n                case 43:\n                    if (GameGrid[44].alive == false || (GameGrid[41].alive == false &amp;&amp; GameGrid[56].alive == false)) {\n                        return true;\n                    }\n                    else {\n                        return false;\n                    }\n\n                case 44:\n                    return true;\n\n                default:\n                    if (this.upperTile != -1) {\n                        if (GameGrid[this.upperTile].alive == true) return false;\n                    }\n                    if (this.rightTile == -1 || this.leftTile == -1) return true;\n                    if (this.rightTile != -1 &amp;&amp; GameGrid[this.rightTile].alive == false) return true;\n                    if (this.leftTile != -1 &amp;&amp; GameGrid[this.leftTile].alive == false) return true;\n\n                    return false;\n            }\n        };\n        return g;\n    }\n\n    Reset = function () {\n        ShuffleDeck();\n        GameGrid.clear();\n\n        for (var i = 0; i &lt; 144; i++) {\n            GameGrid.push(ConfiguredTileFactory(i, Deck[i]));\n            GameGrid[i].alive = true;\n        }\n        GameGrid.Selected = null;\n        Moves.newGame();\n        Moves.Calculate();\n        Timer.start();\n        EndGameController.stop();\n    }\n\n    var Moves = {\n        Available: 0,\n        OptionsArray: null, \/\/ Tile.Type ranges from 1-36 .. using a 37 length Array, to prevent head bending later\n        Clear: function () {\n            this.OptionsArray = new Array(37);\n            for (var i = 0; i &lt; 37; i++) this.OptionsArray[i] = new Array(0);\n        },\n        Calculate: function() {\n            this.Clear();\n            this.Available = 0;\n            var aliveTiles = 0;\n            for (var i = 0; i &lt; 144; i++) {\n                if (GameGrid[i].alive == true) aliveTiles++;\n                if ( GameGrid[i].free() ) { \n                    this.OptionsArray[GameGrid[i].type].push( i );\n                    switch ( this.OptionsArray[GameGrid[i].type].length ) {\n                        case 0:\n                        case 1:\n                            break;\n                        case 2:\n                            this.Available += 1;\n                            break;\n                        case 3:\n                            this.Available += 2;\n                            break;\n                        case 4:\n                            this.Available += 3;\n                            break;\n                        default:\n                            break;\n                    }\n                }\n            }\n            MoveDisplay.setValues({text: \" Moves: \"+ this.Available + \" \"});\n            \/\/ Check for end Game\n            if (this.Available == 0) {\n                var message = aliveTiles == 0 ? \"Well Done !!  New Game?\" : \"No Moves ... New Game?\";\n                GOCon.start(message);\n                Timer.pause();\n                if (aliveTiles == 0) EndGameController.start();\n            }\n        },\n        history: [],\n        addToHistory: function ( position1, position2 ) {\n            this.history.push(new MC.Point(position1, position2));\n        },\n        newGame: function() {\n            this.history = [];\n        },\n        undo: function() {\n            if (this.history.length > 0) {\n                var last = this.history.pop();\n                GameGrid[last.x].alive = true;\n                GameGrid[last.y].alive = true;\n            }\n        },\n        restart: function() {\n            for (var i = 0; i &lt; 144; i++) GameGrid[i].alive = true;\n        },\n        hint: function () {\n            var moves = [];\n            for (var i = 0; i &lt; this.OptionsArray.length; i++) {\n                if( this.OptionsArray[i].length >=2){\n                    moves.push(i);\n                }\n            }\n            if (moves.length > 0) {\n                moves = MC.utils.shuffle(moves);\n                var index = moves[0];\n                this.OptionsArray[index] = MC.utils.shuffle(this.OptionsArray[index]);\n                var t1 = this.OptionsArray[index][0];\n                var t2 = this.OptionsArray[index][1];\n                EffectsFactory(GameGrid[t1].Tile.pos, 1);\n                EffectsFactory(GameGrid[t2].Tile.pos, 1);\n            }\n        }\n    };\n\n    \/\/ of course, we need a piece of fun for the end game\n    var EndGame = new MC.SpriteBin();\n    var EndGameController = {\n        running: false,\n        time: 0,\n        spriteCount: 80,\n        update: function () {\n            if (this.running == true) {\n                if (EndGame.bin.length &lt; this.spriteCount) {\n                    this.time += MC.game.deltaTime;\n                    if (this.time > 0.2) {\n                        this.time = 0;\n                        var tile = TileFactory(MC.maths.randBetween(0, AllTileData.length - 7));\n                        tile.setValues({\n                            edgeBounce: true,\n                            gravity: true,\n                            pos: new MC.Point(MC.maths.randBetween(40, MC.canvas.width - 40), 40),\n                            vel: new MC.Point(MC.maths.randBetween(-80, 80), 0),\n                            angleVel: MC.maths.randBetween( -360,360 )\n                        });\n                        EndGame.push(tile);\n                    }\n                }\n            EndGame.update();\n            }\n        },\n        stop: function () {\n            EndGame.empty();\n            this.running = false;\n        },\n        start: function () {\n            EndGame.empty();\n            this.running = true;\n        },\n        render: function () {\n            EndGame.render();\n        }\n    };\n   \n\n    var leftGUI = new MC.GUI();      \/\/ GUI = holder for Control Boxes (MC.ConBox)\n    var rightGUI = new MC.GUI();\n    var ResetButton = new MC.ConBox({ width: 120,\n                                height: 40,\n                                text: \"New Game (n)\",\n                                onClick: function () { Reset(); GOCon.reset(); }\n    });\n    var Shuffle = new MC.ConBox({\n        width: 120,\n        height: 40,\n        text: \" Shuffle (s) \",\n        onClick: function () { GameGrid.Shuffle(); EndGameController.stop(); }\n\n    });\n    var Undo = new MC.ConBox({\n        width: 120,\n        height: 40,\n        text: \"  Undo (u)  \",\n        onClick: function () { Moves.undo(); GOCon.reset(); EndGameController.stop(); }\n\n    });\n    var Restart = new MC.ConBox({\n        width: 120,\n        height: 40,\n        text: \" Restart (r) \",\n        onClick: function () { Moves.restart(); GOCon.reset(); EndGameController.stop(); Timer.start(); }\n\n    });\n    var Hint = new MC.ConBox({\n        width: 120,\n        height: 40,\n        text: \"  Hint (h)  \",\n        onClick: function () { Moves.hint(); }\n\n    });\n    \/\/ using this an an inert Con Box, for display purposes\n    var MoveDisplay = new MC.ConBox({\n        width: 120,\n        height: 40,\n        text: \" Moves:  \"\n    });\n\n    var TimeDisplay = MoveDisplay.clone(); \/\/ n.b. text will be set by Timer\n\n    leftGUI.push(ResetButton, Restart, Shuffle, Undo, Hint);  \/\/ Add the ConBoxes to the GUI\n    rightGUI.push(MoveDisplay, TimeDisplay);\n\n    \/\/ special ConBox, modified to bounce in at end game, user Timer and the NEW MC.Utils.tween methods\n \n    var GameOver = new MC.ConBox({\n        width: 300,\n        height: 70,\n        text: \"Well Done!!  New Game?\",\n        pos: new MC.Point(MC.canvas.midX, 0 - this.height),\n        onClick: function () { GOCon.onClick(); } \/\/ defer click method to the Controller\n    });\n\n    GOCon = { \/\/ GameOver Controller\n        box: GameOver,\n        size: new MC.Point(GameOver.width, GameOver.height),\n        positions: [ new MC.Point(MC.canvas.midX, 0 - (2.5 * GameOver.height)),\n                     new MC.Point(MC.canvas.midX, MC.canvas.height - GameOver.height),\n                     new MC.Point(MC.canvas.midX, MC.canvas.height + GameOver.height)],\n        times: [ 2, 1 ],\n        animationTime: 0,\n        running: false,\n        comingIn: true,\n        onClick: function () {\n            if (!this.running) {\n                this.running = true;\n                Reset();\n            }\n        },\n        reset: function () {\n            this.box.setValues({\n                width: this.size.x,\n                height: this.size.y,\n                pos: this.positions[0]\n            });\n            this.running = false;\n            this.comingIn = true;\n        },\n        start: function (message) {\n            this.reset();\n            if (message != undefined) this.box.setValues({ text: message });\n            this.running = true;\n            animationTime = 0; },\n        update: function () {\n            if (this.running == true) {\n                this.animationTime += MC.game.deltaTime;\n                var mod = this.comingIn == true ? 0 : 1;\n                var finished = this.animationTime >= this.times[mod];\n                var tween = this.animationTime \/ this.times[mod];\n                this.box.setValues({\n                    pos: MC.maths.lerp(this.positions[mod], this.positions[mod + 1], tween),\n                });\n                if (this.comingIn == true) {\n                    this.box.setValues({\n                        width: this.size.x * (tween + 0.1),\n                        height: this.size.y * (tween + 0.1)\n                    });\n                }\n                if (finished == true) {\n                    if (this.comingIn == false) this.reset();\n                    else {\n                        this.running = false;\n                        this.comingIn = false;\n                        this.animationTime = 0;\n                    }\n                }\n            }\n            else { this.box.update(); }\n        }\n    }\n\n    var Timer = {\n        running: false,\n        elapsed: 0,\n        start: function () {\n            this.running = true;\n            this.elapsed = 0;\n            this.updateLabel();\n        },\n        pause: function () {\n            this.running = false;\n        },\n        update: function () {\n            if (this.running == true) {\n                this.elapsed += MC.game.deltaTime;\n                this.updateLabel();\n            }\n        },\n        updateLabel: function () {\n            \/\/ credit to https:\/\/stackoverflow.com\/questions\/6312993\/javascript-seconds-to-time-string-with-format-hhmmss (Apr 2020)\n            var sec_num = parseInt(this.elapsed, 10);\n            var hours = Math.floor(sec_num \/ 3600);\n            var minutes = Math.floor((sec_num - (hours * 3600)) \/ 60);\n            var seconds = sec_num - (hours * 3600) - (minutes * 60);\n\n            if (hours &lt; 10) { hours = \"0\" + hours; }\n            if (minutes &lt; 10) { minutes = \"0\" + minutes; }\n            if (seconds &lt; 10) { seconds = \"0\" + seconds; }\n            TimeDisplay.setValues({ text: hours + ':' + minutes + ':' + seconds });\n        }\n    };\n\n    MC.keys.n.onUp = function () { Reset(); GOCon.reset(); };\n    MC.keys.s.onUp = function () { GameGrid.Shuffle(); };\n    MC.keys.r.onUp = function () { Restart.onClick(); };\n    MC.keys.u.onUp = function () { Undo.onClick(); };\n    MC.keys.h.onUp = function () { Hint.onClick(); };\n\n    MC.mouse.onClickL = function () { \/\/ configure MC.game so it checks the GUI upon Left click down \n        GameGrid.OnClick();\n        leftGUI.click();\n        GameOver.checkClick();\n\n        \/\/ Feel free to add further code for Left Clicks here \n    };    \n    \n\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ END OF declarations  \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ Game Initialisation  \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    Reset();\n    GOCon.reset();\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/      GAME LOOP       \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\/\/ first call, request subsequently made within\ngameLoop();\n\nfunction gameLoop() {\n    \/\/ ESSENTIAL MAINTAINANCE\n    requestAnimationFrame(gameLoop);    \/\/ Required so the PC will automatically call the gameLoop again when (1) the screen is ready and (2) the CPU has finished any previous frame calculations\n    MC.game.update();                   \/\/ Essential to update MC.game to ensure MC.game.deltaTime is available\n    \n    \/\/ PHYSICS UPDATE LOOP .. \n\n    for (var i = 0; i &lt; MC.game.physicsUpdates; i++) {\n        EndGameController.update();\n        \/\/ Add Further Sprite and SpriteBin updates here\n    }\n\n\n    \/\/ NON-PHYSICS UPDATES\n    GameGrid.update();\n    leftGUI.update();\n    GOCon.update();\n    Effects.update();\n    Timer.update();\n\n    \/\/ RENDER\n    \/\/ Stage 1.  Clear the canvas\n    MC.draw.clearCanvas(\"Black\");\n\n    \/\/ Stage 2.  Render Sprites\n    backdrop.render(new MC.Point(MC.canvas.midX, MC.canvas.midY), 0, MC.canvas.width, MC.canvas.height);\n    GameGrid.render();\n    Effects.render();\n    EndGameController.render();\n    \n    \/\/ Stage 3.  Render GUIs (and moves)\n    leftGUI.render(0, 0);\n    rightGUI.render(MC.canvas.right - 140, 0);\n    GameOver.render();\n\n}\n\n}); \/\/ EoF\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I used to play this, when the Internet was not invented. So it&#8217;s about time I created my own version. It&#8217;s the most complex example of MaxCanvas todate, and I&#8217;ll readily admit to not having tidied the code up (or commenting it) too much. The data structures are a bit clunky and hard to follow, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-588","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=588"}],"version-history":[{"count":3,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/588\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/588\/revisions\/603"}],"wp:attachment":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}