In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use C language to complete the 2048 game, the article is very detailed, has a certain reference value, interested friends must read it!
I. the idea of the game
1. A menu appears at the beginning of the program, allowing players to choose to start the game or quit the game.
2. After the player chooses to start the game, the chessboard appears and the direction of movement is selected through the key.
3. After moving, all squares will be moved in that direction until the blanks are completed, and the squares of the same number will be merged into their sum, resulting in a random position of 2 or 4.
4. When "2048" appears, the game wins; when the chessboard is full and cannot be eliminated, the game fails.
II. Game framework
1. Menu interface
Menu
Void menu () {printf ("* *\ n"); printf ("* press 1 to start the game *\ n"); printf ("* press 0 to exit the game *\ n") Printf ("* *\ n");}
The choice of menu
Int main () {int input = 0; do {menu (); printf ("Please select:"); scanf ("% d", & input); switch (input) {case 1: game2048 () Break; case 0: printf ("quit the game, guests often come to play ~)\ n"); break; default: printf ("give you a chance to choose again\ n"); break }} while (input); return 0;}
Actual effect
I think the menu is quite pleasing to the eye, but it is a bit ugly. After all, if you are a beginner, don't ask too much.
two。 Game subject 1. Initialize the interface:
Generate an array of four rows and four columns (the number of rows and columns here are defined with the # define macro in the header file).
And print it out.
Void DisplayBoard (int board [ROW] [COL], int row, int col) {int I = 0; int j = 0; for (int j = 0; j)
< COL; j++) printf("+-----"); printf("+\n"); for (int i = 0; i < ROW; i++) { for (int j= 0; j < COL; j++) { printf("|"); if (board[i][j]!=0) printf("%5d", board[i][j]); else printf(" "); } printf("|\n"); for (int j = 0; j < COL; j++) { printf("+-----"); } printf("+\n"); } } 效果如下: 方吗?方就对了,方就是个好界面! 2.随机生成初始数字 游戏的界面我们现在已经有了,接下来要做的就是加入两个随机位置的"2"。 void get_num(int board[ROW][COL]){ int x, y; x = rand() % ROW; y = rand() % COL; board[x][y] = 2;//一开始随机的数为2 x = rand() % ROW; y = rand() % COL; while (board[x][y] == 2) { x = rand() % ROW; y = rand() % COL; } board[x][y] = 2; system("cls"); //清屏,美观界面 return;} (这里照例还是使用了时间戳获取随机数的方法)You can see that each occurrence of the two positions is random.
3. Achieve Mobility
Here we take up as an example, we want to move, and determine that two square numbers are the same and merge into their sum, and continue to move up when there is a space after the merge.
To put it simply, move → to merge → and move to fill the void again. The same is true in the other three directions, which can be achieved by changing the values.
Void up (int board [ROW] [COL]) {int I = 0; int j = 0; int x = 0; int y = 0; for (I = 0; I
< ROW; i++) { //移动 j = 0; y = 0; while (j < COL-1 && y < COL-1 ) { if (board[j][i] == 0) { for (x = j; x < ROW-1; x++) board[x][i] = board[x + 1][i]; board[ROW-1][i] = 0; y++; } else j++; } //合并 for (j = 0; j < COL-1; j++) if (board[j][i] == board[j + 1][i] && board[j][i] != 0) { board[j][i] = board[j][i] * 2; //和 board[j + 1][i] = 0; } //再次移动补空 j = 0; y = 0; while (j < COL - 1 && y < COL - 1) { if (board[j][i] == 0) { for (x = j; x < ROW - 1; x++) board[x][i] = board[x + 1][i]; board[ROW - 1][i] = 0; y++; } else j++; } }}4.增加新数字 每次移动会在随机位置出现一个新的数字,可能是2,可能是4。 通过查询网上资料得知,随机到2的概率约为 9/10,随机到4的概率约为 1/10。 void put_num(int board[ROW][COL]){ int x = 0; int y = 0; int z = 0; x = rand() % ROW; y = rand() % COL; while (board[x][y] !=0) { x = rand() % ROW; y = rand() % COL; } z = rand() % 10; if (z 0) { if (board[i - 1][j] == board[i][j]) return 0; } if (j >0) {if (board [I] [j-1] = = board [I] [j]) return 0;} return 1 } int is_win (int board [ROW] [COL]) {int I = 0; int j = 0; int num = 0; for (I = 0; I
< ROW; i++) for (j = 0; j < COL; j++) { if (board[i][j] >Num) num = board [I] [j];} if (num > = 2048) return 1; else return 0;} 6. Game function
Combine the above code
Void game2048 () {int board [ROW] [COL] = {{0}}; int control = 0; DisplayBoard (board); init_num (board); system ("cls"); / / clear screen, beautiful interface DisplayBoard (board) While ((control = _ getch ())! = 0x1b) {switch (control) {case 0xe0: switch (control = getch ()) {case 72: up (board) Break; case 80: down (board); break; case 75: left (board) Break; case 77: right (board); break; default: break } system ("cls"); DisplayBoard (board); if (is_win (board) = = 1) {printf ("Congratulations on winning!") ;} if (is_fail (board) = = 1) {printf ("good food\ n");} III. Game operation.
It is known that there is bug in the game at present. When the grid is full, it is assumed that one direction can be eliminated and the other direction cannot be eliminated. If you press that direction that cannot be eliminated, you will not be able to operate. In view of the fact that I have not been able to think of a good solution for beginners at present. If a boss has a good way, you can also give me some advice.
I am too bad to play for a long time to play less than 2048, the highest only 5121024 is typed by roommates.
Update: fixed bug
Determine whether the array is the same or not, and then choose whether to add a new random position number.
I would like to thank all the friends who helped me with the test, no matter whether it was successful or not, of course, I would like to thank the friends who provided this picture.
4. All codes
Game2048.h
# pragma once # include # define ROW 4#define COL 4const int copy [ROW] [COL]; / initialize and print the game interface void DisplayBoard (int board [ROW] [COL]); / / randomly generate two 2void init_num (int board [ROW] [COL]); / / randomly place the number 2 or 4void put_num (int board [ROW] [COL]) after moving; / / Mobile void up (int board [ROW] [COL]) Void down (int board [ROW] [COL]); void left (int board [ROW] [COL]); void right (int board [ROW] [COL]); / / int is_win (int board [ROW] [COL]); int is_fail (int board [ROW] [COL])
Game2048.c
# define _ CRT_SECURE_NO_WARNINGS 1#include "game2048.h" void DisplayBoard (int board [ROW] [COL]) {int I = 0; int j = 0; printf ("tip:* key controls square movement * ESC key returns menu *\ n"); for (int j = 0; j
< COL; j++) printf("+-----"); printf("+\n"); for (int i = 0; i < ROW; i++) { for (int j= 0; j < COL; j++) { printf("|"); if (board[i][j]!=0) printf("%5d", board[i][j]); else printf(" "); } printf("|\n"); for (int j = 0; j < COL; j++) { printf("+-----"); } printf("+\n"); } } void init_num(int board[ROW][COL]){ int x, y; x = rand() % ROW; y = rand() % COL; board[x][y] = 2;//随机在一个位置生成2 x = rand() % ROW; y = rand() % COL; while (board[x][y] == 2) { x = rand() % ROW; y = rand() % COL; } board[x][y] = 2; return;} void put_num(int board[ROW][COL]){ int x = 0; int y = 0; int z = 0; x = rand() % ROW; y = rand() % COL; while (board[x][y] !=0) { x = rand() % ROW; y = rand() % COL; } z = rand() % 10; if (z 0 && y < COL-1) { if (board[j][i] == 0) { for (x = j; x >0; xMel -) board [x] [I] = board [x-1] [I]; board [0] [I] = 0; yearly + } else jmurmuri;} for (j = COL-1; j > 0 If (board [j] [I] = = board [j-1] [I] & & board [j] [I]! = 0) {board [j] [I] = board [j] [I] * 2; board [j-1] [I] = 0 } j = COL-1; y = 0; while (j > 0 & & y)
< COL - 1) { if (board[j][i] == 0) { for (x = j; x >0; xMel -) board [x] [I] = board [x-1] [I]; board [0] [I] = 0; yearly + } else jmurmuri;}} if (contrast (board) = = 0) put_num (board); else return } void left (int board [ROW] [COL]) {int I = 0; int j = 0; int x = 0; int y = 0; for (I = 0; I
< ROW; i++) { j = 0; y = 0; while (j < 3 && y < 3 ) { if (board[i][j] == 0) { for (x = j; x < ROW-1; x++) board[i][x] = board[i][x + 1]; board[i][COL-1] = 0; y++; } else j++; } for (j = 0; j < 3; j++) if (board[i][j] == board[i][j + 1] && board[i][j] != 0) { board[i][j] = board[i][j] * 2; board[i][j + 1] = 0; } j = 0; y = 0; while (j < 3 && y < 3) { if (board[i][j] == 0) { for (x = j; x < ROW - 1; x++) board[i][x] = board[i][x + 1]; board[i][COL - 1] = 0; y++; } else j++; } } if (contrast(board) == 0) put_num(board); else return;} void right(int board[ROW][COL]){ int i = 0; int j = 0; int x = 0; int y = 0; for (i = 0; i < 4; i++) { j = COL-1; y = 0; while (j>0 & & y
< COL-1) { if (board[i][j] == 0) { for (x = j; x >0; Xmuri -) board [I] [x] = board [I] [x-1]; board [I] [0] = 0; yellowing;} else Jmuri- } for (j = 3; j > 0; board -) if (board [I] [j] = = board [I] [j-1] & & board [I] [j]! = 0) {board [I] [j] = board [I] [j] * 2 Board [I] [j-1] = 0;} j = COL-1; y = 0; while (j > 0 & & y)
< COL - 1) { if (board[i][j] == 0) { for (x = j; x >0; Xmuri -) board [I] [x] = board [I] [x-1]; board [I] [0] = 0; yellowing;} else Jmuri- }} if (contrast (board) = = 0) put_num (board); else return;} int is_fail (int board [ROW] [COL]) {int I = 0; int j = 0; for (I = 0; I
< ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] == 0) return 0; if (i >0) {if (board [I-1] [j] = = board [I] [j]) return 0 } if (j > 0) {if (board [I] [j-1] = = board [I] [j]) return 0 } return 1;} int is_win (int board [ROW] [COL]) {int I = 0; int j = 0; int num = 0; for (I = 0; I
< ROW; i++) for (j = 0; j < COL; j++) { if (board[i][j] >Num) num = board [I] [j];} if (num > = 2048) return 1; else return 0;} void copyboard (int board [ROW] [COL], int copy [ROW] [COL]) {int I = 0; int j = 0; for (I = 0; I < ROW) For (j = 0; j < COL; jacks +) copy [I] [j] = board [I] [j];} int contrast (int board [ROW] [COL]) {int I = 0; int j = 0; for (I = 0; I < 4; iTunes +) for (j = 0; j < 4) Return +) if (copy [I] [j]! = board [I] [j]) return 0; return 1;}
Test.c
# define _ CRT_SECURE_NO_WARNINGS 1#include "game2048.h" void menu () {printf ("* *\ n"); printf ("* press 1 to start the game *\ n"); printf ("* press 0 to exit the game *\ n") Printf ("* *\ n");} void game2048 () {int board [ROW] [COL] = {{0}}; int control = 0; DisplayBoard (board); init_num (board); system ("cls"); / / clear screen, beautiful interface DisplayBoard (board) While ((control = _ getch ())! = 0x1b) {switch (control) {case 0xe0: switch (control = getch ()) {case 72: copyboard (board Copy) Up (board); break; case 80: copyboard (board, copy); down (board); break Case 75: copyboard (board, copy); left (board); break; case 77: copyboard (board, copy) Right (board); break; default: break;} system ("cls"); DisplayBoard (board) If (is_win (board) = = 1) {printf ("Congratulations on winning! \ n ");} if (is_fail (board) = = 1) {printf (" good food\ n ");} int main () {int input = 0; srand ((unsigned int) time (NULL) Do {menu (); printf ("Select:"); scanf ("% d", & input); switch (input) {case 1: game2048 (); break Case 0: printf ("quit the game, guests often come to play ~)\ n"); break; default: printf ("give you a chance to choose again\ n"); break }} while (input); return 0;} these are all the contents of the article "how to fully implement 2048 games in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.