花了我多少冤枉时间和精力的黑白棋c语言代码
#include "graphics.h"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define ENTER 0x1c0d
int a[8][8]={0},key,score1,score2;
char playone[3],playtwo[3];
void playtoplay(void);
void DrawQp(void);
void SetPlayColor(int x);
void MoveColor(int x,int y);
int QpChange(int x,int y,int z);
void DoScore(void);
void PrintScore(int n);
void playWin(void);
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc");
DrawQp();
playtoplay();
getch();
closegraph();
}
void DrawQp()
{
int i,j;
score1=score2=0;
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);
line(i,100,i,420);
}
setcolor(0);
setfillstyle(SOLID_FILL,15);
fillellipse(500,200,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;
a[3][4]=a[4][3]=2;
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2;
DoScore();
}
void playtoplay()
{
int x,y,t=1,i,j,cc=0;
while(1)
{
x=120,y=80;
while(1)
{
PrintScore(1);
PrintScore(2);
SetPlayColor(t);
fillellipse(x,y,15,15);
key=bioskey(0);
if(key==ESC)
break;
else
if(key==ENTER)
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)
{
if(t%2==1)
a[(x-120)/40][(y-120)/40]=1;
else
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))
{
a[(x-120)/40][(y-120)/40]=0;
cc++;
if(cc>=64-score1-score2)
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;
}
DoScore();
break;
}
else
continue;
}
else
if(key==LEFT&&x>120)
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)
break;
if((score1+score2)==64||score1==0||score2==0)
{
playWin();
break;
}
t=t%2+1;
cc=0;
}
}
void SetPlayColor(int t)
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);
else
setfillstyle(SOLID_FILL,8);
}
void MoveColor(int x,int y)
{
if(y<100)
setfillstyle(SOLID_FILL,BLUE);
else
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break;
case 2:
setfillstyle(SOLID_FILL,8);break;
default:
setfillstyle(SOLID_FILL,BLUE);
}
}
int QpChange(int x,int y,int t)
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40;
j=(y-120)/40;
SetPlayColor(t);
if(j<6)
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1)
yes=1;
}
}
if(j>1)
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;
}
void DoScore()
{
int i,j;
score1=score2=0;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[i][j]==1)
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)
{
if(playnum==1)
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);
if(playnum==1)
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)
outtextxy(100,50,"black win!");
else
if(score2
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}