1+ function game2048 ( container )
2+ {
3+ this . container = container ;
4+ this . tiles = new Array ( 16 ) ;
5+ }
6+
7+ game2048 . prototype = {
8+ init : function ( ) {
9+ for ( var i = 0 , len = this . tiles . length ; i < len ; i ++ ) {
10+ var tile = this . newTile ( 0 ) ;
11+ tile . setAttribute ( 'index' , i ) ;
12+ this . container . appendChild ( tile ) ;
13+ this . tiles [ i ] = tile ;
14+ }
15+ this . randomTile ( ) ;
16+ this . randomTile ( ) ;
17+ } ,
18+ newTile : function ( val ) {
19+ var tile = document . createElement ( 'div' ) ;
20+ this . setTileVal ( tile , val )
21+ return tile ;
22+ } ,
23+ setTileVal : function ( tile , val ) {
24+ tile . className = 'tile tile' + val ;
25+ tile . setAttribute ( 'val' , val ) ;
26+ tile . innerHTML = val > 0 ? val : '' ;
27+ } ,
28+ randomTile : function ( ) {
29+ var zeroTiles = [ ] ;
30+ for ( var i = 0 , len = this . tiles . length ; i < len ; i ++ ) {
31+ if ( this . tiles [ i ] . getAttribute ( 'val' ) == 0 ) {
32+ zeroTiles . push ( this . tiles [ i ] ) ;
33+ }
34+ }
35+ var rTile = zeroTiles [ Math . floor ( Math . random ( ) * zeroTiles . length ) ] ;
36+ this . setTileVal ( rTile , Math . random ( ) < 0.8 ? 2 : 4 ) ;
37+ } ,
38+ move :function ( direction ) {
39+ var j ;
40+ switch ( direction ) {
41+ case 'W' :
42+ for ( var i = 4 , len = this . tiles . length ; i < len ; i ++ ) {
43+ j = i ;
44+ while ( j >= 4 ) {
45+ this . merge ( this . tiles [ j - 4 ] , this . tiles [ j ] ) ;
46+ j -= 4 ;
47+ }
48+ }
49+ break ;
50+ case 'S' :
51+ for ( var i = 11 ; i >= 0 ; i -- ) {
52+ j = i ;
53+ while ( j <= 11 ) {
54+ this . merge ( this . tiles [ j + 4 ] , this . tiles [ j ] ) ;
55+ j += 4 ;
56+ }
57+ }
58+ break ;
59+ case 'A' :
60+ for ( var i = 1 , len = this . tiles . length ; i < len ; i ++ ) {
61+ j = i ;
62+ while ( j % 4 != 0 ) {
63+ this . merge ( this . tiles [ j - 1 ] , this . tiles [ j ] ) ;
64+ j -= 1 ;
65+ }
66+ }
67+ break ;
68+ case 'D' :
69+ for ( var i = 14 ; i >= 0 ; i -- ) {
70+ j = i ;
71+ while ( j % 4 != 3 ) {
72+ this . merge ( this . tiles [ j + 1 ] , this . tiles [ j ] ) ;
73+ j += 1 ;
74+ }
75+ }
76+ break ;
77+ }
78+ this . randomTile ( ) ;
79+ } ,
80+ merge : function ( prevTile , currTile ) {
81+ var prevVal = prevTile . getAttribute ( 'val' ) ;
82+ var currVal = currTile . getAttribute ( 'val' ) ;
83+ if ( currVal != 0 ) {
84+ if ( prevVal == 0 ) {
85+ this . setTileVal ( prevTile , currVal ) ;
86+ this . setTileVal ( currTile , 0 ) ;
87+ }
88+ else if ( prevVal == currVal ) {
89+ this . setTileVal ( prevTile , prevVal * 2 ) ;
90+ this . setTileVal ( currTile , 0 ) ;
91+ }
92+ }
93+ } ,
94+ equal : function ( tile1 , tile2 ) {
95+ return tile1 . getAttribute ( 'val' ) == tile2 . getAttribute ( 'val' ) ;
96+ } ,
97+ max : function ( ) {
98+ for ( var i = 0 , len = this . tiles . length ; i < len ; i ++ ) {
99+ if ( this . tiles [ i ] . getAttribute ( 'val' ) == 2048 ) {
100+ return true ;
101+ }
102+ }
103+ } ,
104+ over : function ( ) {
105+ for ( var i = 0 , len = this . tiles . length ; i < len ; i ++ ) {
106+ if ( this . tiles [ i ] . getAttribute ( 'val' ) == 0 ) {
107+ return false ;
108+ }
109+ if ( i % 4 != 3 ) {
110+ if ( this . equal ( this . tiles [ i ] , this . tiles [ i + 1 ] ) ) {
111+ return false ;
112+ }
113+ }
114+ if ( i < 12 ) {
115+ if ( this . equal ( this . tiles [ i ] , this . tiles [ i + 4 ] ) ) {
116+ return false ;
117+ }
118+ }
119+ }
120+ return true ;
121+ } ,
122+ clean : function ( ) {
123+ for ( var i = 0 , len = this . tiles . length ; i < len ; i ++ ) {
124+ this . container . removeChild ( this . tiles [ i ] ) ;
125+ }
126+ this . tiles = new Array ( 16 ) ;
127+ }
128+ }
129+
130+ var game , startBtn ;
131+
132+ window . onload = function ( ) {
133+ var container = document . getElementById ( 'div2048' ) ;
134+ startBtn = document . getElementById ( 'start' ) ;
135+ startBtn . onclick = function ( ) {
136+ this . style . display = 'none' ;
137+ game = game || new game2048 ( container ) ;
138+ game . init ( ) ;
139+ }
140+ }
141+
142+ window . onkeydown = function ( e ) {
143+ var keynum , keychar ;
144+ if ( window . event ) { // IE
145+ keynum = e . keyCode ;
146+ }
147+ else if ( e . which ) { // Netscape/Firefox/Opera
148+ keynum = e . which ;
149+ }
150+ keychar = String . fromCharCode ( keynum ) ;
151+ if ( [ 'W' , 'S' , 'A' , 'D' ] . indexOf ( keychar ) > - 1 ) {
152+ if ( game . over ( ) ) {
153+ game . clean ( ) ;
154+ startBtn . style . display = 'block' ;
155+ startBtn . innerHTML = 'game over, replay?' ;
156+ return ;
157+ }
158+ game . move ( keychar ) ;
159+ }
160+ }
0 commit comments