首页 > 代码库 > 2048小游戏(C语言版)

2048小游戏(C语言版)

  1 #include <climits>  2 #include <cstdio>  3 #include <cstring>  4 #include <stack>  5 #include <string>  6 #include <map>  7 #include <vector>  8 #include <cmath>  9  10  11 /* 12 玩法说明:编译运行后,输入一个整数n,表示自己想玩的游戏中是n*n的表格,现在手机上或者电脑上一般都是4*4的,所以我们可以输入4,回车就可以开始游戏了。 13  14 游戏开始后a s d w表示方向左 下 右 上四个方向。  15  16 正如真实版的2048一样,会输出两个表格,move 表格显示按输入方向平移后的结果,fill 表格显示又随机填充一个格子后的表格 17 */ 18 using namespace std; 19  20 const int MAXX = 10; 21  22 int box[MAXX][MAXX]; 23  24 void moveLeft(int n, bool &ret){ 25     for (int i = 0; i < n; ++i){ 26         for (int j = 0; j < n; ++j){ 27             int k; 28             for (k = j - 1; k >= 0; --k){ 29                 if (box[i][k] != -1){ 30                     break; 31                 } 32             } 33             box[i][k + 1] = box[i][j]; 34  35             if ((k + 1) != j){ 36                 box[i][j] = -1; 37                 ret = true; 38             } 39         } 40     } 41 } 42  43 void mergeLeft(int n, bool &ret){ 44     for (int i = 0; i < n; ++i){ 45         for (int j = 0; j < n - 1; ++j){ 46             if (box[i][j] == -1)continue; 47  48             if (box[i][j] == box[i][j + 1]){ 49                 box[i][j] *= 2; 50                 box[i][j + 1] = -1; 51                 ret = true; 52                 ++j; 53             } 54         } 55     } 56 } 57  58 bool left(int n){ 59     bool ret = false; 60     moveLeft(n, ret); 61     mergeLeft(n, ret); 62     moveLeft(n, ret); 63     return ret; 64 } 65  66 void moveRight(int n, bool &ret){ 67     for (int i = 0; i < n; ++i){ 68         for (int j = n - 1; j >= 0; --j){ 69             if (box[i][j] == -1)continue; 70  71             int k; 72             for (k = j + 1; k < n; ++k){ 73                 if (box[i][k] != -1){ 74                     break; 75                 } 76             } 77  78             box[i][k - 1] = box[i][j]; 79             if ((k - 1) != j){ 80                 box[i][j] = -1; 81                 ret = true; 82             } 83         } 84     } 85 } 86  87 void mergeRight(int n, bool &ret){ 88     for (int i = 0; i < n; ++i){ 89         for (int j = n - 1; j >0; --j){ 90             if (box[i][j] == -1)continue; 91  92             if (box[i][j] == box[i][j - 1]){ 93                 box[i][j] *= 2; 94                 box[i][j - 1] = -1; 95                 ret = true; 96                 --j; 97             } 98         } 99     }100 }101 102 bool right(int n){103     bool ret = false;104     moveRight(n, ret);105     mergeRight(n, ret);106     moveRight(n, ret);107     return ret;108 }109 110 void moveUp(int n, bool &ret){111     for (int j = 0; j < n; ++j){112         for (int i = 0; i < n; ++i){113             int k;114             for (k = i - 1; k >= 0; --k){115                 if (box[k][j] != -1){116                     break;117                 }118             }119             box[k + 1][j] = box[i][j];120 121             if ((k + 1) != i){122                 box[i][j] = -1;123                 ret = true;124             }125         }126     }127 }128 129 void mergeUp(int n, bool &ret){130     for (int j = 0; j < n; ++j){131         for (int i = 0; i < n - 1; ++i){132             if (box[i][j] == -1)continue;133 134             if (box[i][j] == box[i + 1][j]){135                 box[i][j] *= 2;136                 box[i + 1][j] = -1;137                 ret = true;138                 ++i;139             }140         }141     }142 }143 144 bool up(int n){145     bool ret = false;146     moveUp(n, ret);147     mergeUp(n, ret);148     moveUp(n, ret);149     return ret;150 }151 152 void moveDown(int n, bool &ret){153     for (int j = 0; j < n; ++j){154         for (int i = n - 1; i >= 0; --i){155             if (box[i][j] == -1)continue;156 157             int k;158             for (k = i + 1; k < n; ++k){159                 if (box[k][j] != -1)break;160             }161 162             box[k - 1][j] = box[i][j];163 164             if ((k - 1) != i){165                 box[i][j] = -1;166                 ret = true;167             }168         }169     }170 }171 172 void mergeDown(int n, bool &ret){173     for (int j = 0; j < n; ++j){174         for (int i = n - 1; i > 0; --i){175             if (box[i][j] == -1)continue;176 177             if (box[i][j] == box[i - 1][j]){178                 box[i][j] *= 2;179                 box[i - 1][j] = -1;180                 ret = true;181                 --i;182             }183         }184     }185 }186 187 bool down(int n){188     bool ret = false;189     moveDown(n, ret);190     mergeDown(n, ret);191     moveDown(n, ret);192     return ret;193 }194 195 void line(int n){196     for (int i = 0; i < n; ++i){197         printf("--------");198     }199     printf("-");200     printf("\n");201 }202 203 void print(int n){204     for (int i = 0; i < n; ++i){205         line(n);206         for (int j = 0; j < n; ++j){207             printf("|");208             if (box[i][j] == -1){209                 printf("\t");210                 continue;211             }212 213             printf("%2d\t", box[i][j]);214         }215         printf("|");216         printf("\n");217     }218     line(n);219 }220 221 bool isFull(int n){222     bool mark = true;223     for (int i = 0; i < n; ++i){224         for (int j = 0; j < n; ++j){225             if (box[i][j] == -1){226                 mark = false;227                 return mark;228             }229         }230     }231     return mark;232 }233 234 bool isOver(int n){235     if (!isFull(n)){236         return false;237     }238     for (int i = 0; i < n; ++i){239         for (int j = 0; j < n; ++j){240             if (box[i][j] == box[i][j + 1] || box[i][j] == box[i + 1][j]){241                 return false;242             }243         }244     }245     return true;246 }247 248 void fillBox(int n){249     int i, j, num;250 251     if (isFull(n)){252         return;253     }254 255     while (true){256         i = rand() % n;257         j = rand() % n;258         num = rand() % 2 == 0 ? 2 : 4;259 260         if (box[i][j] == -1){261             box[i][j] = num;262             break;263         }264     }265 }266 267 int main(){268     //freopen("in.txt", "r", stdin);269     memset(box, -1, sizeof(box));270 271     int n;272     bool mark;273     scanf("%d%*c", &n);274 275     fillBox(n);276     fillBox(n);277     print(n);278 279     while (true){280         char ch;281         scanf("%c%*c", &ch);282         if (ch == a){283             mark = left(n);284         }285         else if (ch == d){286             mark = right(n);287         }288         else if (ch == w){289             mark = up(n);290         }291         else if (ch == s){292             mark = down(n);293         }294         else{295             continue;296         }297         system("cls");298         printf("Move:\n");299         print(n);300         if (!mark){301             continue;302         }303         fillBox(n);304         printf("Fill:\n");305         print(n);306 307         if (isOver(n)){308             printf("\n\nGame Over!\n\n");309             break;310         }311     }312     313     314     return 0;315 }