c语言vary函数 函数变量c语言
如何用C语言编写一个俄罗斯方块的小游戏(配注释)
#include windows.h
创新互联建站-专业网站定制、快速模板网站建设、高性价比蕉城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式蕉城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖蕉城地区。费用合理售后完善,十年实体公司更值得信赖。
#include time.h
#include conio.h
#include stdio.h
const int back=34;
const int speed[]={0,12,9,6,3,1};
const int number[]={0,2,3,5,7,11,15};
const int col[]={11,15,12,12,13,13,14};
int rank=1;
int level=0; //0表示既不是向右也不是向左,1表示向左,2表示向右
int kind;
int pri_kind; // 7大类,颜色标记,调用col[col_kind];
int revolve=0; // 旋转状态标记
struct
{
int x;
int y;
}current,before;
struct
{
int x;
int y;
int color;
bool having;
}board[15][25];
struct
{
int vary_x[4];
int vary_y[4];
} vary[]={
{{0, 2, 4, 6},{0, 0, 0, 0}},{{0, 0, 0, 0},{0,-1,-2,-3}},
{{0, 2, 2, 0},{0, 0,-1,-1}},{{0,-2,-2,-4},{0 ,0,-1,-1}},
{{0, 0, 2, 2},{0,-1,-1,-2}},{{0, 2, 2, 4},{0, 0,-1,-1}},
{{0, 0,-2,-2},{0,-1,-1,-2}},{{0, 0, 2, 4},{0,-1, 0, 0}},
{{0, 0, 0, 2},{0,-1,-2,-2}},{{0, 0,-2,-4},{0,-1,-1,-1}},
{{0, 2, 2, 2},{0, 0,-1,-2}},{{0, 2, 4, 4},{0, 0, 0,-1}},
{{0,-2,-2,-2},{0, 0,-1,-2}},{{0, 0, 2, 4},{0,-1,-1,-1}},
{{0, 0, 0,-2},{0,-1,-2,-2}},{{0 ,2, 4, 2},{0, 0, 0,-1}},
{{0, 0, 2, 0},{0,-1,-1,-2}},{{0,-2, 0, 2},{0,-1,-1,-1}},
{{0, 0,-2, 0},{0,-1,-1,-2}},
};
struct
{
int sum;
int connection_x[5];
int connection_y[5];
}connection[]={
{2,{-2,2},{0,1}},
{1,{0},{0}},
{2,{2,-2},{0,0}},
{2,{-2,2},{0,0}},
{4,{-2,0,4,-2},{0,0,-1,1}},
{4,{-4,2,-2,4},{0,0,-1,1}},
{4,{-2,2,0,0},{0,0,0,0}},
};
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int b)
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole, b) ;
}
void PrintGarphy()
{
color(1593);
int i,j;
gotoxy(2,2);
for(i=0;i26;i++)
printf("▓");
gotoxy(2,28);
for(i=0;i26;i++)
printf("▓");
for(i=0;i26;i++)
{
gotoxy(2,2+i);
puts("▓");
gotoxy(34,2+i);
if(i==8||i==16)
printf("▓▓▓▓▓▓▓▓▓");
else
puts("▓");
gotoxy(52,2+i);
puts("▓");
}
int t;
FILE *fp=fopen("C:\\els.txt","r");
fscanf (fp,"%d",t);
fclose(fp);
color(43);
gotoxy(37,12);
printf("最高纪录: %d",t);
color(46);
gotoxy(37,14);
puts("当前得分: 0");
color(44);
gotoxy(37,16);
puts("级数 ");
color(39);
gotoxy(37,20);
puts(" ↑ ↓ ← →");
gotoxy(37,22);
puts(" 按空格 暂停");
gotoxy(37,24);
puts(" 按 ESC 退出");
gotoxy(37,26);
puts(" 编写人 ");
color(back);
for(i=0;i15;i++)
{
for(j=0;j25;j++)
{
board[i][j].x=4+2*i;
board[i][j].y=3+j;
board[i][j].having=0;
gotoxy(board[i][j].x,board[i][j].y);
puts(" ");
}
}
}
void GameOver()
{
color(391);
for(int i=24;i=0;i--)
for(int j=0;j15;j++)
{
gotoxy(32-j*2,3+i);
puts("▆");
Sleep(5);
}
color(back);
for(int j=0;j25;j++)
for(int i=0;i15;i++)
{
board[i][j].having=0;
gotoxy(4+2*i,j+3);
puts("▆");
Sleep(5);
}
gotoxy(42,16);
while(rank--)
printf(" ");
color(46);
gotoxy(47,14);
puts(" ");
}
void GetDepth(int *pdepth)
{
(*pdepth)=24;
for(int j=0;j4;j++)
{
int sum=0;
int n=(current.x+vary[kind].vary_x[j]-4)/2;
int m=current.y+vary[kind].vary_y[j]-3;
if(m0) m=0;
for(++m;m25;m++)
if(board[n][m].having==0n=0n15m=0m25)
sum++;
else
break;
if(sum(*pdepth))
(*pdepth)=sum;
}
}
void CheckFull(int *pscore)
{
int i,j;
int sum=0;
int s[]={0,1,3,6,10};
for(i=0;i25;i++)
{
bool mark=1;
for(j=0;j15;j++)
{
if(board[j][i].having==0)
{
mark=0;
board[j][i].color=back;
}
}
if(mark)
{
sum++;
for(int t=i;t0;t--)
{
gotoxy(4,3+t);
for(j=0;j15;j++)
{
if(board[j][t-1].having)
{
color(board[j][t-1].color);
printf("▆");
board[j][t].color=board[j][t-1].color;
board[j][t].having=1;
board[j][t-1].having=0;
}
else
{
color(back);
printf(" ");
board[j][t].color=board[j][t].color;
board[j][t].having=0;
board[j][t-1].having=0;
}
}
}
}
}
(*pscore)+=s[sum];
color(46);
gotoxy(47,14);
printf("%d",*pscore);
if(*pscore=140) rank=5;
else if(*pscore=90) rank=4;
else if(*pscore=50) rank=3;
else if(*pscore=20) rank=2;
else if(*pscore=1) rank=1;
color(44);
gotoxy(42,16);
for(i=0;irank*pscore;i++)
printf("★");
int t;
FILE *fp=fopen("C:\\els.txt","r");
fscanf (fp,"%d",t);
fclose(fp);
if(*pscoret)
{
fp=fopen("C:\\els.txt","w");
fprintf (fp,"%d",*pscore);
fclose(fp);
color(43);
gotoxy(47,12);
printf("%d",*pscore);
}
fclose(fp);
}
void PrintNext(int pkind)
{
int i;
int x=43,y=6;
color(34);
for(i=0;i4;i++)
{
gotoxy(x-3,y+i-2);
puts(" ");
}
pri_kind=pkind;
if(pkind==6) pkind=15,x--;
else if(pkind==5) pkind=11,x--;
else if(pkind==4) pkind=7,x--;
else if(pkind==3) pkind=5,x-=2;
else if(pkind==2) pkind=3,x+=2;
else if(pkind==1) pkind=2,x--;
else pkind=0,x-=3;
color(col[pri_kind]);
for(i=0;i4;i++)
{
gotoxy(x+vary[pkind].vary_x[i],y+vary[pkind].vary_y[i]);
puts("▆");
}
}
void CheckBoundary()
{
int i,n,m;
for(i=0;i4;i++)
{
m=current.x+vary[kind].vary_x[i];
if(m4||m32)
{
current.x+=(m4?2:-2);
i=0;
}
}
for(i=0;i4;i++) // 对四个方块分别检查
{
m=current.x+vary[kind].vary_x[i];
n=current.y+vary[kind].vary_y[i];
m=(m-4)/2;
n-=3;
if(n0||n24||board[m][n].having)
{
if(level==1) // 左移
current.x+=2; //复原
else if(level==2) // 右移
current.x-=2; //复原
level=0;
break;
}
}
}
void LevelMove()
{
CheckBoundary();
if(level==0) return;
int i;
color(back);
for(i=0;i4;i++)
{
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y[i]);
puts(" ");
}
}
color(col[pri_kind]);
for(i=0;i4;i++)
{
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary_y[i]);
puts("▆");
}
}
before.x=current.x;
}
void Revolve(int **prev_count) //旋转函数
{
int i;
before.x=current.x;
before.y=current.y;
(**prev_count)++;
(**prev_count)%=connection[pri_kind].sum;
current.x=current.x+connection[pri_kind].connection_x[**prev_count];
current.y=current.y+connection[pri_kind].connection_y[**prev_count];
CheckBoundary();
for(i=0;i4;i++)
{
int m=(current.x-4+vary[number[pri_kind]+**prev_count].vary_x[i])/2;
int n=current.y+vary[number[pri_kind]+**prev_count].vary_y[i]-3;
if(board[m][n].having||m14||m0||n24)
{
current.x=current.x-connection[pri_kind].connection_x[**prev_count];
current.y=current.y-connection[pri_kind].connection_y[**prev_count];
revolve=0;
(**prev_count)--;
before.x=current.x;
before.y=current.y;
return ;
}
}
color(back);
for(i=0;i4;i++)
{
if(before.y+vary[kind].vary_y[i]2)
{
gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y[i]);
puts(" ");
}
}
kind=number[pri_kind]+**prev_count;
color(col[pri_kind]);
for(i=0;i4;i++)
{
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary_y[i]);
puts("▆");
}
}
Sleep(speed[rank]);
before.x=current.x;
before.y=current.y;
}
void ChoiceDirection(int *prev_count)
{
int t=50/rank;
while(t--)
{
if(_kbhit())
{
char c=_getch();
switch(c)
{
case 0x48:// up
revolve=1;
Revolve(prev_count);
break;
case 0x50: //down
level=0;
return;
case 0x4b: //left
before.x=current.x;
current.x-=2;
level=1;
LevelMove();
break;
case 0x4d: //right
before.x=current.x;
current.x+=2;
level=2;
LevelMove();
break;
case ' ':
_getch();
break;
case 27:
color(back);
exit(1);
default:break;
}
}
Sleep(speed[rank]);
}
}
void StraightFall(int depth)
{
int i;
int rev_count=0;
before.x=current.x;
before.y=current.y;
while(GetDepth(depth),depth--)
{
ChoiceDirection(rev_count);
GetDepth(depth);
if(depth==0)
{
revolve=0;
before.x=current.x;
before.y=current.y;
break;
}
color(back);
for(i=0;i4;i++)
{
if(before.y+vary[kind].vary_y[i]2)
{
gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y[i]);
puts(" ");
}
}
current.y++;
color(col[pri_kind]);
for(i=0;i4;i++)
{
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary_y[i]);
puts("▆");
}
}
revolve=0;
before.x=current.x;
before.y=current.y;
}
ChoiceDirection(rev_count);
if(level||revolve)
{
level=0;
revolve=0;
ChoiceDirection(rev_count);
StraightFall(depth); //递归调用
}
}
int main()
{
srand((unsigned long)time(0));
system("mode con cols=56 lines=30");
system("color 24");
int score=0;
FILE *fp=fopen("C:\\els.txt","r");
if(fp==NULL)
{
fp=fopen("C:\\els.txt","w");
fprintf (fp,"%d",score);
fclose(fp);
}
PrintGarphy();
kind=rand()%7;
int depth;
while(1)
{
int pkind=rand()%7;
PrintNext(pkind);
current.x=18;
current.y=2; // 方块初始出现的坐标
GetDepth(depth); // 初始深度
if(depth==0)
{
GameOver();
score=0;
rank=1;
}
pri_kind=kind;
if(kind==6) kind=15,current.x-=2;
else if(kind==5) kind=11;
else if(kind==4) kind=7;
else if(kind==3) kind=5;
else if(kind==2) kind=3,current.x+=2;
else if(kind==1) kind=2;
else kind=0,current.x-=2;
StraightFall(depth);
for(int i=0;i4;i++)
{
int m=(current.x-4+vary[kind].vary_x[i])/2;
int n=current.y-3+vary[kind].vary_y[i];
board[m][n].having=1;
board[m][n].color=col[pri_kind];
}
CheckFull(score);
kind=pkind;
}
return 0;
}
C语言的问题
#include"graphics.h"
#include"stdio.h"
#define LEN sizeof(struct dian)
#define NULL 0
int dian[8][8],result[10][2]; /*dian[][]中存格子的状态,无子,黑子或白子*/
int N,M,s=0,K=0;
int dx,dy,key;
char COMPUTER=0;
int computer=2;
struct dian /*记录所有已经下的子的位置*/
{
int x;
int y;
struct dian *next;
};
struct dian a,*p1,*p,*head;
void init(void) /*初始化*/
{
int gd=DETECT,gm;
initgraph(gd,gm,"c:\\tc");
cleardevice();
}
void jiemian1(void) /*第一个界面*/
{
setlinestyle(0,0,3);
setcolor(3);
rectangle(1,1,640,480);
setcolor(4);
rectangle(3,3,638,478);
setcolor(6);
rectangle(5,5,636,476);
settextstyle(0,0,6);
setcolor(7);
outtextxy(160,160,"HEI BAI");
settextstyle(0,0,1);
setcolor(9);
outtextxy(240,280,"press 1 to play with computer");
outtextxy(240,320,"press 2 to play with another");
do{ /*选择是和人玩还是和电脑下去*/
COMPUTER=bioskey(0);
}while(COMPUTER!='1'COMPUTER!='2');
cleardevice();
}
void qipan(void) /*画棋盘*/
{
int i;
setlinestyle(0,0,3);
setcolor(9);
for(i=0;i=160;i+=20)
{
line(240,i+160,400,i+160);
line(i+240,160,i+240,320);
}
}
void insert(int x,int y) /*将下子的位置插入链表*/
{
struct dian *p2;
p2=(struct dian*)malloc(LEN);
p2-x=x; p2-y=y;
p1-next=p2;
p2-next=NULL;
p1=p2;
}
void paint(int x,int y,int t) /*画棋子*/
{
setcolor(t);
setlinestyle(0,0,1);
circle(20*x+250,20*y+170,8);
setfillstyle(SOLID_FILL,t);
floodfill(20*x+250,20*y+170,t);
}
void jiemian2(void) /*下棋的界面*/
{
int i,j;
for(i=0;i8;i++)
for(j=0;j8;j++)
dian[i][j]=1;
setcolor(8);
rectangle(236,156,404,324);
setfillstyle(SOLID_FILL,6);
bar(238,158,402,322);
setcolor(3);
rectangle(8,8,160,378);
bar(10,10,158,376);
settextstyle(0,0,1);
outtextxy(20,20,"LEFT : change");
outtextxy(20,50,"RIGHT : change");
outtextxy(20,80,"ENTER: play");
outtextxy(20,110,"ESC : leave");
setcolor(9);
outtextxy(20,200,"anything wrong:");
outtextxy(10,230,"zhenlin220@126.com");
qipan();
a.x=3;a.y=3;a.next=NULL;dian[3][3]=0; /*刚开局时候棋盘上的四个棋子*/
p=head=a;p1=a;
paint(3,3,0);
insert(3,4);paint(3,4,7); dian[3][4]=7;
insert(4,3);paint(4,3,7); dian[4][3]=7;
insert(4,4);paint(4,4,0); dian[4][4]=0;
}
void shine(int x,int y,int t) /*棋子闪烁*/
{
static int i=0,j=0;
if(i==xj==y) return 0;
do{
paint(x,y,t);
delay(50000);
paint(x,y,6);
delay(50000);
}while(!kbhit());
key=bioskey(0);
i=x;j=y;
}
void scan(int x,int y,int t) /*查找可以下子的位置或是要变颜色的棋子*/
{
int b,c,d,e,i,j,m,n,r;
K=0;
if(dian[x][y]==1) r=1;
else r=t;
b=x-1; c=x+1; d=y-1; e=y+1;
if(b0) b=0; /*查一个子如黑子四周格子的情况,有可能出边界,把边界限制一下*/
if(c7) c=7;
if(d0) d=0;
if(e7) e=7;
for(i=b;i=c;i++)
for(j=d;j=e;j++)
{
if(dian[i][j]==t||dian[i][j]==1) continue;
dx=i-x;dy=j-y;m=i;n=j;
while(dian[m][n]==s){m+=dx;n+=dy;}
if(dian[m][n]==r) continue;
if(m0||m7||n0||n7) continue;
result[K][0]=m;result[K][1]=n;
K++;
}
}
void vary(int x,int y,int t) /*下子,插入链表,查找并改变需要改变颜色的棋子*/
{
int i,m,n;
if(t==0) N+=1;
else M+=1;
paint(x,y,t);
insert(x,y);
scan(x,y,t);
dian[x][y]=t;
for(i=0;iK;i++)
{
dx=result[i][0]-x;
dy=result[i][1]-y;
if(dx==0) dx=0;
else dx=dx/abs(dx);
if(dy==0) dy=0;
else dy=dy/abs(dy);
m=x; n=y; m+=dx; n+=dy;
do{
if(t==0) { N++; M--;}
else { M++; N--;}
paint(m,n,t);
dian[m][n]=t;
m+=dx; n+=dy;
}while(dian[m][n]!=t);
}
}
void presskey(int t) /*按键*/
{
int i;
for(i=0;iK;i++)
{
if(computer) /*如果和电脑下并且这一步不是电脑下*/
do{
shine(result[i][0],result[i][1],t); /*闪烁棋子*/
}while(key!=0x4b00key!=0x4d00key!=0x1c0dkey!=0x011b);/*如果没有按键继续闪烁*/
if(computer==0)
{
srand(time(NULL)); /*电脑下棋随机选择下哪个位置*/
i=rand()%K;
}
if(computer==0||key==0x1c0d)
{
vary(result[i][0],result[i][1],t);break;
}
if(key==0x011b) exit(0); /*如果按ESC离开游戏*/
}
}
void run(int t)
{
int i;
if(t==0) s=7;
if(t==7) s=0;
a: p=head;
do{
if(dian[p-x][p-y]==s) continue;
scan(p-x,p-y,t); /*查找可以下子的地方并存在result数组中*/
if(K==0) continue;
presskey(t);
if(computer==0||key==0x1c0d) break;
}while((p=p-next)!=NULL);
if(key==0x4b00||key==0x4d00) goto a;
}
void score(void) /*显示分数*/
{
char str1[4],str2[4];
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
setcolor(3);
rectangle(4,4,636,476);
setcolor(4);
rectangle(6,6,634,474);
setfillstyle(SOLID_FILL,YELLOW);
rectangle(8,380,632,472);
setcolor(9);
rectangle(10,382,630,470);
bar(12,384,628,468);
settextstyle(0,0,2);
setcolor(6);
outtextxy(280,400,"score");
sprintf(str1,"%d",N);
sprintf(str2,"%d",M);
outtextxy(120,430,"PLAYER1");
outtextxy(260,430,str1);
outtextxy(300,430,":");
outtextxy(330,430,str2);
if(COMPUTER=='1') outtextxy(390,430,"COMPUTER");
else outtextxy(390,430,"PLAYER2");
}
void winer(void) /*胜利显示*/
{
settextstyle(0,0,4);
setcolor(9);
if(NM) outtextxy(200,50,"player1 win!");
else if(NM) outtextxy(200,50,"player2 win!");
else outtextxy(200,50,"no winer!");
}
main()
{int i;
init(); /*初始化*/
jiemian1(); /*第一个界面*/
b:
cleardevice();
N=2; M=2; /*初始分数*/
jiemian2();
do{
if(COMPUTER=='1')
{
if(s==0) computer=1;
else computer=0;
}
score();
run(s);
}while(N+M!=64||N==0||M==0);
score();
winer();
settextstyle(0,0,1);
outtextxy(260,120,"press any key to restart");
getch();
goto b;
}
求个能在VC下运行的俄罗斯方块的C语言代码,不用GRAPHIC的头文件的
#include windows.h
#include time.h
#include conio.h
#include stdio.h
const int back=34;
const int speed[]={0,12,9,6,3,1};
const int number[]={0,2,3,5,7,11,15};
const int col[]={11,15,12,12,13,13,14};
int rank=1;
int level=0; //0表示既不是向右也不是向左,1表示向左,2表示向右
int kind;
int pri_kind; // 7大类,颜色标记,调用col[col_kind];
int revolve=0; // 旋转状态标记
struct
{
int x;
int y;
}current,before;
struct
{
int x;
int y;
int color;
bool having;
}board[15][25];
struct
{
int vary_x[4];
int vary_y[4];
}vary[]={{{0, 2, 4, 6},{0, 0, 0, 0}},{{0, 0, 0, 0},{0,-1,-2,-3}},{{0, 2, 2, 0},{0, 0,-1,-1}},{{0,-2,-2,-4},{0 ,0,-1,-1}},{{0, 0, 2, 2},{0,-1,-1,-2}},{{0, 2, 2, 4},{0, 0,-1,-1}},{{0, 0,-2,-2},{0,-1,-1,-2}},{{0, 0, 2, 4},{0,-1, 0, 0}},{{0, 0, 0, 2},{0,-1,-2,-2}},{{0, 0,-2,-4},{0,-1,-1,-1}},{{0, 2, 2, 2},{0, 0,-1,-2}},{{0, 2, 4, 4},{0, 0, 0,-1}},{{0,-2,-2,-2},{0, 0,-1,-2}},{{0, 0, 2, 4},{0,-1,-1,-1}},{{0, 0, 0,-2},{0,-1,-2,-2}},{{0 ,2, 4, 2},{0, 0, 0,-1}},{{0, 0, 2, 0},{0,-1,-1,-2}},{{0,-2, 0, 2},{0,-1,-1,-1}},{{0, 0,-2, 0},{0,-1,-1,-2}},};
struct
{
int sum;
int connection_x[5];
int connection_y[5];
}connection[]={{2,{-2,2},{0,1}},{1,{0},{0}},{2,{2,-2},{0,0}},{2,{-2,2},{0,0}},{4,{-2,0,4,-2},{0,0,-1,1}},{4,{-4,2,-2,4},{0,0,-1,1}},{4,{-2,2,0,0},{0,0,0,0}},};
void gotoxy(int x, int y)
{
COORD pos;//WINDOWS API中定义的一个结构表示一个字符在控制台屏幕上的坐标,坐上角(0,0)
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int b)
{
HANDLE hConsole=GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole,b);
}
void PrintGarphy()
{
color(1593);
int i,j;
gotoxy(2,2);
for(i=0;i26;i++)
printf("▓");
gotoxy(2,28);
for(i=0;i26;i++)
printf("▓");
for(i=0;i26;i++)
{
gotoxy(2,2+i);
puts("▓");
gotoxy(34,2+i);
if(i==8||i==16)
printf("▓▓▓▓▓▓▓▓▓");
else
puts("▓");
gotoxy(52,2+i);
puts("▓");
}
int t;
FILE *fp=fopen("C:\\els.txt","r");
fscanf (fp,"%d",t);
fclose(fp);
color(43);
gotoxy(37,12);
printf("最高纪录: %d",t);
color(46);
gotoxy(37,14);
puts("当前得分: 0");
color(44);
gotoxy(37,16);
puts("级数 ");
color(39);
gotoxy(37,20);
puts(" ↑ ↓ ← →");
gotoxy(37,22);
puts(" 按空格 暂停");
gotoxy(37,24);
puts(" 按 ESC 退出");
gotoxy(37,26);
puts(" 编写人 ");
color(back);
for(i=0;i15;i++)
{
for(j=0;j25;j++)
{
board[i][j].x=4+2*i;
board[i][j].y=3+j;
board[i][j].having=0;
gotoxy(board[i][j].x,board[i][j].y);
puts(" ");
}
}
}
void GameOver()
{
color(391);
for(int i=24;i=0;i--)
for(int j=0;j15;j++)
{
gotoxy(32-j*2,3+i);
puts("▆");
Sleep(5);
}
color(back);
for(int j=0;j25;j++)
for(int i=0;i15;i++)
{
board[i][j].having=0;
gotoxy(4+2*i,j+3);
puts("▆");
Sleep(5);
}
gotoxy(42,16);
while(rank--);
printf(" ");
color(46);
gotoxy(47,14);
puts(" ");
}
void GetDepth(int *pdepth)
{
*pdepth=24;
for(int j=0;j4;j++)
{
int sum=0;
int n=(current.x+vary[kind].vary_x[j]-4)/2;
int m=current.y+vary[kind].vary_y[j]-3;
if(m0)
m=0;
for(++m;m25;m++)
if(board[n][m].having==0n=0n15m=0m25)
sum++;
else
break;
if(sum*pdepth)
*pdepth=sum;
}
}
void CheckFull(int *pscore)
{
int i,j;
int sum=0;
int s[]={0,1,3,6,10};
for(i=0;i25;i++)
{
bool mark=1;
for(j=0;j15;j++)
{
if(board[j][i].having==0)
{
mark=0;
board[j][i].color=back;
}
}
if(mark)
{
sum++;
for(int t=i;t0;t--)
{
gotoxy(4,3+t);
for(j=0;j15;j++)
{if(board[j][t-1].having)
{
color(board[j][t-1].color);
printf("▆");
board[j][t].color=board[j][t-1].color;
board[j][t].having=1;
board[j][t-1].having=0;
}
else
{
color(back);
printf(" ");
board[j][t].color=board[j][t].color;
board[j][t].having=0;
board[j][t-1].having=0;
}
}
}
}
}
(*pscore)+=s[sum];
color(46);
gotoxy(47,14);
printf("%d",*pscore);
if(*pscore=140) rank=5;
else if(*pscore=90) rank=4;
else if(*pscore=50) rank=3;
else if(*pscore=20) rank=2;
else if(*pscore=1) rank=1;
color(44); gotoxy(42,16);
for(i=0;irank*pscore;i++)
printf("★");
int t;
FILE *fp=fopen("C:\\els.txt","r");
fscanf (fp,"%d",t);
fclose(fp);
if(*pscoret)
{
fp=fopen("C:\\els.txt","w");
fprintf (fp,"%d",*pscore);
fclose(fp);
color(43);
gotoxy(47,12);
printf("%d",*pscore);
}
fclose(fp);
}
void PrintNext(int pkind)
{
int i;
int x=43,y=6;
color(34);
for(i=0;i4;i++)
{
gotoxy(x-3,y+i-2);
puts(" ");
}
pri_kind=pkind;
if(pkind==6)
pkind=15,x--;
else if(pkind==5)
pkind=11,x--;
else if(pkind==4) pkind=7,x--; else if(pkind==3) pkind=5,x-=2;
else if(pkind==2) pkind=3,x+=2;
else if(pkind==1)
pkind=2,x--;
else
pkind=0;x-=3;
color(col[pri_kind]);
for(i=0;i4;i++)
{ gotoxy(x+vary[pkind].vary_x[i],y+vary[pkind].vary_y[i]);
puts("▆"); }}
void CheckBoundary()
{
int i,n,m;
for(i=0;i4;i++)
{
m=current.x+vary[kind].vary_x[i];
if(m4||m32)
{ current.x+=(m4?2:-2);
i=0;
}
}
for(i=0;i4;i++) // 对四个方块分别检查
{
m=current.x+vary[kind].vary_x[i];
n=current.y+vary[kind].vary_y[i];
m=(m-4)/2;
n-=3;
if(n0||n24||board[m][n].having)
{
if(level==1) // 左移
current.x+=2; //复原
else if(level==2) // 右移
current.x-=2; //复原
level=0;
break;
}
}
}
void LevelMove()
{
CheckBoundary();
if(level==0)
return;
int i;
color(back);
for(i=0;i4;i++)
{ if(current.y+vary[kind].vary_y[i]2)
{ gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y[i]);
puts(" "); } }
color(col[pri_kind]);
for(i=0;i4;i++)
{
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary_y[i]);
puts("▆"); } }
before.x=current.x;}void Revolve(int **prev_count) //旋转函数
{ int i;
before.x=current.x;
before.y=current.y;
(**prev_count)++;
(**prev_count)%=connection[pri_kind].sum;
current.x=current.x+connection[pri_kind].connection_x[**prev_count];
current.y=current.y+connection[pri_kind].connection_y[**prev_count];
CheckBoundary();
for(i=0;i4;i++)
{
int m=(current.x-4+vary[number[pri_kind]+**prev_count].vary_x[i])/2;
int n=current.y+vary[number[pri_kind]+**prev_count].vary_y[i]-3;
if(board[m][n].having||m14||m0||n24)
{ current.x=current.x-connection[pri_kind].connection_x[**prev_count];
current.y=current.y-connection[pri_kind].connection_y[**prev_count];
revolve=0;
(**prev_count)--;
before.x=current.x;
before.y=current.y;
return ;
}
}
color(back);
for(i=0;i4;i++)
{
if(before.y+vary[kind].vary_y[i]2)
{
gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y[i]);
puts(" ");
}
}
kind=number[pri_kind]+**prev_count;
color(col[pri_kind]);
for(i=0;i4;i++)
{
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary_y[i]);
puts("▆");
} } Sleep(speed[rank]); before.x=current.x; before.y=current.y;}
void ChoiceDirection(int *prev_count){
int t=50/rank;
while(t--)
{
if(_kbhit())
{ char c=_getch();
switch(c)
{ case 0x48:// up
revolve=1; Revolve(prev_count);
break; case 0x50: //down
level=0;
return;
case 0x4b: //left
before.x=current.x;
current.x-=2; level=1;
LevelMove();
break; case 0x4d: //right
before.x=current.x;
current.x+=2;
level=2;
LevelMove();
break;
case ' ':
_getch();
break;
case 27:
color(back);
exit(1);
default:break;
} }
Sleep(speed[rank]); }}
void StraightFall(int depth)
{ int i;
int rev_count=0;
before.x=current.x;
before.y=current.y;
while(GetDepth(depth),depth--)
{
ChoiceDirection(rev_count);
GetDepth(depth);
if(depth==0)
{ revolve=0;
before.x=current.x;
before.y=current.y;
break; }
color(back);
for(i=0;i4;i++)
{
if(before.y+vary[kind].vary_y[i]2)
{ gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y[i]);
puts(" ");
} }
current.y++;
color(col[pri_kind]);
for(i=0;i4;i++) {
if(current.y+vary[kind].vary_y[i]2)
{
gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary_y[i]);
puts("▆"); } } revolve=0; before.x=current.x; before.y=current.y; }
ChoiceDirection(rev_count); if(level||revolve) { level=0;
revolve=0;
ChoiceDirection(rev_count); StraightFall(depth); //递归调用
}}int main(){ srand((unsigned long)time(0)); system("mode con cols=56 lines=30");
system("color 24"); int score=0; FILE *fp=fopen("C:\\els.txt","r");
if(fp==NULL)
{ fp=fopen("C:\\els.txt","w"); fprintf (fp,"%d",score);
fclose(fp); } PrintGarphy(); kind=rand()%7; int depth; while(1)
{
int pkind=rand()%7;
PrintNext(pkind);
current.x=18;
current.y=2; // 方块初始出现的坐标
GetDepth(depth); // 初始深度
if(depth==0)
{
GameOver();
score=0; rank=1; }
pri_kind=kind;
if(kind==6) kind=15,current.x-=2;
else if(kind==5) kind=11;
else if(kind==4) kind=7;
else if(kind==3) kind=5;
else if(kind==2) kind=3,current.x+=2;
else if(kind==1) kind=2;
else kind=0,current.x-=2;
StraightFall(depth);
for(int i=0;i4;i++)
{
int m=(current.x-4+vary[kind].vary_x[i])/2;
int n=current.y-3+vary[kind].vary_y[i];
board[m][n].having=1;
board[m][n].color=col[pri_kind];
}
CheckFull(score);
kind=pkind;
}
return 0;
}
c语言如何给一个二维数组求每一行的方差?
#include stdio.h
void main()
{
double a[2][1000],vx=0,vy=0,varx,vary,ax=0,ay=0;
int i,n;
scanf("%d",n); //n是整型,所以用%d
for(i=0;i=n-1;i++)
{
scanf("%lf",a[0][i]); //数组起始为0,不是1
scanf("%lf",a[1][i]);
ax+=a[0][i]; //求和
ay+=a[1][i];
}
ax=ax/n; //求均值
ay=ay/n;
for(i=0;i=n-1;i++)
{
vx=vx+(a[0][i]-ax)*(a[0][i]-ax);
vy=vy+(a[1][i]-ay)*(a[1][i]-ay);
}
varx=vx/n;
vary=vy/n;
printf("varx=%lf\nvary=%lf\n",varx,vary);
}
名称栏目:c语言vary函数 函数变量c语言
文章路径:http://myzitong.com/article/hpophg.html