2048c语言实现
1 #ifndef __2048_H__ 2 #define __2048_H__ 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<time.h> 6 int judge(int (*map)[4],int dimension); 7 void left_mv(int (*map)[4], int dimension); 8 void right_mv(int (*map)[4], int dimension); 9 void up_mv(int (*map)[4], int dimension); 10 void down_mv(int (*map)[4], int dimension); 11 void show_map(int (*map)[4], int dimension); 12 #endif
1 #include"2048.h" 2 int judge(int (*map)[4],int dimension){ 3 int count = 0; 4 for(int row = 0; row < dimension; row++){ 5 for(int col = 0; col < dimension; col++){ 6 if(map[row][col] == 2048){ 7 return 1;//胜利 8 } 9 if(map[row][col] != 0){ 10 count++;//检测是否被占满 11 } 12 } 13 } 14 15 if(count == dimension * dimension){ 16 return 2;//失败 17 } 18 return 0; 19 } 20 21 22 void left_mv(int (*map)[4], int dimension){ 23 //先将所有的0 右移 24 int temp; 25 for(int row = 0; row < dimension; row ++){ 26 for(int col = 0 ;col < dimension -1; col++){ 27 if(map[row][col] == 0){ 28 temp = map[row][col]; 29 map[row][col] = map[row][col + 1]; 30 map[row][col + 1] = temp; 31 } 32 } 33 } 34 for(int row = 0; row < dimension; row ++){ 35 for(int col = 0 ;col < dimension -1; col++){ 36 if(map[row][col] == 0){ 37 temp = map[row][col]; 38 map[row][col] = map[row][col + 1]; 39 map[row][col + 1] = temp; 40 } 41 } 42 } 43 for(int row = 0; row < dimension; row++){ 44 for(int col = 0; col < dimension -1; col++){ 45 if(map[row][col] == 0){ 46 map[row][col] = map[row][col + 1]; 47 map[row][col + 1] = 0; 48 }else if(map[row][col] == map[row][col + 1]){ 49 map[row][col] += map[row][col + 1]; 50 map[row][col + 1] = 0; 51 } 52 } 53 } 54 } 55 void right_mv(int (*map)[4], int dimension){ 56 //先将所有的0 左移 57 int temp; 58 for(int row = 0; row < dimension; row++){ 59 for(int col = dimension -1 ;col > 0; col--){ 60 if(map[row][col] == 0){ 61 temp = map[row][col]; 62 map[row][col] = map[row][col - 1]; 63 map[row][col - 1] = temp; 64 } 65 } 66 } 67 for(int row = 0; row < dimension; row ++){ 68 for(int col= dimension -1;col > 0; col--){ 69 if(map[row][col] == 0){ 70 temp = map[row][col]; 71 map[row][col] = map[row][col - 1]; 72 map[row][col - 1] = temp; 73 } 74 } 75 } 76 for(int row = 0; row < dimension; row++){ 77 for(int col = dimension -1;col > 0; col--){ 78 if(map[row][col] == 0){ 79 map[row][col] = map[row][col - 1]; 80 map[row][col - 1] = 0; 81 }else if(map[row][col] == map[row][col - 1]){ 82 map[row][col] += map[row][col - 1]; 83 map[row][col - 1] = 0; 84 } 85 } 86 } 87 } 88 void up_mv(int (*map)[4], int dimension){ 89 int temp = 0; 90 //先将所有0下移 91 for(int col = 0; col < dimension; col ++){ 92 for(int row = 0 ;row < dimension -1; row++){ 93 if(map[row][col] == 0){ 94 temp = map[row][col]; 95 map[row][col] = map[row + 1][col]; 96 map[row + 1][col] = temp; 97 } 98 } 99 } 100 for(int col = 0; col < dimension; col++){ 101 for(int row = 0 ;row < dimension -1; row++){ 102 if(map[row][col] == 0){ 103 temp = map[row][col]; 104 map[row][col] = map[row + 1][col]; 105 map[row + 1][col] = temp; 106 } 107 } 108 }////////////////////// 109 for(int col = 0; col < dimension; col++){ 110 for(int row = 0; row < dimension -1; row++){ 111 if(map[row][col] == 0){ 112 map[row][col] = map[row + 1][col]; 113 map[row + 1][col] = 0; 114 }else if(map[row][col] == map[row + 1][col]){ 115 map[row][col] += map[row + 1][col]; 116 map[row + 1][col] = 0; 117 } 118 119 } 120 121 } 122 } 123 void down_mv(int (*map)[4], int dimension){ 124 int temp = 0; 125 //先将所有0上移 126 for(int col = 0; col < dimension; col ++){ 127 for(int row = dimension -1 ; row > 0; row--){ 128 if(map[row][col] == 0){ 129 temp = map[row][col]; 130 map[row][col] = map[row -1][col]; 131 map[row - 1][col] = temp; 132 } 133 } 134 } 135 for(int col = 0; col < dimension; col++){ 136 for(int row = dimension -1;row > 0; row--){ 137 if(map[row][col] == 0){ 138 temp = map[row][col]; 139 map[row][col] = map[row - 1][col]; 140 map[row - 1][col] = temp; 141 } 142 } 143 }////////////////////// 144 for(int col = 0; col < dimension; col++){ 145 for(int row = dimension -1;row > 0; row--){ 146 if(map[row][col] == 0){ 147 map[row][col] = map[row - 1][col]; 148 map[row - 1][col] = 0; 149 }else if(map[row][col] == map[row - 1][col]){ 150 map[row][col] += map[row - 1][col]; 151 map[row - 1][col] = 0; 152 } 153 154 } 155 156 } 157 } 158 void show_map(int (*map)[4], int dimension){ 159 system("clear"); 160 int rand_x,rand_y; 161 int num_rand; 162 if(rand() % 2 == 0){ 163 num_rand = 2; 164 }else{ 165 num_rand = 4; 166 } 167 168 while(1){ 169 rand_x = rand() % 4; 170 rand_y = rand() % 4; 171 if(map[rand_x][rand_y] == 0){ 172 map[rand_x][rand_y] = num_rand; 173 break; 174 } 175 } 176 177 printf(" 1 2 3 4\n"); 178 for(int row = 0; row < dimension; row++){ 179 printf(" ---------+---------+---------+---------\n"); 180 printf("%d|",row); 181 for(int col = 0; col < dimension; col++){ 182 if(map[row][col] == 0){ 183 printf(" %4c |",' '); 184 }else{ 185 printf(" %4d |", map[row][col]); 186 } 187 } 188 printf("\n"); 189 } 190 printf(" ---------+---------+---------+---------\n"); 191 }
#include"2048.h" int main(){ srand(time(0)); int map[4][4] = {}; char switch_w; int test; show_map(map,4); while(1){ scanf("%c",&switch_w); switch(switch_w){ case 'a': left_mv(map,4); show_map(map,4); break; case 'd': right_mv(map, 4); show_map(map,4); break; case 'w': up_mv(map, 4); show_map(map, 4); break; case 'x': down_mv(map, 4); show_map(map, 4); break; } test = judge(map,4); if(test == 1){ printf("闯关成功\n"); break; }else if(test == 2){ printf("闯关失败\n"); break; } } }

浙公网安备 33010602011771号