Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to write a console version of 2048 in C language

Shulou Source: shulou.com Published: 2022-06-02 01:04:48 11月27日 Update

How to write a console version of 2048 in C language, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Let's start with the header files that you need to use:

# include (needless to say)

# include (system, etc.)

# include (memset function, very useful)

# include (to use _ getch (void);)

Bool is often used in the program, but there is no bool in the C language, so you need to define a type bool:

Typedef int bool

# define true 1

# define false 0

Define the image size:

# define ROW 5

# define COL ROW

Define various types of data:

Empty: int BOX_NONE = 0

Used to determine whether there is a number in a location: int giBox [row] [COL]

Save game state: int game_state = 0

Matters needing attention:

A number that produces a random position:

* rand [rand ()% count] = (1 + rand ()% 2) * 2

/ / generate a random number. Rand ()% count refers to the remainder after the random number / 0, and the final range becomes [0 to count-1].

/ / the same range of the latter is (1 + [0J1]) * 2 = 2 or 4

/ / pampi already has those positions with zero values

Location switching:

/ / swap the values of two locations, usually with 0

Add two numbers:

/ / combining two numbers, res is the one that moves

Display the game interface:

A number that produces a random position:

Location switching:

Add two numbers:

The most important thing is the movement function. In the movement function, it should be noted that the number of each position should be operated, and the position of the number should be exchanged with the most marginal zero position.

See the following overall code for details:

This is the answer to the 2048 question on how to write a console version in C language. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Location number language function question move console control two addition more range random number help answer easy important simple item code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Redmi Shulou Tech Info Huawei