不要问我ege怎么装
http://tieba.baidu.com/p/2227018541
好,现在我们装好了ege
开始写2048吧
没有算法,单纯模拟,不用讲解——这才叫【水】的含义
界面极度简单,不喜勿喷
1 #include<iostream> 2 #include <math.h> 3 #include <cstdlib> 4 #include <fstream> 5 #include <Windows.h> 6 #include <direct.h> 7 #include <graphics.h> 8 #include <string.h> 9 #include <time.h> 10 #include <math.h> 11 #include <string.h> 12 using namespace std; 13 int a[6][6]; 14 int size; 15 int zj; 16 int jg; 17 int _lb; 18 int tou; 19 int han; 20 int sd; 21 int yc; 22 int bkr,bkg,bkb,wzr,wzg,wzb,btr,btg,btb; 23 color_t bk; 24 color_t wz; 25 color_t bt; 26 bool bo; 27 unsigned long long score=0; 28 bool p[5]; 29 PIMAGE bl[17]; 30 unsigned int see; 31 int GetDirection() //读取方向 32 { 33 int ret = 0; 34 35 do 36 { 37 int ch = getch(); 38 switch(ch) 39 { 40 case 'w': 41 ret = 2; // top 42 break; 43 case 'a': 44 ret = 1; // left 45 break; 46 case 'd': 47 ret = 3; // right 48 break; 49 case 's': 50 ret = 4; // down 51 break; 52 default: 53 break; 54 } 55 } while (ret == 0); 56 57 return ret; 58 } 59 int ha(int k) 60 { 61 int j=1,i=0; 62 for(;j!=k;i++) 63 j*=2; 64 return i; 65 } 66 void print() 67 { 68 cleardevice(); 69 setcolor(bt); 70 setfont(tou,tou/2,"楷体"); 71 xyprintf(0,0,"分数%llu",-score); 72 xyprintf(0,tou,"种子%d",see); 73 for(int i=1;i<=4;i++) 74 for(int j=1;j<=4;j++) 75 if(a[i][j]!=0) 76 putimage(j*(size+jg)-size-jg+_lb,2*tou+han+i*(size+jg)-size-jg,bl[ha(abs(a[i][j]))],SRCCOPY); 77 } 78 int add()//增加数 79 { 80 int sum=0; 81 int x[20],y[20];//空的格子的列表 82 for(int i=1;i<=4;i++) 83 for(int j=1;j<=4;j++) 84 if(!a[i][j]) 85 { 86 x[++sum]=i; 87 y[sum]=j; 88 } 89 int p=(rand()%8==0)?4:2;//生成的数 90 int q=rand()%sum+1;//列表里挑一个 91 a[x[q]][y[q]]=p; 92 print(); 93 delay_ms(0); 94 return 0; 95 } 96 bool over() 97 { 98 for(int i=1;i<=4;i++) 99 for(int j=1;j<=4;j++) 100 if((a[i][j]==0)||(a[i][j]==a[i-1][j])||(a[i][j]==a[i+1][j])||(a[i][j]==a[i][j+1])||(a[i][j]==a[i][j-1])) 101 return 0; 102 if(MessageBox(NULL,"就喜欢你看不惯我又**不到我的感觉","你跪了",MB_RETRYCANCEL)==4) 103 { 104 for(int i=1;i<=4;i++) 105 for(int j=1;j<=4;j++) 106 a[i][j]=0; 107 score=0; 108 add(); 109 return 0; 110 } 111 else 112 { 113 closegraph(); 114 return 1; 115 } 116 } 117 int _up() 118 { 119 for(int i=2;i<=4;i++) 120 for(int j=1;j<=4;j++) 121 if((a[i][j]!=0)&&((a[i-1][j]==a[i][j])||(a[i-1][j]==0))) 122 { 123 bo=1; 124 int ii=i; 125 while((ii>0)&&(a[--ii][j]==0)); 126 if(a[ii][j]==a[i][j]) 127 { 128 int ls=a[i][j]; 129 a[i][j]=0; 130 cleardevice(); 131 for(int zh=1;zh<=(i-ii)*size;zh+=sd) 132 { 133 print(); 134 putimage(j*(size+jg)-size-jg+_lb,2*tou+han+i*(size+jg)-size-jg+_lb-zh,bl[ha(ls)],SRCCOPY); 135 delay_ms(yc); 136 } 137 a[ii][j]*=2; 138 a[ii][j]=-a[ii][j]; 139 score+=a[ii][j]; 140 print(); 141 } 142 else 143 { 144 int ls=a[i][j]; 145 a[i][j]=0; 146 ii++; 147 for(int zh=1;zh<=(i-ii)*size;zh+=sd) 148 { 149 print(); 150 putimage(j*(size+jg)-size-jg+_lb,2*tou+han+i*(size+jg)-size-jg+_lb-zh,bl[ha(ls)],SRCCOPY); 151 delay_ms(yc); 152 } 153 a[ii][j]=ls; 154 print(); 155 } 156 } 157 return 0; 158 } 159 int _down() 160 { 161 for(int i=3;i>=1;i--) 162 for(int j=1;j<=4;j++) 163 if((a[i][j]!=0)&&((a[i+1][j]==a[i][j])||(a[i+1][j]==0))) 164 { 165 bo=1; 166 int ii=i; 167 while((ii<5)&&(a[++ii][j]==0)); 168 if(a[ii][j]==a[i][j]) 169 { 170 int ls=a[i][j]; 171 a[i][j]=0; 172 for(int zh=1;zh<=(ii-i)*size;zh+=sd) 173 { 174 print(); 175 putimage(j*(size+jg)-size-jg+_lb,2*tou+han+i*(size+jg)-size-jg+_lb+zh,bl[ha(ls)],SRCCOPY); 176 delay_ms(yc); 177 } 178 a[ii][j]*=2; 179 a[ii][j]=-a[ii][j]; 180 score+=a[ii][j]; 181 print(); 182 } 183 else 184 { 185 int ls=a[i][j]; 186 a[i][j]=0; 187 ii--; 188 for(int zh=1;zh<=(ii-i)*size;zh+=sd) 189 { 190 print(); 191 putimage(j*(size+jg)-size-jg+_lb,2*tou+han+i*(size+jg)-size-jg+_lb+zh,bl[ha(ls)],SRCCOPY); 192 delay_ms(yc); 193 } 194 a[ii][j]=ls; 195 print(); 196 } 197 } 198 return 0; 199 } 200 int _left() 201 { 202 for(int i=1;i<=4;i++) 203 for(int j=2;j<=4;j++) 204 if((a[i][j]!=0)&&((a[i][j-1]==a[i][j])||(a[i][j-1]==0))) 205 { 206 bo=1; 207 int ii=j; 208 while((ii>0)&&(a[i][--ii]==0)); 209 if(a[i][ii]==a[i][j]) 210 { 211 int ls=a[i][j]; 212 a[i][j]=0; 213 for(int zh=1;zh<=(j-ii)*size;zh+=sd) 214 { 215 print(); 216 putimage(j*(size+jg)-size-jg+_lb-zh,2*tou+han+i*(size+jg)-size-jg+_lb,bl[ha(ls)],SRCCOPY); 217 delay_ms(yc); 218 } 219 a[i][ii]*=2; 220 a[i][ii]=-a[i][ii]; 221 score+=a[i][ii]; 222 print(); 223 } 224 else 225 { 226 int ls=a[i][j]; 227 a[i][j]=0; 228 ii++; 229 for(int zh=1;zh<=(j-ii)*size;zh+=sd) 230 { 231 print(); 232 putimage(j*(size+jg)-size-jg+_lb-zh,2*tou+han+i*(size+jg)-size-jg+_lb,bl[ha(ls)],SRCCOPY); 233 delay_ms(yc); 234 } 235 a[i][ii]=ls; 236 print(); 237 } 238 } 239 return 0; 240 } 241 int _right() 242 { 243 for(int i=1;i<=4;i++) 244 for(int j=3;j>=1;j--) 245 if((a[i][j]!=0)&&((a[i][j+1]==a[i][j])||(a[i][j+1]==0))) 246 { 247 bo=1; 248 int ii=j; 249 while((ii<5)&&(a[i][++ii]==0)); 250 if(a[i][ii]==a[i][j]) 251 { 252 int ls=a[i][j]; 253 a[i][j]=0; 254 for(int zh=1;zh<=(ii-j)*size;zh+=sd) 255 { 256 print(); 257 putimage(j*(size+jg)-size-jg+_lb+zh,2*tou+han+i*(size+jg)-size-jg+_lb,bl[ha(ls)],SRCCOPY); 258 delay_ms(yc); 259 } 260 a[i][ii]*=2; 261 a[i][ii]=-a[i][ii]; 262 score+=a[i][ii]; 263 print(); 264 } 265 else 266 { 267 int ls=a[i][j]; 268 a[i][j]=0; 269 ii--; 270 for(int zh=1;zh<=(ii-j)*size;zh+=sd) 271 { 272 print(); 273 putimage(j*(size+jg)-size-jg+_lb+zh,2*tou+han+i*(size+jg)-size-jg+_lb,bl[ha(ls)],SRCCOPY); 274 delay_ms(yc); 275 } 276 a[i][ii]=ls; 277 print(); 278 } 279 } 280 return 0; 281 } 282 color_t gc(int k) 283 { 284 if(k<=10) 285 return EGERGB(153+10*k,153+10*k,255);//0x9999FF;102 204 153 286 else 287 return EGERGB(255,255-10*(k-10),255-10*(k-10)); 288 } 289 int initblock(int k) 290 { 291 setbkcolor(gc(k),bl[k]); 292 setcolor(bk,bl[k]); 293 for(int i=0;i<size;i++) 294 if(i<zj) 295 { 296 line(0,i,zj-(int)sqrt(zj*zj-(zj-i)*(zj-i)),i,bl[k]); 297 line(size-zj+(int)sqrt(zj*zj-(zj-i)*(zj-i)),i,size,i,bl[k]); 298 } 299 else 300 if(i>=size-zj) 301 { 302 line(0,i,zj-(int)sqrt(zj*zj-(size-zj-i)*(size-zj-i)),i,bl[k]); 303 line(size-zj+(int)sqrt(zj*zj-(size-zj-i)*(size-zj-i)),i,size,i,bl[k]); 304 } 305 char poi[6]; 306 itoa(1 << k,poi,10); 307 setcolor(wz,bl[k]); 308 int _k=1,__k=1<<k; 309 while(__k>9) 310 { 311 _k++; 312 __k/=10; 313 } 314 setfont(-size/2,size/(_k+2),"宋体",bl[k]); 315 outtextxy(size/(_k+2),size/4,poi,bl[k]); 316 return 0; 317 } 318 void init() 319 { 320 initgraph(size*4+jg*3+_lb*2,2*tou+jg*5+han+size*4); 321 setcaption("2048"); 322 SetTransparent(getHWnd,255,BLACK); 323 setrendermode(RENDER_MANUAL); 324 for(int i=1;i<=16;i+=1) 325 { 326 bl[i]=newimage(size,size); 327 initblock(i); 328 } 329 setbkcolor(bk); 330 } 331 int main() 332 { 333 scanf("%d",&see); 334 srand(see); 335 fstream _file; 336 _file.open("set.txt",ios::in); 337 if(!_file) 338 { 339 freopen("set.txt","w",stdout); 340 if(MessageBox(NULL,"你还没建配置文件,是否自动创建 Unless you know exactly what you're doing, is is recommended that you click 确定","我**你妈",MB_OKCANCEL)==1) 341 printf("100 20 20 10 100 10 10 1 102 204 255 0 0 0 248 217 111 "); 342 else 343 system("notepad set.txt"); 344 fclose(stdout); 345 } 346 freopen("set.txt","r",stdin); 347 scanf("%d%d%d%d%d",&size,&zj,&jg,&_lb,&tou); 348 scanf("%d%d%d%d%d",&han,&sd,&yc,&bkr,&bkg); 349 scanf("%d%d%d%d%d",&bkb,&wzr,&wzg,&wzb,&btr); 350 scanf("%d%d",&btg,&btb); 351 fclose(stdin); 352 bk=EGERGB(bkr,bkg,bkb); 353 wz=EGERGB(wzr,wzg,wzb); 354 bt=EGERGB(btr,btg,btb); 355 init(); 356 add(); 357 while(true) 358 { 359 if(over()) 360 return 0; 361 if(bo) 362 add(); 363 bo=0; 364 int c=GetDirection(); 365 switch(c) 366 { 367 case 1: 368 _left(); 369 if(!bo) 370 p[1]=1; 371 break; 372 case 2: 373 _up(); 374 if(!bo) 375 p[2]=1; 376 break; 377 case 3: 378 _right(); 379 if(!bo) 380 p[3]=1; 381 break; 382 case 4: 383 _down(); 384 if(!bo) 385 p[4]=1; 386 break; 387 } 388 for(int i=1;i<=4;i++) 389 for(int j=1;j<=4;j++) 390 a[i][j]=abs(a[i][j]); 391 } 392 return 0; 393 }