欢迎来到英杰社区https://bbs.csdn.net/topics/617804998
一、游戏描述:
2048是一款数字益智类游戏,玩家需要使用键盘控制数字方块的移动,合并相同数字的方块,最终达到数字方块上出现“2048”的目标。
每次移动操作,所有数字方块会朝着指定方向同时滑动,并在靠近边界或其他数字方块时停止。如果两个相邻的数字方块数字相同,则它们会合并成一个方块,数字值为原先相同方块的数字值相加。
二、效果展示
三、代码讲解:
实现了一个2048游戏的基本功能,包括初始化游戏界面、移动方块、放置新数字以及判定胜负等功能:
DisplayBoard(int board[ROW][COL])
:初始化并打印游戏界面。这个函数用于显示游戏的当前状态,包括棋盘上的数字和空格。
init_num(int board[ROW][COL])
:在游戏开始时随机生成两个数字2。这个函数用于初始化游戏棋盘,给棋盘上的两个随机位置赋值为2。
put_num(int board[ROW][COL])
:在每次移动后随机放置一个数字2或4。这个函数用于在每次玩家移动后,在空白的位置随机生成一个数字2或4。
up(int board[ROW][COL])
、down(int board[ROW][COL])
、left(int board[ROW][COL])
、right(int board[ROW][COL])
:分别实现了向上、向下、向左、向右移动的功能。这些函数用于处理玩家的移动操作,更新棋盘上的数字位置。
is_win(int board[ROW][COL])
:判断是否获胜。这个函数用于检查游戏是否达到了2048,如果有一个位置的值为2048,则游戏获胜。
is_fail(int board[ROW][COL])
:判断是否失败。这个函数用于检查游戏是否失败,即所有位置都被填满且无法进行有效的移动操作。
#pragma once#include <stdio.h>#include <time.h>#include <stdlib.h>#include <windows.h>#include<conio.h>#define ROW 4#define COL 4const int copy[ROW][COL];//初始化并打印游戏界面void DisplayBoard(int board[ROW][COL]);//开局随机生成两个2void init_num(int board[ROW][COL]);//在移动后随机放置数字2或4void put_num(int board[ROW][COL]);//移动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]);
显示2048游戏的棋盘界面
打印横向的分隔线,使用"+"和"-"来表示。遍历每个格子,如果格子上有数字,则打印该数字并确保宽度为5个字符,右对齐;如果格子上没有数字,则打印5个空格。在每行末尾打印"|"再次打印横向的分隔线,使用"+"和"-"来表示。
void DisplayBoard(int board[ROW][COL]){int i = 0;int j = 0;printf("tip:*方向键控制方块移动*ESC键返回菜单*\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]);elseprintf(" ");}printf("|\n");for (int j = 0; j < COL; j++){printf("+-----");}printf("+\n");}}
初始化游戏棋盘:
生成一个随机的行索引
x
和列索引 y
,确保它们在棋盘范围内。在随机选择的位置 (x, y)
上放置一个数字2。再次生成一个随机的行索引 x
和列索引 y
,确保它们与第一个位置不重复。在新的随机选择的位置 (x, y)
上放置另一个数字2。 void init_num(int board[ROW][COL]){int x, y;x = rand() % ROW;y = rand() % COL;board[x][y] = 2;//随机在一个位置生成2x = rand() % ROW;y = rand() % COL;while (board[x][y] == 2){x = rand() % ROW;y = rand() % COL;}board[x][y] = 2;return;}
空白位置随机放置一个数字:
生成一个随机的行索引x
和列索引 y
,确保它们在棋盘范围内。如果在选择的位置 (x, y)
上已经有数字,则继续随机选择位置,直到找到一个空白位置。生成一个随机数 z
,范围为0到9之间的整数。如果 z
小于9,则在当前空白位置 (x, y)
放置数字2;如果 z
等于9,则放置数字4。 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 < 9)board[x][y] = 2;elseboard[x][y] = 4;return;}
简单的 2048 游戏的移动和合并操作
上下左右四个方向的移动操作:up、down、left、right
每个函数都按照相应的方向进行移动并合并相同的数字。
在每个函数中,首先进行移动操作,然后进行合并操作,再次进行移动操作,并最后检查游戏是否可以继续进行(通过contrast(board)
函数判断)。如果游戏可以继续,则调用put_num(board)
函数来生成新的数字;否则直接返回。
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++;}elsej++;}//合并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++;}elsej++;}}if (contrast(board) == 0)put_num(board);elsereturn;}void down(int board[ROW][COL]){int i = 0;int j = 0;int x = 0;int y = 0;for (i = 0; i < ROW; i++){j = COL - 1;y = 0;while (j > 0 && y < COL - 1){if (board[j][i] == 0){for (x = j; x > 0; x--)board[x][i] = board[x - 1][i];board[0][i] = 0;y++;}elsej--;}for (j = COL - 1; j > 0; 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 = COL - 1;y = 0;while (j > 0 && y < COL - 1){if (board[j][i] == 0){for (x = j; x > 0; x--)board[x][i] = board[x - 1][i];board[0][i] = 0;y++;}elsej--;}}if (contrast(board) == 0)put_num(board);elsereturn;}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++;}elsej++;}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++;}elsej++;}}if (contrast(board) == 0)put_num(board);elsereturn;}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; x--)board[i][x] = board[i][x - 1];board[i][0] = 0;y++;}else j--;}for (j = 3; j > 0; 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 = COL - 1;y = 0;while (j > 0 && y < COL - 1){if (board[i][j] == 0){for (x = j; x > 0; x--)board[i][x] = board[i][x - 1];board[i][0] = 0;y++;}else j--;}}if (contrast(board) == 0)put_num(board);elsereturn;}
判断在 2048 游戏中是否无法继续移动
游戏板上还有空格(值为0),即游戏可以继续;相邻的方块(上下或左右相邻)有相同的值,即游戏可以继续合并。如果以上任意情况成立,函数将返回0,表示游戏可以继续;否则,如果所有方块都被填满且没有相邻方块的值相同,函数将返回1,表示游戏无法继续移动,游戏结束。
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;}
检查在2048游戏中是否获胜
该函数遍历整个游戏板,找到其中的最大值。如果最大值达到或超过2048,函数将返回1,表示玩家已经获胜;否则返回0,表示游戏尚未获胜。
在2048游戏中,玩家的目标是通过合并方块使得其中一个方块的值达到2048。一旦某个方块的值达到2048,玩家就获胜了。
这个函数的作用是在每一步移动后检查游戏是否已经达到了获胜条件。如果返回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;elsereturn 0;}
copyboard
函数的作用是将一个二维数组 board
中的元素复制到另一个二维数组 copy
中。通常在编程中,这样的函数用于保存一个数组的副本,以便后续的比较或者其他操作而不影响原始数组。
void copyboard(int board[ROW][COL], int copy[ROW][COL]){int i = 0;int j = 0;for (i = 0; i < ROW; i++)for (j = 0; j < COL; j++)copy[i][j] = board[i][j];}
菜单功能:
void menu(){printf("**************************\n");printf("**************************\n");printf("**************************\n");printf("*****按 1 开 始 游 戏*****\n");printf("*****按 0 退 出 游 戏*****\n");printf("**************************\n");printf("**************************\n");printf("**************************\n");}
主函数:
int main(){int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请选择:");scanf("%d", &input);switch (input){case 1:game2048();break;case 0:printf("退出游戏)\n");break;default:printf("再次进入游戏\n");break;}} while (input);return 0;}
完整代码:
Test.c
#define _CRT_SECURE_NO_WARNINGS 1#include "game2048.h"void menu(){printf("**************************\n");printf("**************************\n");printf("**************************\n");printf("*****按 1 开 始 游 戏*****\n");printf("*****按 0 退 出 游 戏*****\n");printf("**************************\n");printf("**************************\n");printf("**************************\n");}void game2048(){int board[ROW][COL] = { {0} };int control = 0;DisplayBoard(board);init_num(board);system("cls"); //清屏,美观界面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("恭喜你赢了!\n");}if (is_fail(board) == 1){printf("抱歉,您未能通关\n");}}}}int main(){int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请选择:");scanf("%d", &input);switch (input){case 1:game2048();break;case 0:printf("退出游戏)\n");break;default:printf("再次进入游戏\n");break;}} while (input);return 0;}
game.c
#define _CRT_SECURE_NO_WARNINGS 1#define _CRT_SECURE_NO_WARNINGS 1#include "game2048.h"void DisplayBoard(int board[ROW][COL]){int i = 0;int j = 0;printf("tip:*方向键控制方块移动*ESC键返回菜单*\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]);elseprintf(" ");}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;//随机在一个位置生成2x = 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 < 9)board[x][y] = 2;elseboard[x][y] = 4;return;}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++;}elsej++;}//合并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++;}elsej++;}}if (contrast(board) == 0)put_num(board);elsereturn;}void down(int board[ROW][COL]){int i = 0;int j = 0;int x = 0;int y = 0;for (i = 0; i < ROW; i++){j = COL - 1;y = 0;while (j > 0 && y < COL - 1){if (board[j][i] == 0){for (x = j; x > 0; x--)board[x][i] = board[x - 1][i];board[0][i] = 0;y++;}elsej--;}for (j = COL - 1; j > 0; 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 = COL - 1;y = 0;while (j > 0 && y < COL - 1){if (board[j][i] == 0){for (x = j; x > 0; x--)board[x][i] = board[x - 1][i];board[0][i] = 0;y++;}elsej--;}}if (contrast(board) == 0)put_num(board);elsereturn;}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++;}elsej++;}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++;}elsej++;}}if (contrast(board) == 0)put_num(board);elsereturn;}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; x--)board[i][x] = board[i][x - 1];board[i][0] = 0;y++;}else j--;}for (j = 3; j > 0; 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 = COL - 1;y = 0;while (j > 0 && y < COL - 1){if (board[i][j] == 0){for (x = j; x > 0; x--)board[i][x] = board[i][x - 1];board[i][0] = 0;y++;}else j--;}}if (contrast(board) == 0)put_num(board);elsereturn;}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;elsereturn 0;}void copyboard(int board[ROW][COL], int copy[ROW][COL]){int i = 0;int j = 0;for (i = 0; i < ROW; i++)for (j = 0; j < COL; j++)copy[i][j] = board[i][j];}int contrast(int board[ROW][COL]){int i = 0;int j = 0;for (i = 0; i < 4; i++)for (j = 0; j < 4; j++)if (copy[i][j] != board[i][j])return 0;return 1;}