2048 Game Implementation Principles

Source: Internet
Author: User

Record the logic of the 2048 game

The essence of this game is the two-bit array, which analyzes the key logic and implementation with the 4*4 two-bit array. Two-bit array 1

--------"Results

Fig. 1 Fig. 2 Fig. 3

All of our operations are operations on the data of this two-dimensional array. Divided into up and down four directions. Let's say the direction to the left (2).

The result of the left operation is 3;

When the opposite direction is, all the data runs along the horizontal direction to the left, well, this is: see the result.

 Level at odds:

1: The horizontal description operates on one row of a two-dimensional array, while the vertical operation is a column of a two-bit array. This allows the operation of the two-dimensional array to be manipulated into one-dimensional arrays after traversal.

2: The left-hand side indicates that the priority of the data is from the left. This determines where the traversal of a one-dimensional array begins.

 

Figure 2 The CCP four rows, each row can get a thought array

ARR1:[0,0,2,0];

ARR2:[0,4,2,0];

arr3:[0,0,4,4];

ARR4:[2,0,2,0];

  Here is the logic of each line (the core logic of this game, the person who came up with this is really powerful)

varGetData =function(arr) {//iterates through the array from the current position of the array to the next start, looking for a position that is not 0 ()        //If you don't find anything, don't do it.        //If you find            //if the current position is 0, swap the current position with the next (the current position gets the data at the next position and the next position data to 0, minus one for the subscript)            //if the current position and the next position are equal, the current position data is set at the next position data 0        vari,nexti,len,m; Len=arr.length;  for(i = 0; i < len; i + = 1) {            //find Nexti First .Nexti =-1;  for(M = i+1; m < Len; m++){                if(Arr[m]!== 0) {Nexti=m;  Break; }            }            if(Nexti!==-1) {                //there is a location next to 0.                if(Arr[i] = = = 0) {Arr[i]=Arr[nexti]; Arr[nexti]= 0; I-= 1; } Else if(Arr[i] = = =Arr[nexti]) {Arr[i]= Arr[i] * *; Arr[nexti]= 0; }            }        }        returnarr; };

So the left direction becomes.

Gets an array of each row from top to bottom, in the direction left. Parameters (Row,left);

   

The other three directions at the beginning remember how to get the array, and so on after the operation is put back so that can be achieved

 

varGetData=function(arr) {
//iterates through the array from the current position of the array to the next start of the traversal, looking for not0the location()
// If you don't find anything, don't do it.
// If you find
//if the current location is0, then swap the current position with the next (the current position gets the data at the next position, and the next position data is0, minus one for the subscript)
//if the current position and the next position are equal, the current position data * *, the next position data is placed0
varI,Nexti,Len,m;
Len= Arr.length;
for(I=0;I<Len;I+=1) {
//First FindNexti
Nexti= -1;
for(m=I+1;m<Len;m++){
if(arr[m] !==0) {
Nexti=m;
Break;
}
}

if(Nexti!== -1) {
//There is no next0the location
if(arr[I] ===0) {
arr[I] = arr[Nexti];
arr[Nexti] =0;
I-=1;
}else if(arr[I] = = = arr[Nexti]) {
arr[I] = arr[I] *2;
arr[Nexti] =0;
}
}
}
returnArr
};

2048 Game Implementation Principles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.