c++实现2048游戏
代码实现
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<conio.h> 5 int sz[6][6],cnt=0; 6 7 /*初始化数组*/ 8 void csh() 9 { 10 for (int i = 1;i <= 4;i++) 11 for (int j = 1;j <= 4;j++) 12 sz[i][j] = 0; 13 14 for (int i = 0;i <= 5;i++) 15 sz[0][i] = sz[i][0] = sz[5][i] = sz[i][5] = '*'; 16 } 17 /*输出数组*/ 18 void sc() 19 { 20 for (int i = 0;i <= 5;i++) 21 { 22 for (int j = 0;j <= 5;j++) 23 if (sz[i][j] == 0) 24 printf(" "); 25 else if (sz[i][j] == '*') 26 printf(" * "); 27 else 28 printf(" %d ", sz[i][j]); 29 printf("\n"); 30 } 31 printf("现在您的积分为:%d",cnt); 32 } 33 34 /*随机位置生成2或4*/ 35 void suiji() 36 { 37 int num,*p; 38 srand(time(NULL)); 39 for (int j = 0;j <= 10;j++) 40 num = (rand() % 2 + 1) * 2; 41 p=&sz[rand() % 4 + 1][rand() % 4 + 1]; 42 if (*p == 0) 43 *p = num; 44 else 45 { 46 while (*p != 0) 47 { 48 p = &sz[rand() % 4 + 1][rand() % 4 + 1]; 49 } 50 *p = num; 51 } 52 } 53 54 /*控制数组数字向上移动,返回是否移动 55 return 56 isMove 是否移动 57 true 是 58 false 否*/ 59 bool up() 60 { 61 bool isMove=false; 62 63 for(int k=1;k<=4;k++) 64 for (int i = 2;i <= 4;i++) 65 for (int j = 1;j <= 4;j++) 66 if (sz[i - 1][j] != '*'&& (sz[i - 1][j] == sz[i][j]|| sz[i - 1][j]==0)) 67 { 68 if (sz[i - 1][j] == sz[i][j]) 69 { 70 sz[i - 1][j] = sz[i - 1][j] + sz[i][j]; 71 sz[i][j] = 0; 72 cnt = cnt + sz[i - 1][j]; 73 74 } 75 else{ 76 sz[i -1][j] = sz[i][j]; 77 sz[i][j] = 0; 78 } 79 if(sz[i-1][j]!=0) 80 isMove = true; 81 } 82 return isMove; 83 } 84 85 /*控制数组数字向下移动,返回是否移动 86 return 87 isMove 是否移动 88 true 是 89 false 否*/ 90 bool down() 91 { 92 int isMove=false; 93 94 for (int k = 1;k <= 4;k++) 95 for (int i = 3;i >= 1;i--) 96 for (int j = 1;j <= 4;j++) 97 if (sz[i + 1][j] != '*' && (sz[i + 1][j] == sz[i][j] || sz[i + 1][j] == 0)) 98 { 99 if (sz[i + 1][j] == sz[i][j]) 100 { 101 sz[i + 1][j] = sz[i + 1][j] + sz[i][j]; 102 sz[i][j] = 0; 103 cnt = cnt + sz[i + 1][j]; 104 } 105 else { 106 sz[i + 1][j] = sz[i][j]; 107 sz[i][j] = 0; 108 } 109 if (sz[i + 1][j] != 0) 110 isMove = true; 111 } 112 return isMove; 113 } 114 115 /*控制数组数字向左移动,返回是否移动 116 return 117 isMove 是否移动 118 true 是 119 false 否*/ 120 bool left() 121 { 122 int isMove = false; 123 124 for (int k = 1;k <= 4;k++) 125 for (int i = 1;i <= 4;i++) 126 for (int j = 2;j <= 4;j++) 127 if (sz[i][j-1] != '*' && (sz[i][j-1] == sz[i][j] || sz[i][j-1] == 0)) 128 { 129 if (sz[i][j-1] == sz[i][j]) 130 { 131 sz[i][j-1] = sz[i][j-1] + sz[i][j]; 132 sz[i][j] = 0; 133 cnt = cnt + sz[i][j-1]; 134 } 135 else { 136 sz[i][j-1] = sz[i][j]; 137 sz[i][j] = 0; 138 } 139 if (sz[i][j-1] != 0) 140 isMove = true; 141 } 142 return isMove; 143 } 144 145 /*控制数组数字向右移动,返回是否移动 146 return 147 isMove 是否移动 148 true 是 149 false 否*/ 150 bool right() 151 { 152 bool isMove = false; 153 154 for (int k = 1;k <= 4;k++) 155 for (int i = 1;i <= 4;i++) 156 for (int j = 3;j >= 1;j--) 157 if (sz[i][j+1] != '*' && (sz[i][j + 1] == sz[i][j] || sz[i][j + 1] == 0)) 158 { 159 if (sz[i][j + 1] == sz[i][j]) 160 { 161 sz[i][j +1] = sz[i][j + 1] + sz[i][j]; 162 sz[i][j] = 0; 163 cnt = cnt + sz[i][j + 1]; 164 } 165 else { 166 sz[i][j + 1] = sz[i][j]; 167 sz[i][j] = 0; 168 } 169 if (sz[i][j+1] != 0) 170 isMove = true; 171 } 172 return isMove; 173 } 174 175 /*接收键盘输入,进行判断移动方向,并连接到各个方向函数去,返回是否移动 176 return 177 isMove 是否移动 178 true 是 179 false 否*/ 180 bool movePd() 181 { 182 char pd; 183 bool isMove; 184 185 pd = _getch(); 186 while(pd!='w'&& pd != 'W'&& pd != 'A'&& pd != 'a'&& pd != 's'&& pd != 'S'&& pd != 'd'&& pd != 'D') 187 pd = _getch(); 188 switch (pd) 189 { 190 case 'W': 191 case 'w':isMove=up();break; 192 case 'S': 193 case 's':isMove = down();break; 194 case 'A': 195 case 'a':isMove = left(); break; 196 case 'D': 197 case 'd':isMove = right(); 198 } 199 return isMove; 200 } 201 202 /*判断游戏是否结束 203 return 204 ture 游戏结束 205 false 游戏继续*/ 206 bool isEnd() 207 { 208 for (int i = 1; i <= 4; i++) 209 for (int j = 1; j <= 4;j++) 210 if (sz[i][j] == 0) 211 return false; 212 for (int i = 1; i <= 4; i++) 213 for (int j = 1; j <= 4; j++) 214 if (sz[i][j] == sz[i+1][j]|| sz[i][j] == sz[i - 1][j] || sz[i][j] == sz[i][j+1] || sz[i][j] == sz[i][j-1]) 215 return false; 216 return true; 217 } 218 void main() 219 { 220 bool end = false,isMove; 221 222 csh(); 223 suiji(); 224 sc(); 225 while (!end) 226 { 227 isMove=movePd(); 228 if (isMove) 229 { 230 system("cls"); 231 suiji(); 232 sc(); 233 end = isEnd(); 234 } 235 236 } 237 printf("\n游戏结束!!!\n"); 238 239 }
运行截图
题外话
看起来很丑对吧,但要美观实在太麻烦了。。。不过反正重点是游戏的实现,美观方面就无所谓了