rt,全局变量m的值一直是3,求大佬指出蒟蒻哪里写错了。
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#define s(x,y) ((rand()%((y)-(x)))+(x))
//#define f(x,y) (pow(2,s(x,y)))
#define cls system("cls")
#define ring printf("\a")
int a[6][11],top[6],n,k[4],m,e,h,score;
char s[1010][1010]= {"zeroth","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth"};
int f(int x,int y);//set the randow value of the new card
void HideCursor();//hide the cursor
void warn();//tell something to player
void pnum(int p);//print n by nth
void print();//print the map
void print2();//print the score and the cards
int print3();//tell the player what should he do now
int check(int p);//check if line p has 2048
int calc(int p);//check if line p could have new card
int put(int p,int k);//put k into line p
int f(int x,int y){
int sr=s(1,4+3*(y-1));
if(1<=sr && sr<=4) return 2;
if(5<=sr && sr<=7) return 4;
if(8<=sr && sr<=10) return 8;
if(11<=sr && sr<=13) return 16;
if(14<=sr && sr<=16) return 32;
if(17<=sr && sr<=19) return 64;
}
void HideCursor() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
void warn() {
printf("Don\'t type any keys except numbers when you are playing!\n\n");
printf("Type ESC to end the game.%d\n\n\n",m);
}
void pnum(int p) {
printf("%s",s[p]);
}
void print() {
for(int i=1; i<=n; i++) {
printf("Line %02d(%d): ",i,top[i]);
if(top[i])
for(int j=1; j<=top[i]; j++) {
if(j==1) printf("[%4d]",a[i][j]);
else printf(" [%3d]",a[i][j]);
}
else printf("There\'s no cards there.");
printf("\n");
}
printf("\n");
}
void print2() {
printf("Your score: %d.\n\n",score);
for(int i=1; i<=e; i++) {
if(i==1) printf("The next card: ");
else {
printf("The next ");
pnum(i);
printf(" card: ");
}
printf("[%d].\n",k[i]);
}
printf("\n");
}
int print3() {
printf("Please input which line you will put [%d].\n",k[1]);
//scanf("%d",&h);
Line:
do {
h=getch()-'0';
int h2=h+'0'-'a'+1;
if(1<=h2 && h2<=n) h=h2;
if(h==-21) exit(0);
} while(1>h || h>n);
int t=put(h,k[1]);
if(t==2) return 1;
if(t==1) return 0;
goto Line;
}
int check(int p) {
for(int i=1; i<=top[p]; i++) {
if(a[p][i]==2048) {
for(int j=1; j<=top[p]; j++)
if(j!=i) score+=a[p][j];
top[p]=0;
return 1;
}
}
return 0;
}
int calc(int p) {
int r=1,g=0;
while(a[p][top[p]]==a[p][top[p]-1] && top[p]>1) {
cls;
ring;
warn();
print();
printf("Your score: %d.\n\n",score);
Sleep(500);
top[p]--;
a[p][top[p]]*=2;
score+=r*a[p][top[p]];
r++;
if(a[p][top[p]]>pow(2,m) && m<6) g=1;
int q=check(p);
if(q==1) return g;
}
}
int put(int p,int k) {
if(top[p]<10) {
a[p][++top[p]]=k;
a[p][0]+=k;
int u=calc(p);
if(u) return 2;
return 1;
}
return 0;
}
int main() {
HideCursor();
srand((unsigned)time(NULL));
n=5;
m=2;
e=3;
score=0;
for(int i=1; i<n; i++) k[i]=f(1,m);
while(1) {
cls;
ring;
warn();
k[e]=f(1,m);
print();
print2();
int l=print3();
if(l) m++;
for(int i=2; i<=n; i++) k[i-1]=k[i];
//getch();
}
return 0;
}