C++新手1024.cpp试制
来源:互联网 发布:系统优化的方法 编辑:程序博客网 时间:2024/06/06 03:33
于2014年9月1日后学习C++,由于学习繁重,至今(2015年11月14日)只读了C++ Primer Plus的260页,现以此水平试制1024,望指正。
已知BUG:随机数不够随机,尤其第一格出现的概率较偏大
1024 Build4
#include <iostream>#include <time.h>#include <windows.h>#include <conio.h>using namespace std;struct extraction{int x = 0,y = 0; int gameover = 0;};void welcome();//Startvoid initialization(int ar1[][4],int & num,int & num_t);//initializationvoid reset(int & num,int & num_t);//Next_NUM_Appearextraction renew(int ar[][4]);//The_One_which_need_Fill_Up XY.sthvoid show(int ar[][4]);//showint judgement(const int ar[][4]);//Judgement Really_Losevoid up(int ar1[][4],int ar2[][4],int* n,int _n);void down(int ar1[][4],int ar2[][4],int* n,int _n);void left(int ar1[][4],int ar2[][4],int* n,int _n);void right(int ar1[][4],int ar2[][4],int* n,int _n);int main(){ system("color f1"); int ar1[4][4]{}; int ar2[4][4]{}; int num = 0,num_t = 0;//Use for NUM char choice = '\0'; int n = 1;//Decide ar1(1)||ar2(0) int i = 1;//Next Choice 判断退出与继续 extraction XY; welcome(); initialization(ar1,num,num_t); n ? show(ar1) : show(ar2); while(i) { choice = getch();//按字母数字什么的好像没用,不会读取似的////////////////////////// if(choice==0) choice = getch(); switch(choice) { case 72 : n ? up(ar1,ar2,&n,1) : up(ar2,ar1,&n,0); XY = n ? renew(ar1) : renew(ar2); if(XY.gameover==1) break; reset(num,num_t); n ? ar1[XY.y][XY.x]=num : ar2[XY.y][XY.x]=num; n ? show(ar1) : show(ar2); break; case 80 : n ? down(ar1,ar2,&n,1) : down(ar2,ar1,&n,0); XY = n ? renew(ar1) : renew(ar2); if(XY.gameover==1) break; reset(num,num_t); n ? ar1[XY.y][XY.x]=num : ar2[XY.y][XY.x]=num; n ? show(ar1) : show(ar2); break; case 75 : n ? left(ar1,ar2,&n,1) : left(ar2,ar1,&n,0); XY = n ? renew(ar1) : renew(ar2); if(XY.gameover==1) break; reset(num,num_t); n ? ar1[XY.y][XY.x]=num : ar2[XY.y][XY.x]=num; n ? show(ar1) : show(ar2); break; case 77 : n ? right(ar1,ar2,&n,1) : right(ar2,ar1,&n,0); XY = n ? renew(ar1) : renew(ar2); if(XY.gameover==1) break; reset(num,num_t); n ? ar1[XY.y][XY.x]=num : ar2[XY.y][XY.x]=num; n ? show(ar1) : show(ar2); break; case 27 : i=0; }if(XY.gameover==1) { if((XY.gameover = n ? judgement(ar1) : judgement(ar2))==0)//可行,继续 continue; break;//不可行,跳出循环 } } system("cls"); if(XY.gameover) cout << "\n\n\n\n\n\n\n\t\t\t\tLose?!\n\n\t\t\tExit from the program!\n\n\n\n\n\n\n"; else cout << "\n\n\n\n\n\n\n\n\t\t\t\tBye!\t\t\t\n\n\n\n\n\n\n\n"; Sleep(1500); cout << "Here is the time you play: " << clock()/CLOCKS_PER_SEC << 's'; return 0;}void initialization(int ar1[][4],int & num,int & num_t){ int x = 0,y = 0; //随机数 坐标 for(;y<4;++y) {for(;x<4;++x) {Sleep(1); num_t = clock()%10; if(0<=num_t&&num_t<6) ar1[y][x] = 0; else if(6<=num_t&&num_t<9) ar1[y][x] = 2; else if(num_t == 9) ar1[y][x] = 4; }x=0; }}void reset(int & num,int & num_t) //决定num值 { Sleep(1); num_t = clock()%10; if(0<=num_t&&num_t<8) num = 2; else if(8<=num_t&&num_t<=9) num = 4;}extraction renew(int ar[][4]){ extraction XY; int x=0,y=0,exit=1,count=0; for(;y<4;++y) { for(;x<4;++x) {if(ar[y][x]==0) ++count; } x=0; }if(count==0) {XY.gameover=1; exit=0; } while(exit) {x=0,y=0; for(;y<4;++y) {for(;x<4;++x) {if((!(clock()%10))&&!ar[y][x]) { XY.x = x; XY.y = y; exit=0; break; } }x=0; if(exit==0) break; } } return XY;} void show(int ar[][4])//显示{ system("cls"); cout << "\n\n\n\n\n\n\n"; int y=0,x=0; for(;y<4;++y) {cout << "\t\t\t"; for(;x<4;++x) {cout << ar[y][x] << '\t'; }x=0; cout << endl << endl; }cout << endl;}void up(int ar1[][4],int ar2[][4],int* n,int _n){ int cut=0,i1=0,i2=0,it=0; for(;i1<=3;++i1)//X { i2=0,it=0; for(;i2<=3;++i2)//Y { if(ar1[i2][i1])//排除0 { for(int o=1;i2+o<=3;++o)//加倍合并 { if(ar1[i2+o][i1]!=0 && ar1[i2][i1]!=ar1[i2+o][i1])//遇到异数中断 as '2''4''2' break; if(ar1[i2][i1]==ar1[i2+o][i1]) { ar2[it][i1]=2*ar1[i2][i1]; ++it; while(--o!=0) ++i2; ++i2; cut=1; break; } } if(cut==1)//双倍生效 { cut = 0; continue; }ar2[it][i1]=ar1[i2][i1]; ++it; } } }_n ? --*n : ++*n;//决定ar1(1)||ar2(0) for(int i1=0;i1<4;++i1)//数组清零 { for(int i2=0;i2<4;++i2) ar1[i1][i2]=0; }}void down(int ar1[][4],int ar2[][4],int* n,int _n){ int cut=0,i1=0,i2=3,it=3; for(;i1<=3;++i1) { i2=3,it=3; for(;i2>=0;--i2) { if(ar1[i2][i1]) { for(int o=1;i2-o>=0;++o) { if(ar1[i2-o][i1]!=0 && ar1[i2][i1]!=ar1[i2-o][i1]) break; if(ar1[i2][i1]==ar1[i2-o][i1]) { ar2[it][i1]=2*ar1[i2][i1]; --it; while(--o!=0) --i2; --i2; cut=1; break; } } if(cut==1) { cut = 0; continue; }ar2[it][i1]=ar1[i2][i1]; --it; } } }_n ? --*n : ++*n; for(int i1=0;i1<4;++i1) { for(int i2=0;i2<4;++i2) ar1[i1][i2]=0; }}void left(int ar1[][4],int ar2[][4],int* n,int _n){ int cut=0,i1=0,i2=0,it=0; for(;i1<=3;++i1) { i2=0,it=0; for(;i2<=3;++i2) { if(ar1[i1][i2]) { for(int o=1;i2+o<=3;++o) { if(ar1[i1][i2+o]!=0 && ar1[i1][i2]!=ar1[i1][i2+o]) break; if(ar1[i1][i2]==ar1[i1][i2+o]) { ar2[i1][it]=2*ar1[i1][i2]; ++it; while(--o!=0) ++i2; ++i2; cut=1; break; } } if(cut==1) { cut = 0; continue; }ar2[i1][it]=ar1[i1][i2]; ++it; } } } _n ? --*n : ++*n; for(int i1=0;i1<4;++i1) { for(int i2=0;i2<4;++i2) ar1[i1][i2]=0; }}void right(int ar1[][4],int ar2[][4],int* n,int _n){ int cut=0,i1=0,i2=3,it=3; for(;i1<=3;++i1) { i2=3,it=3; for(;i2>=0;--i2) { if(ar1[i1][i2]) { for(int o=1;i2-o>=0;++o) { if(ar1[i1][i2-o]!=0 && ar1[i1][i2]!=ar1[i1][i2-o]) break; if(ar1[i1][i2]==ar1[i1][i2-o]) { ar2[i1][it]=2*ar1[i1][i2]; --it; while(--o!=0) --i2; --i2; cut=1; break; } } if(cut==1) { cut = 0; continue; }ar2[i1][it]=ar1[i1][i2]; --it; } } }_n ? --*n : ++*n; for(int i1=0;i1<4;++i1) { for(int i2=0;i2<4;++i2) ar1[i1][i2]=0; }}void welcome(){ int go=0; char esc = '0'; cout << "\n\n\n\n\n" << " ┌────────────────┐\n" << " │ │\n" << " │ Welecome │\n" << " │ │\n" << " │ 1024 │\n" << " │ │\n" << " └────────────────┘\n\n\n" << " [Enter]開始 [S]設定 [ESC]退出程式 \n\n"; do { go=0; esc = getch(); if(esc==0) esc = getch(); switch(esc) { case '\r' : break; case 'S' : case 's' : cout << "Sorry,haven't finished!"; cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; go=1; break;//UnFinish case 27 : exit(0); default : go = 1; cin.sync(); } }while(go);}int judgement(const int ar[][4]){ int x=0,y=0; for(;y<4;++y)//横线 { for(;x<3;++x) { if(ar[y][x]==ar[y][x+1]) return 0; }x=0; }y=0; for(;x<4;++x)//纵向 { for(;y<3;++y) { if(ar[x][y]==ar[x][y+1]) return 0; }y=0; }x=0; return 1;}
std::cout << “谢谢!”;
1 0
- C++新手1024.cpp试制
- 新手请教 xxxContainer.cpp 与 xxxContainerView.cpp
- 制作组BGM试制
- 壤股滩瘴试制慌可啪慷滤频蔡盼梢
- 【非技术】周末试制打卤面
- [C++]PSAPIDEM.CPP
- c&cpp常见问题
- .c .cpp区别
- C Cpp IDE
- .c .cpp .h
- C 还是 CPP
- Android C/CPP log
- Android C/CPP log
- mm.cpp/c/pas
- set.cpp/c/pas
- c/cpp混合编译
- mac c cpp
- cpp 调 c 函数
- 2011蓝桥杯java试题及部分参考答案
- Python学习笔记0
- BestCoder Round #62 (div.2)-Clarke and baton(二分搜索)
- iOS Note项目
- MYSQL的COMMIT和ROLLBACK
- C++新手1024.cpp试制
- 你想Xcode所有知识(所有插件,快捷键,经验技巧)
- effective c++尽量少做转型动作(1)
- 关于android.intent.action.MAIN在manifest里的使用?
- UISlider自定义滑块大小
- [ahk]操作Windows计算器实例
- hdu 1325 Is It A Tree?(方法)
- JAVA连接数据库的方式有多种
- TextView的一些常用属性以及对应参数(部分5.0以上)