运行通过环境:Dev C++,VS2019
运用二维数组的方式,实现了2048的逻辑,框的显示,结束时输出得分;
未实现菜单功能,每次新生成的是2,为上下左右操作都写了一个函数,感觉有些不精简。但是仍只有三百行代码
#include
#include"conio.h"
#include
#include
using namespace std;
/*2048游戏编写*/
int arr[6][6] = { {-6,-5,-5,-5,-5,-6},
{-6,0,0,0,0,-6},
{-6,0,0,0,0,-6},
{-6,0,0,2,0,-6},
{-6,0,0,0,0,-6},
{-6,-5,-5,-5,-5,-6},
};
bool k = false;//成功失败bool值,如果上下左右都无法移动则失败
int main() {
void action();//操作
void tiShi();//提示函数
tiShi();
action();
return 0;
}
void map() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (arr[i][j] == -5) {
cout << setw(4) << "■■";//打印边框
}
else if (arr[i][j] == -6) {//打印边框
cout << "■";
}
else if (arr[i][j] == 0) {//数为0时显示空格
cout << setw(4) <<" ";
}
else {
cout << setw(4) << arr[i][j];//其它打印该数
}
}
cout << endl;
}
}
void generate() {//生成函数,生成2
bool k = false;
int i=0, j=0;
for (i = 1;i<5;i++) {
for ( j = 1;j<5;j++) {
if (arr[i][j]==0) {
k = true;
break;
}
}
if (k) break;
}
//生成
while(k) {
if (arr[1][2] == 0) {
arr[1][2] = 2;
break;
}
if (arr[1][3] == 0) {
arr[1][3] = 2;
break;
}
if (arr[4][2] == 0) {
arr[4][2] = 2;
break;
}
if (arr[4][3] == 0) {
arr[4][3] = 2;
break;
}
else {
arr[i][j] = 2;
break;
}
}
}
void action() {
void judge();
void map();//地图函数声明
void perform_w();//声明
void perform_s();//下
void perform_a();//左
void perform_d();//右
map();
char ch;//从键盘获取一个字符
while (1) {
ch = _getch();
switch (ch) {
case 'w':
case 'W':
perform_w();
generate();//生成
break;
case 's':
case 'S':
perform_s();
generate();
break;
case 'A':
case 'a':
perform_a();
generate();
break;
case 'd':
case 'D':
perform_d();
generate();
break;
case 't':
case 'T':
exit(0);
break;
}
system("cls");
map();
judge();//判断
k = false;//行动一次后初始化k
}
}
void perform_w() {
for (int i = 1; i < 5; i++) {
for (int j = 4; j >1;j--) {
if (arr[j-1][i] == 0&&arr[j][i]!=0) {//上面如果是0,则上移
arr[j - 1][i] = arr[j][i];
arr[j][i] = 0;
}
if (arr[j][i]==arr[j-1][i]) { //如果两个位置数一样
arr[j - 1][i] = 2 * arr[j - 1][i];//上一个数加倍
arr[j][i] = 0;//现在数为0
}
}
}
//移位
for (int i = 1; i < 5; i++) {
for (int j = 4; j > 1; j--) {
if (arr[j - 1][i] == 0 && arr[j][i] != 0) {//上面如果是0,则上移
arr[j - 1][i] = arr[j][i];
arr[j][i] = 0;
}
}
if (arr[2][i] == 0) {//如果二号位是0
arr[2][i] = arr[3][i];
arr[3][i] = 0;
}
}
}
//下
void perform_s() {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (arr[j +1][i] == 0) {//下面如果是0,则下移
arr[j +1][i] = arr[j][i];
arr[j][i] = 0;
}
if (arr[j][i] == arr[j + 1][i]) {
arr[j +1][i] = 2 * arr[j + 1][i];
arr[j][i] = 0;
}
}
}
//移位
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (arr[j + 1][i] == 0&&arr[j][i]!=0) {//下面如果是0,则下移
arr[j + 1][i] = arr[j][i];
arr[j][i] = 0;
}
}
if (arr[3][i] == 0) {//如果二号位是0
arr[3][i] = arr[2][i];
arr[2][i] = 0;
}
}
}
//左操作
void perform_a() {
for (int i = 1; i < 5; i++) {
for (int j = 4; j > 1; j--) {
if (arr[i][j-1] == 0) {//下面如果是0,则下移
arr[i][j-1] = arr[i][j];
arr[i][j] = 0;
}
if (arr[i][j] == arr[i][j-1]) {
arr[i][j-1] = 2 * arr[i][j-1];
arr[i][j] = 0;
}
}
}
//移位
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (arr[i][j - 1] == 0) {//下面如果是0,则下移
arr[i][j - 1] = arr[i][j];
arr[i][j] = 0;
}
}
if (arr[i][2] == 0) {//如果二号位是0
arr[i][2] = arr[i][3];
arr[i][3] = 0;
}
}
}
void perform_d() {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 4; j++) {
if (arr[i][j + 1] == 0) {//下面如果是0,则下移
arr[i][j + 1] = arr[i][j];
arr[i][j] = 0;
}
if (arr[i][j] == arr[i][j + 1]) {
arr[i][j + 1] = 2 * arr[i][j + 1];
arr[i][j] = 0;
}
}
}
//移位
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (arr[i][j + 1] == 0) {//下面如果是0,则下移
arr[i][j + 1] = arr[i][j];
arr[i][j] = 0;
}
}
if (arr[i][3] == 0) {//如果二号位是0
arr[i][3] = arr[i][2];
arr[i][2] = 0;
}
}
}
void judge() {
//思路:其中的任何一个数不等于它上下左右的一个数
//且地图中没有0
bool k1=false;//判断地图是否有0
for (int i = 1; i < 5; i++) {
for (int j=1;j<5;j++) {
if (arr[i][j] == 0) {
k = true;
break;
}
if (arr[i][j] == arr[i + 1][j] || arr[i][j] == arr[i - 1][j] ||
arr[i][j] == arr[i][j + 1] || arr[i][j] == arr[i][j - 1]) {
k = true;
break;
}
}
if (k == true) break;
//if (k1 == true) break;
}
if (k == false) {
int score = 0;//得分
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
score += arr[i][j];
}
}
cout << endl << "------------游戏结束------------" << endl;
cout << "----------—得分:" << score << "------------" << endl;
cout << "---------期待你的更高分---------" << endl;
for (int c = 0;c<5;c++) {//执行结束时,用继续输入的方式,保持窗口不关闭
cin >> c;
}
exit(0);//退出
}
}
void tiShi() {
cout << "***------>欢迎游玩2048益智小游戏<-------***" << endl;
cout << "*** w:上滑s:下滑 a:左滑 d:右滑 ***" << endl;
cout << "*** 按T退出 ***" << endl;
cout << "*** 敬请期待更新 ***" << endl;
cout << "*** 2021/4/22 ***" << endl;
cout << "*******************************************" << endl;
}
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
2048是款益智小游戏,基本原理就是向一个方向移动,相同合并,每次移动,就新生成一个2或4.直到不能动为止。
需要的知识只有二维数组和函数。
本程序全由博主凭个人理解敲的,其中逻辑肯定不是最优解,但胜在通俗易懂,如果喜欢,点个赞