断断续续自学了很久,没方向感。这几天写的垃圾2048,望指教需要通过学哪些东西来优化。
以这代码为例,如果想代码更清晰易懂,界面更美观,更友好,要编成类似手机上的《全民2048》那样需要学哪些,怎么学?程序代码:
/* 2048 */ /*方向键控制上下左右*/ #include <stdio.h> #include <stdlib.h> //包含生成随机数函数rand() #include <time.h> //为生成随机数提供种子 #include <conio.h> //包含getch() #include "windows.h" int shenchen (void); //生成随机数为2或者4 void zuobiao (int pa[][4]); //生成坐标,并填入数字 int pandan(const int a[][4], int ch); //判断用户选择的方向是否合法 int gameover (const int a[][4]); //判断游戏结束 void display(const int a[][4]); void w(int a[][4]); void s(int a[][4]); void a(int a[][4]); void d(int a[][4]); void display(const int a[][4]) { int i, j; for (i = 0; i < 4; i++) { puts("|-----+-----+-----+-----|"); printf("|"); for (j = 0; j < 4; j++) if (a[i][j] == 0) { printf("%5c", ' '); printf("|"); } else { printf("%5d", a[i][j]); printf("|"); } printf("\n"); if (i == 3) puts("|-----+-----+-----+-----|"); } } void zuobiao (int pa[][4]) { int x, y, i, j; int t = 0; //判断是否有空位 for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) if (pa[i][j] == 0) t = 1; if (t) break; } if (t) { do { x = rand() % 4; y = rand() % 4; } while(pa[x][y] != 0); //是否会出现坐标对应的值永远不等于0?? pa[x][y] = shenchen(); } } int shenchen (void) { int i; i = rand() % 10; if (i / 2 == 0) i = 4; else i = 2; return i; } int main (void) { int b[4][4] = {0}; //模拟4X4界面 int ch; //存储用户输入的字符 srand((unsigned)time(0)); //随机函数 zuobiao(b); zuobiao(b); display(b); while ((ch = getch()) != 27) { if (!pandan(b, ch)) ch = 'q'; switch (ch) { case 72 : w(b); zuobiao (b); break; case 80 : s(b); zuobiao (b); break; case 75 : a(b); zuobiao (b); break; case 77 : d(b); zuobiao (b); break; } if (!gameover(b)) { printf ("游戏结束"); break; } Sleep(200); system("CLS"); //清屏 display(b); } return 0; } void w (int a[][4]) { int i, j, k; //将4x4数组中不为0的数全部向上移 for (j = 0; j < 4; j++) for (i = 0; i < 3; i++) for (k = i+1; k < 4; k++) if (a[i][j] == 0 && a[k][j] != 0) { a[i][j] = a[k][j]; a[k][j] = 0; } //将同一个方向上相同的按顺序相加 j = 0; while (j < 4) { if (a[0][j] == a[1][j]) { a[0][j] *= 2; a[1][j] = 0; if (a[2][j] == a[3][j]) { a[1][j] = 2 * a[2][j]; a[2][j] = a[3][j] = 0; } else { a[1][j] = a[2][j]; a[2][j] = a[3][j]; a[3][j] = 0; } } else if (a[1][j] == a[2][j]) { a[1][j] *= 2; a[2][j] = a[3][j]; a[3][j] = 0; } else if (a[2][j] == a[3][j]) { a[2][j] *= 2; a[3][j] = 0; } j++; } } void s (int a[][4]) { int i, j, k; //将4x4数组中不为0的数全部向上移 for (j = 0; j < 4; j++) for (i = 3; i > 0; i--) for (k = i-1; k >= 0; k--) if (a[i][j] == 0 && a[k][j] != 0) { a[i][j] = a[k][j]; a[k][j] = 0; } //将同一个方向上相同的按顺序相加 j = 3; while (j >= 0) { if (a[3][j] == a[2][j]) { a[3][j] *= 2; a[2][j] = 0; if (a[1][j] == a[0][j]) { a[2][j] = 2 * a[1][j]; a[1][j] = a[0][j] = 0; } else { a[2][j] = a[1][j]; a[1][j] = a[0][j]; a[0][j] = 0; } } else if (a[2][j] == a[1][j]) { a[2][j] *= 2; a[1][j] = a[0][j]; a[0][j] = 0; } else if (a[1][j] == a[0][j]) { a[1][j] *= 2; a[0][j] = 0; } j--; } } void a (int a[][4]) { int i, j, k; for (i = 0; i < 4; i++) for (j = 0; j < 3; j++) for (k = j+1 ; k < 4; k++) if (a[i][j] == 0 && a[i][k] != 0) { a[i][j] = a[i][k]; a[i][k] = 0; } i = 0; while (i < 4) { if (a[i][0] == a[i][1]) { a[i][0] *= 2; a[i][1] = 0; if (a[i][2] == a[i][3]) { a[i][1] = 2 * a[i][2]; a[i][2] = a[i][3] = 0; } else { a[i][1] = a[i][2]; a[i][2] = a[i][3]; a[i][3] = 0; } } else if (a[i][1] == a[i][2]) { a[i][1] *= 2; a[i][2] = a[i][3]; a[i][3] = 0; } else if (a[i][2] == a[i][3]) { a[i][2] *= 2; a[i][3] = 0; } i++; } } void d (int a[][4]) { int i, j, k; for (i = 0; i < 4; i++) for (j = 3; j > 0; j--) for (k = j-1 ; k >= 0; k--) if (a[i][j] == 0 && a[i][k] != 0) { a[i][j] = a[i][k]; a[i][k] = 0; } i = 3; while (i >= 0) { if (a[i][2] == a[i][3]) { a[i][3] *= 2; a[i][2] = 0; if (a[i][0] == a[i][1]) { a[i][2] = 2 * a[i][1]; a[i][1] = a[i][0] = 0; } else { a[i][2] = a[i][1]; a[i][1] = a[i][0]; a[i][0] = 0; } } else if (a[i][1] == a[i][2]) { a[i][2] *= 2; a[i][1] = a[i][0]; a[i][0] = 0; } else if (a[i][0] == a[i][1]) { a[i][1] *= 2; a[i][0] = 0; } i--; } } int pandan (const int a[][4], int ch) { int i, j, t; switch (ch) { case 72 : for (j = 0; j < 4; j++) { for (i = 0; i < 3; i++) { if (a[i][j] == 0) if(a[i+1][j] > 0) t = 1; else t = 0; else if(a[i+1][j] == a[i][j]) t = 1; else t = 0; if (t == 1) break; } if (t == 1) break; } break; case 80 : for (j = 0; j < 4; j++) { for (i = 3; i > 0; i--) { if (a[i][j] == 0) if(a[i-1][j] > 0) t = 1; else t = 0; else if(a[i-1][j] == a[i][j]) t = 1; else t = 0; if (t == 1) break; } if (t == 1) break; } break; case 75 : for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { if (a[i][j] == 0) if(a[i][j+1] > 0) t = 1; else t = 0; else if(a[i][j+1] == a[i][j]) t = 1; else t = 0; if (t == 1) break; } if (t == 1) break; } break; case 77 : for (i = 0; i < 4; i++) { for (j = 3; j > 0; j--) { if (a[i][j] == 0) if(a[i][j-1] > 0) t = 1; else t = 0; else if(a[i][j-1] == a[i][j]) t = 1; else t = 0; if (t == 1) break; } if (t == 1) break; } break; default : t = 0; } return t; } int gameover (const int a[][4]) { int i, j; for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) if (a[i][j] == 0) return 1; for (i = 0; i < 4; i++) for (j = 0; j < 3; j++) if (a[i][j] == a[i][j+1]) return 1; for (i = 0; i < 4; i++) for (j = 0; j < 3; j++) if (a[j][i] == a[j+1][i]) return 1; return 0; }