400 Line Code C Language Console edition 2048 game, write crazy C language code

Source: Internet
Author: User

Today idle to boring, with everyone to write the Black window version of 2048, the effect is as follows:

First, the game introduction

"2048" is a recently popular digital game. The original 2048 was first published on GitHub and was originally written by Gabriele Cirulli. It is based on the "1024" and "Little 3 Legends" (threes!) the development of a new type of digital games.

Ii. rules of the game

The rules of the game are very simple, you need to control all the blocks in the same direction, two of the same number of blocks hit together after merging into their and, after each operation will be randomly generated in the blank squares of a 2 or 4 (the probability of generating 2 is larger), and eventually get a "2048" block even victory.

Third, the core algorithm

1, block move, and merge algorithms.

main idea: The game Digital panel is abstracted into 4 rows and 4 columns of two-dimensional array a[4][4], the value of 0 of the position represents an empty square, the other represents the corresponding number square. Treat each row equally, study only one row of moving and merging algorithms, and then iterate through the rows to implement all the rows of the move merge algorithm. In a row, use b[4] to represent an array of rows, using two subscript variables to traverse the column items, where j and K are used, where j is always behind K, to find the first number after the K term is not 0, and the K term is used to denote the item to be compared, always separated by a number 0 between the J item, or simply next to each other. Without losing its generality, considering moving to the left, J equals 1, while K equals 0, and then determines if J is greater than 0, if, then the relationship between J and K numbers is divided into 3 cases, respectively P1:, P2:b[k]==0 and P3:b[k]!=0 and B[k]!=b[j] If no, then J adds 1, and then continues to look for the first digit that is not 0 after the K term. where P1,P2 and P3 correspond as follows:

P1:B[K]==B[J], then b[k] = 2 * b[k] (description two number merged), and b[j] = 0 (after merging the residual J value is cleared 0), then K is added 1, and then the next cycle.

P2:b[k]==0, it means b[j] before all is empty lattice, at this time directly move B[j] to K position, that is b[k] = B[j], and then b[j] = 0 (after moving the residual J value of 0), then the K value is unchanged, and then the next cycle.

P3:b[k]!=0 and B[k]!=b[j], it means that two numbers are unequal and not 0, at which point two counts together, i.e. b[k+1] = B[j]. Then there are two small cases, if the j!=k+1, then b[j] = 0 (after moving the residual J-value of 0); If not, it means that two of the number was originally put together, then no special treatment (equivalent to not moved). Then k self-add 1, then the next cycle.

To give an example of a P1, the process is represented as follows:

The move merge algorithm in one row is described as follows (this is the left-hand case, and the other direction is similar, the difference is only the way of traversing the row and column items of a two-dimensional array):

2, judge whether the game is the end of the algorithm

Core idea: traverse the two-dimensional array to see if there are horizontal and vertical two adjacent elements equal, if present, the game does not end, if not exist, then the game is over.

The algorithm code is described below (board represents a two-dimensional array used in the real game source):

3. Generate Random number algorithm

Core idea: According to the generated random number, a certain value is modeled to generate a certain probability of the number. In this game, set 2 probability is 4 twice times, so you can use the system provided by the random number function to generate a number, and then 3 to take the remainder, the resulting number if less than 2 in the game panel space generated a 2, if the remainder equals 2, then generate 4. When choosing which space to be in the number of births, it is also based on the random function provided by the system to generate a count, and then the number of spaces to take the remainder, and then in the remainder of the space out to generate a number.

4, the algorithm of drawing interface

Core idea: Use the system to provide the console interface to clear the screen function, to achieve the effect of refreshing the interface, the use of Control tab position, to draw the game digital panel effect.

Because the drawing interface is not the essence of the game, and the code snippet is relatively long, so the algorithm description is omitted here, the reader can refer to the full source code.

Four, the complete source code is as follows, the code is too large to show, interested can be settled in the humble house to obtain:


400 Line Code C Language Console edition 2048 game, write crazy C language code

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.