diff --git a/saolei.cpp b/saolei.cpp new file mode 100644 index 0000000..b2a48cb --- /dev/null +++ b/saolei.cpp @@ -0,0 +1,438 @@ +#include +#include +#include +#include +int map[4][4] = { 0 }; +int score = 0; +int movenum = -1; +char moveinput; +int gameover = 1; +int change = 1; +void randomnum()//产生随机数 +{ + int i, j, n; + if (change == 1) + { + do + { + i = ((unsigned int)rand()) % 4; + j = ((unsigned int)rand()) % 4; + } while (map[i][j] != 0); + n = (unsigned int)rand() % 2; + if (n == 0) + { + map[i][j] = n + 2; + } + else if (n == 1) + { + map[i][j] = n + 3; + } + movenum++; + } +} +int up() +{ + int x1 = 0, x2 = 0;//x1为当前选定的元素,x2下一次移动处的元素 + int k = 0; + for (int j = 0; j < 4; j++)//j代表列 + { + for (int i = 0; i < 4; i++)//i代表行 + { + x1 = map[i][j]; + if (x1 != 0)//判断两元素是否相等,相等则相加 + { + k = i + 1; + while (k < 4) + { + x2 = map[k][j]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[k][j];//移动的数字即为增加的分数 + map[i][j] = map[k][j] * 2; + map[k][j] = 0; + } + } + break; + } + k++; + } + } + for (int i = 0; i < 4; i++)//对一列中所有相同元素进行合并 + { + x1 = map[i][j]; + if (x1 != 0) + { + k = i + 1; + while (k < 4) + { + x2 = map[k][j]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[k][j]; + map[i][j] = map[k][j] * 2; + map[k][j] = 0; + } + } + break; + } + k++; + } + } + } + for (int j = 0; j < 4; j++)//将非零元素进行移动 + { + for (int i = 0; i < 4; i++) + { + x1 = map[i][j]; + if (x1 == 0) + { + k = i + 1; + while (k < 4) + { + x2 = map[k][j]; + if (x2 != 0) + { + change = 1; + map[i][j] = map[k][j]; + map[k][j] = 0; + k = 4;//停止循环 + } + k++; + } + } + } + } + return change; +} +int down() +{ + int x1 = 0, x2 = 0; + int k = 0; + for (int j = 0; j < 4; j++) + { + for (int i = 3; i >= 0; i--) + { + x1 = map[i][j]; + if (x1 != 0) + { + k = i - 1; + while (k >= 0) + { + x2 = map[k][j]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[k][j]; + map[i][j] = map[k][j]*2; + map[k][j] = 0; + } + break; + } + k--; + } + } + } + for (int i = 3; i >= 0; i--) + { + x1 = map[i][j]; + if (x1 != 0) + { + k = i - 1; + while (k >= 0) + { + x2 = map[k][j]; + if (x2 != 0) + { + if (x2 == x1) + { + change = 1; + score += map[k][j]; + map[i][j] = map[k][j] * 2; + map[k][j] = 0; + } + break; + } + k--; + } + } + } + } + for (int j = 0; j < 4; j++) + { + for (int i = 3; i >= 0; i--) + { + x1 = map[i][j]; + if (x1 == 0) + { + k = i - 1; + while (k >= 0) + { + x2 = map[k][j]; + if (x2 != 0) + { + change = 1; + map[i][j] = map[k][j]; + map[i][j] = map[k][j]; + map[k][j] = 0; + break; + } + k--; + } + } + } + } + return change; +} +int left() +{ + int x1 = 0, x2 = 0; + int k = 0; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + x1 = map[i][j]; + if (x1 != 0) + { + k = i + 1; + while (k <= 4) + { + x2 = map[i][k]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[i][k]; + map[i][j] = 2 * map[i][k]; + map[i][k] = 0; + } + break; + } + k++; + } + } + } + for (int j = 0; j < 4; j++) + { + x1 = map[i][j]; + if (x1 != 0) + { + k = i + 1; + while (k <= 4) + { + x2 = map[i][k]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[i][k]; + map[i][j] = 2 * map[i][k]; + map[i][k] = 0; + } + break; + } + k++; + } + } + } + } + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + x1 = map[i][j]; + if (x1 == 0) + { + k = j + 1; + while (k < 4) + { + x2 = map[i][k]; + if (x2 != 0) + { + change = 1; + map[i][j] = map[i][k]; + map[i][k] = 0; + break; + } + k++; + } + } + } + } + return change; +} +int right() +{ + int x1 = 0, x2 = 0; + int k = 0; + for (int i = 0; i < 4; i++) + { + for (int j = 3; j >= 0; j--) + { + x1 = map[i][j]; + if (x1 != 0) + { + k = j - 1; + while (k >= 0) + { + x2 = map[i][k]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[i][k]; + map[i][j] = 2 * map[i][k]; + map[i][k] = 0; + } + break; + } + k--; + } + } + } + for (int j = 3; j >= 0; j--) + { + x1 = map[i][j]; + if (x1 != 0) + { + k = j - 1; + while (k >= 0) + { + x2 = map[i][k]; + if (x2 != 0) + { + if (x1 == x2) + { + change = 1; + score += map[i][k]; + map[i][j] = 2 * map[i][k]; + map[i][k] = 0; + } + break; + } + k--; + } + } + } + } + for (int i = 0; i < 4; i++) + { + for (int j = 3; j >= 0; j--) + { + x1 = map[i][j]; + if (x1 == 0) + { + k = j - 1; + while (k >= 0) + { + x2 = map[i][k]; + if (x2 != 0) + { + change = 1; + map[i][j] = map[i][k]; + map[i][k] = 0; + break; + } + k--; + } + } + } + } + return change; +} +void mapprint()//打印棋盘 +{ + system("CLS"); + printf("*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*\n"); + printf("up-w down-s left-a right-d\n"); + printf("*移动次数:%d * 分数:%d\n", movenum, score); + printf("*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*—*\n");//打印棋盘 + printf("*____________________________*\n"); + for (int i = 0; i < 4; i++) + { + printf(" |"); + for (int j = 0; j < 4; j++) + { + if (map[i][j] == 0) + { + printf(" |"); + } + else + { + printf("%3d|", map[i][j]); + } + } + printf("\n"); + printf("*____________________________*\n"); + } +} +void move()//进行上下左右的移动 +{ + moveinput = _getch(); + change = 0; + switch (moveinput) + { + case'w': up(); break; + case's':down(); break; + case'a':left(); break; + case'd':right(); break; + } +} +void over() +{ + gameover = 0; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + if (map[i][j] == 0) + { + gameover = 1; + } + if (i >= 1) + { + if (map[i][j] == map[i - 1][j]) + { + gameover = 1; + } + } + if (j >= 1) + { + if (map[i][j] == map[i][j - 1]) + { + gameover = 1; + } + } + } + } +} +void quest() +{ + srand((unsigned int)time(0)); + while (gameover == 1) + { + randomnum(); + mapprint(); + move(); + over(); + } +} +int main() +{ + quest(); + printf("Game over \n"); + printf("你的得分为:%d\n", score); + printf("移动次数为:%d\n", movenum); + return 0; +} \ No newline at end of file