Function game2048 (container) { This. container = container; This. tiles = new Array (16 ); } Game2048.prototype = { Init: function (){ For (var I = 0, len = this. tiles. length; I <len; I ++ ){ Var tile = this. newTile (0 ); Tile. setAttribute ('index', I ); This. container. appendChild (tile ); This. tiles [I] = tile; } This. randomTile (); This. randomTile (); }, NewTile: function (val ){ Var tile = document. createElement ('div '); This. setTileVal (tile, val) Return tile; }, SetTileVal: function (tile, val ){ Tile. className = 'tile tile' + val; Tile. setAttribute ('val', val ); Tile. innerHTML = val> 0? Val :''; }, RandomTile: function (){ Var zeroTiles = []; For (var I = 0, len = this. tiles. length; I <len; I ++ ){ If (this. tiles [I]. getAttribute ('val') = 0 ){ ZeroTiles. push (this. tiles [I]); } } Var rTile = zeroTiles [Math. floor (Math. random () * zeroTiles. length)]; This. setTileVal (rTile, Math. random () <0.8? 2: 4 ); }, Move: function (direction ){ Var j; Switch (direction ){ Case 'W ': For (var I = 4, len = this. tiles. length; I <len; I ++ ){ J = I; While (j> = 4 ){ This. merge (this. tiles [j-4], this. tiles [j]); J-= 4; } } Break; Case's ': For (var I = 11; I> = 0; I --){ J = I; While (j <= 11 ){ This. merge (this. tiles [j + 4], this. tiles [j]); J + = 4; } } Break; Case 'A ': For (var I = 1, len = this. tiles. length; I <len; I ++ ){ J = I; While (j % 4! = 0 ){ This. merge (this. tiles [j-1], this. tiles [j]); J-= 1; } } Break; Case 'D ': For (var I = 14; I> = 0; I --){ J = I; While (j % 4! = 3 ){ This. merge (this. tiles [j + 1], this. tiles [j]); J + = 1; } } Break; } This. randomTile (); }, Merge: function (prevTile, currTile ){ Var prevVal = prevTile. getAttribute ('val '); Var currVal = currTile. getAttribute ('val '); If (currVal! = 0 ){ If (prevVal = 0 ){ This. setTileVal (prevTile, currVal ); This. setTileVal (currTile, 0 ); } Else if (prevVal = currVal ){ This. setTileVal (prevTile, prevVal * 2 ); This. setTileVal (currTile, 0 ); } } }, Equal: function (tile1, tile2 ){ Return tile1.getAttribute ('val') = tile2.getAttribute ('val '); }, Max: function (){ For (var I = 0, len = this. tiles. length; I <len; I ++ ){ If (this. tiles [I]. getAttribute ('val') = 2048 ){ Return true; } } }, Over: function (){ For (var I = 0, len = this. tiles. length; I <len; I ++ ){ If (this. tiles [I]. getAttribute ('val') = 0 ){ Return false; } If (I % 4! = 3 ){ If (this. equal (this. tiles [I], this. tiles [I + 1]) { Return false; } } If (I <12 ){ If (this. equal (this. tiles [I], this. tiles [I + 4]) { Return false; } } } Return true; }, Clean: function (){ For (var I = 0, len = this. tiles. length; I <len; I ++ ){ This. container. removeChild (this. tiles [I]); } This. tiles = new Array (16 ); } } Var game, startBtn; Window. onload = function (){ Var container = document. getElementById ('div2048 '); StartBtn = document. getElementById ('start '); StartBtn. onclick = function (){ This. style. display = 'none '; Game = game | new game2048 (container ); Game. init (); } } Window. onkeydown = function (e ){ Var keynum, keychar; If (window. event) {// IE Keynum = e. keyCode; } Else if (e. which) {// Netscape/Firefox/Opera Keynum = e. which; } Keychar = String. fromCharCode (keynum ); If (['w', 's', 'A', 'D']. indexOf (keychar)>-1 ){ If (game. over ()){ Game. clean (); StartBtn. style. display = 'block '; StartBtn. innerHTML = 'game over, replay? '; Return; } Game. move (keychar ); } } |