C Language implementation simple 2048 games

Source: Internet
Author: User
Tags function definition

Always like to play this small game, simple game contains luck and thinking and strategy, like this simple without losing the connotation of the game style. So the idea of realizing it in C language was born.

The specific code is imitating this: https://www.cnblogs.com/judgeyoung/p/3760515.html

Bo Master analysis are in place, a lot of algorithmic skills are worth reference, C language implementation of the main idea of 2048 has been in that blog detailed analysis, but I think in the blogger's code is still a lot of good ideas are worth learning from me.

For example, this generates random numbers, by the way, the probability of a random number:

/*generating a random number function definition*/voidAdd_rand_num () {Srand (0)); intn = rand ()% get_null_count ();/*determine where to generate random numbers in empty locations*/     for(inti =0; I <4; i++)    {         for(intj =0; J <4; J + +)        {            if(Board[i][j] = =0&& n--= =0)/*Locate the location you want to generate*/{Board[i][j]= (rand ()%3?2:4);/*determine what value is generated, set the probability of generating 2 is twice times the probability of 4*/                return; }        }    }}

The first is the Srand () function, which is the initialization function of a random number generator.

The prototype is: void Srand (unsigned seed)

The usage is: the programmer needs to provide a random number of seeds for this function: Srand (random number), if the same seed is used, then the rand () function behind each run is to generate the same random number, that is, pseudo-random number.

such as: Srand (1), directly with one to initialize the seed, followed by the same random number.

In order to generate a real random number, we generally use system time as the seed of the random number initialization function. Use the time () function to obtain system times:

Its return value is from 00:00:00 GMT, January 1, 1970 to the current number of seconds, and then the time_t data into (unsigned) type and then to the Srand function, that is: Srand ((unsigned) time (&t)) ;

There is also a regular usage that does not need to define the time_t t variable, namely: Srand ((unsigned) time (NULL)); Pass in a null pointer directly, because your program often does not require parameters to obtain the T data.

The second sentence is:

int n = rand ()% get_null_count (); // generate a random position in a free grid

The number of remaining spaces is calculated by using random numbers, and the random number is less than the remaining lattice number.

The last sentence is:

if 0 0) // a random number is filled in at random positions at 0 o'clock, but if the random position is not 0?              {                324);  the probability of 1/3,2/3 is obtained by filling a 2 or 4 in a randomly generated blank, and using the three expressions to obtain the residual of 3.                 return;   End Function            }

That is, when there is no number in the lattice and in the location of the random number just generated, fill in a number 2 or 4, and in order to reduce the difficulty, fixed generation 2 is the probability of generating 4 twice times.

The probability distribution is produced by using the three-mesh operator and the characteristics of the 3 redundancy.

RAND () The number of random numbers to 3 is only possible for 0/1/2, and in the three-mesh operator, when the first number is not 0 o'clock, the value of the operator takes the value of the middle number, otherwise the last number is taken. So the probability of taking 2 is 2/3, and the probability of taking 4 is 1/3, which gives rise to different probabilities. But this method can only produce the probability distribution of n:1, if it is to produce 4:5, 8:17 probability, this method is not applicable.

The above code is still a bit of a loophole, because the game is initially required to have two numbers, a number must be 2, the other number is the number generated above, but if the second number is exactly generated in the random position is the first number, then according to the code here, there is nothing to do, The result is a case where the first interface is just a number.

The solution is to not use Get_null_count () in the two-digit program that was just started, but instead to get the exact position of the first number 2, and then generate a number when the second number is generated, where the first number is excluded.

The main body of the program is the number of moving up and down, Go_left () and the other three functions, their ideas are similar, take the Go_left () function to analyze:

According to the original author's thoughts, there are three situations when moving.

If the adjacent two numbers are the same, they are merged and the numbers are added together.

If the adjacent numbers are different, simply say, there are two things: numbers need to move and no need to move.

The specific code is:

/*left shift function*/voidGo_left (void){    /*i traverse row subscript*/     for(inti =0; I <4; i++)    {        /*j for column subscript, K for comparison column subscript, circular entry K < J*/         for(intj =1, k =0; J <4; J + +)        {            /*find the first item that is not 0 after K*/            if(Board[i][j] >0)            {                /*Scenario 1*/                if(Board[i][j] = = Board[i][k])//merge two numbers in the same way{scoer+ = board[i][k++] <<=1; BOARD[I][J]=0; If_need_add_num=1;//generate random numbers and refresh the interface after merging                }                /*Scenario 2*/                Else if(Board[i][k] = =0)//k is empty, move J to K-Grid{Board[i][k]=Board[i][j]; BOARD[I][J]=0; If_need_add_num=1; }                /*Scenario 3*/                Else                                //The K term is not empty, nor is it equal to J, at which point two does not need to be moved, but the subscript needs to be transformedBOARD[I][++K] = Board[i][j];//move J to the right of the K item.                if(j! = k)//not equal after moving, they're not next to each other.{Board[i][j]=0; If_need_add_num=1;//at this point the move does not eliminate a number, but also to add a random number out                }            }        }    }}

The project complete code has been given in the original blog.

Operating effect:

C Language implementation simple 2048 games

Related Article

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.