下午写实验报告的时候学了几个C++知识点,明白了一个道理:C++课程知识十分重要,并且对ACM竞赛的帮助可能一直被我低估了。之前我对作业课程的重视程度其实不够高(虽然相比较于高数、物理之类积极很多)。明天开始,一天写一篇实验报告,并且学习相关知识。
新学+复习的知识:
一、::
作用域运算符的功能
第一种用来表示全局变量。
int a = 2;
int main()
{
int a = 3;
cout << a * a << ' ' << a * ::a << endl;
return 0;
}
第二种类作用不说了,针对继承类两个同名变量的使用时。
二、引用与动态空间的分配以及释放
每次复习了很快又忘了。希望这次能记得。
new返回的是指针。
三、unsigned修饰符
emmm没什么好说的,太菜了,今天才知道具体使用方式(感觉没什么用。。)。
C语言2048小游戏的优化
在学校的时候写过一次,那时候的自己太年轻了,写的很多bug,今天随便改了一下,至少功能方面应该没bug了,优化什么的就算了,没什么意义。
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int mp[4][4];
char temp[100];
void color(short x) //自定义函根据参数改变颜色
{
if (x >= 0 && x <= 15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
int main()
{
int score = 0; //计算分数
int i, j, t, x, y, if_end = 10, level;
srand(time(NULL));
printf(" ——————————\n"); //打印界面
printf("| 请选择游戏难度: |\n");
printf("| 1.简单模式(2048) |\n");
printf("| 2.困难模式(4096) |\n");
printf("|输入序号按下回车确认|\n");
printf(" ——————————\n");
scanf("%d", &level);
if (level == 1) //选择难度
level = 2048;
else if (level == 2)
level = 4096;
printf("请按任意方向键↑↓ ← →开始游戏\n");
for (t = 0; t < 10000; t++)
{
int if_Victory = 0;
int ch1 = 0;
int ch2 = 0;
if (ch1 = getch())
ch2 = getch();
int sum = 0;
if (t == 0)
sum = 1;
if (ch2 == 72) //上
{
system("cls");
for (i = 1; i <= 3; i++)
{
for (j = 0; j <= 3; j++)
{
if (mp[i - 1][j] == 0 && mp[i][j] != 0)
{
mp[i - 1][j] = mp[i][j];
mp[i][j] = 0;
}
}
}
for (i = 1; i <= 3; i++)
{
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == mp[i - 1][j])
{
mp[i][j] = 0;
mp[i - 1][j] *= 2;
}
}
}
for (i = 1; i <= 3; i++)
{
for (j = 0; j <= 3; j++)
{
if (mp[i - 1][j] == 0 && mp[i][j] != 0)
{
mp[i - 1][j] = mp[i][j];
mp[i][j] = 0;
}
}
}
int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) //如果只剩一个空的格子了,那么只需随机生成一个数
geshu++;
while (geshu != 2 && geshu != if_end - 1 && sum != 0)
{
x = rand() % 4;
y = rand() % 4; //生成的随机数的坐标
if (mp[x][y] == 0)
{
int tempp = rand() % 2; //随机生成2或4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("———————————\n"); //打印地图
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\n———————————\n");
}
if (!if_end)
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1 >= 0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1 >= 0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1 <= 3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1 <= 3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; //计算分数
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(" score=%-6d\n ——————\n", score);
color(20); //取消颜色
if (if_Victory == 1)
{
printf(" 恭喜你!游戏成功!\n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf(" 游戏结束!\n");
gets(temp);
gets(temp);
}
}
if (ch2 == 80) //下
{
system("cls");
for (i = 2; i >= 0; i--)
{
for (j = 0; j <= 3; j++)
{
if (mp[i + 1][j] == 0 && mp[i][j] != 0)
{
mp[i + 1][j] = mp[i][j];
mp[i][j] = 0;
}
}
}
for (i = 2; i >= 0; i--)
{
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == mp[i + 1][j])
{
mp[i][j] = 0;
mp[i + 1][j] *= 2;
}
}
}
for (i = 2; i >= 0; i--)
{
for (j = 0; j <= 3; j++)
{
if (mp[i + 1][j] == 0 && mp[i][j] != 0)
{
mp[i + 1][j] = mp[i][j];
mp[i][j] = 0;
}
}
}
int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) //如果只剩一个空的格子了,那么只需随机生成一个数
geshu++;
while (geshu != 2 && geshu != if_end - 1 && sum != 0)
{
x = rand() % 4;
y = rand() % 4; //生成的随机数的坐标
if (mp[x][y] == 0)
{
int tempp = rand() % 2; //随机生成2或4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("———————————\n"); //打印地图
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\n———————————\n");
}
if (!if_end)
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1 >= 0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1 >= 0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1 <= 3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1 <= 3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; //计算分数
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(" score=%-6d\n ——————\n", score);
color(20); //取消颜色
if (if_Victory == 1)
{
printf(" 恭喜你!游戏成功!\n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf(" 游戏结束!\n");
gets(temp);
gets(temp);
}
}
if (ch2 == 75) //左
{
system("cls");
for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j - 1] == 0 && mp[i][j] != 0)
{
mp[i][j - 1] = mp[i][j];
mp[i][j] = 0;
}
}
}
for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j] == mp[i][j - 1])
{
mp[i][j] = 0;
mp[i][j - 1] *= 2;
}
}
}
for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j - 1] == 0 && mp[i][j] != 0)
{
mp[i][j - 1] = mp[i][j];
mp[i][j] = 0;
}
}
}
int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) //如果只剩一个空的格子了,那么只需随机生成一个数
geshu++;
while (geshu != 2 && geshu != if_end - 1 && sum != 0)
{
x = rand() % 4;
y = rand() % 4; //生成的随机数的坐标
if (mp[x][y] == 0)
{
int tempp = rand() % 2; //随机生成2或4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("———————————\n"); //打印地图
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\n———————————\n");
}
if (!if_end)
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1 >= 0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1 >= 0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1 <= 3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1 <= 3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; //计算分数
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(" score=%-6d\n ——————\n", score);
color(20); //取消颜色
if (if_Victory == 1)
{
printf(" 恭喜你!游戏成功!\n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf(" 游戏结束!\n");
gets(temp);
gets(temp);
}
}
if (ch2 == 77) //右
{
system("cls");
for (j = 2; j >= 0; j--)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j + 1] == 0 && mp[i][j] != 0)
{
mp[i][j + 1] = mp[i][j];
mp[i][j] = 0;
}
}
}
for (j = 2; j >= 0; j--)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j] == mp[i][j + 1])
{
mp[i][j] = 0;
mp[i][j + 1] *= 2;
}
}
}
for (j = 2; j >= 0; j--)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j + 1] == 0 && mp[i][j] != 0)
{
mp[i][j + 1] = mp[i][j];
mp[i][j] = 0;
}
}
}
int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) //如果只剩一个空的格子了,那么只需随机生成一个数
geshu++;
while (geshu != 2 && geshu != if_end - 1 && sum != 0)
{
x = rand() % 4;
y = rand() % 4; //生成的随机数的坐标
if (mp[x][y] == 0)
{
int tempp = rand() % 2; //随机生成2或4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("———————————\n"); //打印地图
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\n———————————\n");
}
if (!if_end)
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1 >= 0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1 >= 0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1 <= 3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1 <= 3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; //计算分数
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(" score=%-6d\n ——————\n", score);
color(20); //取消颜色
if (if_Victory == 1)
{
printf(" 恭喜你!游戏成功!\n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf(" 游戏结束!\n");
gets(temp);
gets(temp);
}
}
}
return 0;
}