C语言实现五子棋小游戏源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#include
#include
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
int chessx,chessy;
int key;
int chess[20][20];
int flag=1;
void chessboard();
void draw_cicle(int x,int y,int color);
void play();
int result(int x,int y);
void start();
void start()
{
setfont(30, 0, "宋体", 900, 900, 0, false, false, false);
outtextxy(80,240,"GAME START!");
setfont(30, 0, "宋体", 900, 900, 0, false, false, false);
outtextxy(120,340,"ESC-exit/press enter to continue");
}
void chessboard()
{
int i,j;
setbkcolor(WHITE);
cleardevice();
for(i=40;i<=440;i=i+20)
for(j=40;j<=440;j++)
{
putpixel(i,j,8);
putpixel(j,i,8);
}
setcolor(8);
setlinestyle(PS_SOLID);
rectangle(32,32,448,448);
}
void draw_cicle(int x,int y,int color)
{
setcolor(color);
setlinestyle(PS_SOLID);
x=(x+2)*20;
y=(y+2)*20;
circle(x,y,8);
}
void draw_pixel(int x,int y,int color)
{
x=(x+2)*20;
y=(y+2)*20;
{
putpixel(x+8,y,color);
putpixel(x,y-8,color);
putpixel(x+8,y+8,color);
putpixel(x-8,y,color);
}
}
void play()
{
int i;
int j;
switch(key)
{
case LEFT:
if(chessx-1<0)
break;
else
{
for(i=chessx-1,j=chessy;i>=1;i--)
if(chess[i][j]==0)
{
draw_cicle(chessx,chessy,WHITE);
draw_pixel(chessx,chessy,8);
break;
}
if(i<1)
break;
chessx=i;
if(flag==1)
draw_cicle(chessx,chessy,BLUE);
else
draw_cicle(chessx,chessy,RED);
}
break;
case RIGHT:
if(chessx+1>19)
break;
else
{
for(i=chessx+1,j=chessy;i<=19;i++)
if(chess[i][j]==0)
{
draw_cicle(chessx,chessy,WHITE);
draw_pixel(chessx,chessy,8);
break;
}
if(i>19)
break;
chessx=i;
if(flag==1)
draw_cicle(chessx,chessy,BLUE);
else
draw_cicle(chessx,chessy,RED);
}
break;
case DOWN:
if((chessx+1)>19)
break;
else
{
for(i=chessx,j=chessy+1;j<=19;j++)
if(chess[i][j]==0)
{
draw_cicle(chessx,chessy,WHITE);
draw_pixel(chessx,chessy,8);
break;
}
if(j>19)
break;
chessy=j;
if(flag==1)
draw_cicle(chessx,chessy,BLUE);
else
draw_cicle(chessx,chessy,RED);
}
break;
case UP:
if(chessx-1<0)
break;
else
{
for(i=chessx,j=chessy-1;j>=1;j--)
if(chess[i][j]==0)
{
draw_cicle(chessx,chessy,WHITE);
draw_pixel(chessx,chessy,8);
break;
}
if(j<1)
break;
chessy=j;
if(flag==1)
draw_cicle(chessx

,chessy,BLUE);
else
draw_cicle(chessx,chessy,RED);
}
break;
case ESC:
break;
case SPACE:
if(chessx>=1&&chessx<=19&&chessy>=1&&chessy<=19)
{
if(chess[chessx][chessy]==0)
{
chess[chessx][chessy]=flag;
if(result(chessx,chessy)==1)
{
if(flag==1)
{
cleardevice();
setfont(36, 0, "宋体", 900, 900, 0, false, false, false);
outtextxy(80,200,"BLUE win!");
getch();
closegraph();
exit(0);
}
if(flag==2)
{
cleardevice();
setfont(36, 0, "宋体", 900, 900, 0, false, false, false);
outtextxy(80,200,"RED win!");
getch();
closegraph();
exit(0);
}
}
if(flag==1)
flag=2;
else
flag=1;
break;
}
}
else
break;
}
}
int result(int x,int y)
{
int j,k,n1,n2;
while(1)
{
n1=0;
n2=0;
for(j=x,x=y;j>=1&&k>=1;j--,k--)
{
if(chess[j][k]==flag)
n1++;
else
break;
}
for(j=x,x=y;j<=19&&k<=19;j++,k++)
{
if(chess[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
return(1);
n1=0;
n2=0;
for(j=x,x=y;j<=19&&k>=1;j++,k--)
{
if(chess[j][k]==flag)
n1++;
else
break;
}
for(j=x,x=y;j>=1&&k<=19;j--,k++)
{
if(chess[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
return(1);
n1=0;
n2=0;
for(j=x,x=y;j>=1;j--)
{
if(chess[j][k]==flag)
n1++;
else
break;
}
for(j=x,x=y;j<=19;j++)
{
if(chess[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
return(1);
n1=0;
n2=0;
for(j=x,x=y;k>=1;k--)
{
if(chess[j][k]==flag)
n1++;
else
break;
}
for(j=x,x=y;k<=19;k++)
{
if(chess[j][k]==flag)
n2++;
else
break;
}
if(n1+n2-1>=5)
return(1);
return(0);
}
}
void main()
{
int gdrive=DETECT,gmode;
initgraph(&gdrive,&gmode,"");
start();
key=getchar();
if(key==VK_ESCAPE)
exit(0);
else
{
cleardevice();
flag=1;
chessboard();
do
{
chessx=0;
chessy=0;
if(flag==1)
draw_cicle(chessx,chessy,BLUE);
else
draw_cicle(chessx,chessy,RED);
do
{

key=getchar();
play();
}
while(key!=VK_SPACE&&key!=VK_ESCAPE);
}
while(key!=VK_ESCAPE);
closegraph();
}
}



相关文档
最新文档