五子棋C语言代码
C语言五子棋游戏源代码

ncl ud e< #define N 10void welcome();void initqipan();void showqi(int i);void save(int p);void panduan(int p);void heqi();void over();int zouqihang();int zouqilie();/****************** 结构体**************** */struct zuobiao{int x[N*N];inty[N*N]; }weizhi[N*N];主函数**************** *//******************void main() {int p=0;welcome();for(p=1;p<=N*N;p++){weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie(); save(p);showqi(p);panduan(p);}if(p==N*N)heqi();over();}/****************** 建立棋盘*****************/ void initqipan(){int i,j;for(i=0;i<N;i++){printf("%d",i);printf(" ");}for(i=1;i<N;i++){for(j=0;j<N;j++){if(j==0)printf("%d",i);elseprin tf("•");}printf("\n");}}显示棋子**************** */ void showqi(int p){int i,j,k,m;int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=N*N;i++)fread(&weizhi[i],sizeof(struct zuobiao),1,fp);{a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];}for(m=1;m<p;m++){while(weizhi[p].x[p]==a[m]&&weizhi[p].y[p]==b[m]) {printf("error!\n");weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie();m=1;}}for(i=0;i<N;i++){printf("%d",i);printf(" ");}printf("\n");for(i=1;i<N;i++){for(j=1;j<N;j++){if(j==1)printf("%d",i);for(k=1;k<=p;k++){if(i==weizhi[k].x[k]&&j==weizhi[k].y[k]){if(k%2==1){pr intf(" O ");break;} else if(k%2==0){printf(" •");break;} }}if(k>p)pri ntf(" •");else continue;}printf("\n");int zouqihang()走棋行**************** */}int x;printf(" 请输入要走棋子所在行数 !\n" printf("x="); scanf("%d",&x);while(x>N-1||x<1){ printf("error!\n");printf(" 请输入要走棋子所在行数printf("x=");scanf("%d",&x);}return x;} int zouqilie(){int y;printf(" 请输入要走棋子所在列数 !\n printf("y="); scanf("%d",&y);while(y>N-1||y<1) !\n");走棋列 ****************printf("error!\n");printf(" 请输入要走棋子所在列数!\n");printf("y=");scanf("%d",&y);}return y;}文件保存**************** */ void save(int i){FILE *fp; fp=fopen("wuzi_list","wb");fwrite(&weizhi[i],sizeof(struct zuobiao),1,fp);}判断输赢****************** */ void panduan(int p){int i,j,k[8]={1,1,1,1,1,1,1,1,};int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=p;i++){fread(&weizhi[i],sizeof(struct zuobiao),1,fp);a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];判断行***************** */ for(i=1;i<=p;i++){if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)){k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)){k[0]++;else if((a[i]==a[j])&&(b[i]==b[j]-3)){continue;k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)){k[0]++;continue;}else if(k[0]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[0]==5)break;k[0]=1;}else if(k[0]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)){k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)){k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-3)){k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)){k[1]++;continue;}else if(k[1]==5){printf("Player 2 wins!!!\n");}elsecontinue;} if(k[1]==5) break;k[1]=1;判断列********************** for(i=1;i<=p;i++){if(k[0]==5||k[1]==5)break;else if(i%2==1)for(j=1;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])){k[2]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[2]++;else if(k[2]==5){continue;printf("Player 1 wins!!!\n");}elsecontinue;}if(k[2]==5)break;k[2]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])) { k[3]++;else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[3]++;continue;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[3]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[3]++;continue;}else if(k[3]==5){printf("Player 2 wins!!!\n");}elsecontinue;三 eaiq(++■—d u—****************(K 49— T tt )**************三 eaiq(9H S >!)七((CXI s q H "二q )0303(cxl sy ''=s 七①s ai宀(DFIUQU8F+T N((L s q H "二q )0303(二=丫世5七(cxl +H r c k v r v D」s(V H XI %W ① s aik[4]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)) {k[4]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j]-4)) {k[4]++;continue;}else if(k[4]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[4]==5)break;k[4]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j]-1)){k[5]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j]-2)){k[5]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)){k[5]++;continue;(D S _(D宀 厂三二s u '?cxl」9e _d =)七U K巧“丄亠乂)七①S 05宀(DFIUQU8++◎>!((寸s q H "二q )0303(寸自丫"^))七① S 05-************(++■—d u—(r ^—K tt )«K********三 eaiq(9世令)七宀break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)){k[6]++;continue;}else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)){k[6]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3)){k[6]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4)){continue;}else if(k[6]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[6]==5)break;k[6]=1;}else if(k[6]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)) continue;}{else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)) {k[7]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3)) {k[7]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4)) {k[7]++;continue;}else if(k[7]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[7]==5)break;k[7]=1;}}}和棋****************** */ void heqi()printf( H************************************ \n");printf(" Tie!!!\n");printf( H************************************ \n");}游戏结束****************** */ void over()printf( H************************************ \n");printf(" game over!!!\n");printf( H************************************ \n");}游戏开始****************** */ void welcome()printf( H************************************ \n");printf(" Welcome!!!\n");printf( H************************************ \n");}。
C语言实现五子棋小游戏源代码

break;
case ESC:
break;
case SPACE:
if(chessx>=1&&chessx<=19&&chessy>=1&&chessy<=19)
{
if(chess[chessx][chessy]==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;
}
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);
{
chess[chessx][chessy]=flag;
if(result(chessx,chessy)==1)
{
if(flag==1)
{
cleardevice();
n1=0;
n2=0;
C语言五子棋游戏源代码

C语言五子棋游戏源代码标准化管理处编码[BBX968T-XBB8968-NNJ668-MM9N]#define N 10void welcome();void initqipan();void showqi(int i);void save(int p);void panduan(int p);void heqi();void over();int zouqihang();int zouqilie();/******************结构体*****************/ struct zuobiao{int x[N*N];int y[N*N];}weizhi[N*N];/******************主函数*****************/ void main(){int p=0;welcome();initqipan();for(p=1;p<=N*N;p++){weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie();save(p);showqi(p);panduan(p);}if(p==N*N)heqi();over();}/******************建立棋盘*****************/ void initqipan(){int i,j;for(i=0;i<N;i++){printf("%d",i);printf(" ");}printf("\n");for(i=1;i<N;i++){for(j=0;j<N;j++){if(j==0)printf("%d",i);elseprintf("·");}printf("\n");}}/******************显示棋子*****************/ void showqi(int p){int i,j,k,m;int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=N*N;i++){fread(&weizhi[i],sizeof(struct zuobiao),1,fp);a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];}for(m=1;m<p;m++){while(weizhi[p].x[p]==a[m]&&weizhi[p].y[p]==b[m]) {printf("error!\n");weizhi[p].x[p]=zouqihang();weizhi[p].y[p]=zouqilie();m=1;}}for(i=0;i<N;i++){printf("%d",i);printf(" ");}printf("\n");for(i=1;i<N;i++){for(j=1;j<N;j++){if(j==1)printf("%d",i);for(k=1;k<=p;k++){if(i==weizhi[k].x[k]&&j==weizhi[k].y[k]) {if(k%2==1){printf("○");break;} else if(k%2==0){printf("●");break;} }}if(k>p)printf("·");else continue;}printf("\n");}}/******************走棋行*****************/ int zouqihang(){int x;printf("请输入要走棋子所在行数!\n");printf("x=");scanf("%d",&x);while(x>N-1||x<1){printf("error!\n");printf("请输入要走棋子所在行数!\n"); printf("x=");scanf("%d",&x);}return x;}/******************走棋列*****************/ int zouqilie(){int y;printf("请输入要走棋子所在列数!\n");printf("y=");scanf("%d",&y);while(y>N-1||y<1){printf("error!\n");printf("请输入要走棋子所在列数!\n"); printf("y=");scanf("%d",&y);}return y;}/******************文件保存*****************/ void save(int i){FILE *fp;fp=fopen("wuzi_list","wb");fwrite(&weizhi[i],sizeof(struct zuobiao),1,fp);}/****************判断输赢*******************/void panduan(int p){int i,j,k[8]={1,1,1,1,1,1,1,1,};int a[N*N],b[N*N];FILE *fp;fp=fopen("wuzi_list","rb");for(i=1;i<=p;i++){fread(&weizhi[i],sizeof(struct zuobiao),1,fp); a[i]=weizhi[i].x[i];b[i]=weizhi[i].y[i];}/*****************判断行******************/for(i=1;i<=p;i++){if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)){k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)) {k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-3)) {k[0]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)) {k[0]++;continue;}else if(k[0]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[0]==5)break;k[0]=1;}else if(k[0]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j])&&(b[i]==b[j]-1)) {k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-2)) {k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-3)) {k[1]++;continue;}else if((a[i]==a[j])&&(b[i]==b[j]-4)) {k[1]++;continue;}else if(k[1]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[1]==5)break;k[1]=1;}}/**********************判断列************************/ for(i=1;i<=p;i++){if(k[0]==5||k[1]==5)break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])){k[2]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[2]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[2]++;continue;}else if(k[2]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[2]==5)break;k[2]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j])){k[3]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j])) {k[3]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j])) {k[3]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j])) {k[3]++;continue;}else if(k[3]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[3]==5)break;k[3]=1;}}/****************判断对角(左上-右下)******************/ for(i=1;i<=p;i++){if(k[0]==5||k[1]==5||k[2]==5||k[3]==5)break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j]-1)){k[4]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j]-2)) {k[4]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)) {k[4]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j]-4)) {k[4]++;continue;}else if(k[4]==5){printf("Player 1 wins!!!\n");}elsecontinue;}if(k[4]==5)break;k[4]=1;}else if(k[2]==5)break;else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]-1)&&(b[i]==b[j]-1)){k[5]++;continue;}else if((a[i]==a[j]-2)&&(b[i]==b[j]-2)) {k[5]++;continue;}else if((a[i]==a[j]-3)&&(b[i]==b[j]-3)) {k[5]++;continue;}else if((a[i]==a[j]-4)&&(b[i]==b[j]-4)) {k[5]++;continue;}else if(k[5]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[5]==5)break;k[5]=1;}}/**********判断对角(左下-右上)************/for(i=1;i<=p;i++){if(k[0]==5||k[1]==5||k[2]==5||k[3]==5||k[4]==5||k[5]==5) break;else if(i%2==1){for(j=1;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)){k[6]++;continue;}else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)) {k[6]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3)) {k[6]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4))k[6]++;continue;}else if(k[6]==5){printf("Player 1 wins!!!\n"); }elsecontinue;}if(k[6]==5)break;k[6]=1;}else if(k[6]==5)else if(i%2==0){for(j=2;j<=p;j=j+2){if((a[i]==a[j]+1)&&(b[i]==b[j]-1)){k[7]++;continue;}else if((a[i]==a[j]+2)&&(b[i]==b[j]-2)) {k[7]++;continue;}else if((a[i]==a[j]+3)&&(b[i]==b[j]-3))k[7]++;continue;}else if((a[i]==a[j]+4)&&(b[i]==b[j]-4)) {k[7]++;continue;}else if(k[7]==5){printf("Player 2 wins!!!\n");}elsecontinue;}if(k[7]==5)break;k[7]=1;}}}/****************和棋*******************/void heqi(){printf("************************************\n"); printf(" Tie!!!\n");printf("************************************\n"); }/****************游戏结束*******************/void over(){printf("************************************\n"); printf(" game over!!!\n");printf("************************************\n"); }/****************游戏开始*******************/void welcome(){printf("************************************\n"); printf(" Welcome!!!\n");printf("************************************\n"); }。
五子棋代码C语言版

#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <string.h>#include <malloc.h>struct rcd;//声明节点结构typedef struct rcd* Record;//节点指针别名typedef struct rcd record;//节点别名#define MAXIMUS 15 //定义棋盘大小int p[MAXIMUS][MAXIMUS];//存储对局信息char buff[MAXIMUS*2+1][MAXIMUS*4+3];//输出缓冲器int Cx,Cy;//当前光标位置int Now;//当前走子的玩家,1代表黑,2代表白int wl,wp;//当前写入缓冲器的列数和行数位置char* showText;//在棋盘中央显示的文字信息int count;//回合数int Putable;//指示当前是否可以走棋int Exiting;//1为当场上无子并按ESC时询问是否退出程序的状态,2为非此状态int ExiRep;//1为当回放到最后一回合并按向后时询问是否退出回放的状态,2为非此状态Record RecBeg,RecNow;//记录的开始节点和当前节点struct rcd//记录节点结构,双链表形式{int X;//此记录走棋的X坐标int Y;//此记录走棋的Y坐标Record Next;//前一个记录Record Back;//后一个记录};Record newRecord()//记录节点构造函数{Record r=(Record)malloc(sizeof(record));//申请一个节点对象r->Next=NULL;//给予前后节点初值NULLr->Back=NULL;return r;}void Exit()//检查退出程序{int input;if(Exiting)//如果是第二次按下ESC{exit(0);}else//如果是第一次按下ESC则询问是否退出程序{showText="是否退出?再次按下ESC退出,其他键返回";Exiting=1;//指示已经按下过ESC}}void ExitRep()//检查退出回放{int input;if(ExiRep)//如果是第二次后移{ExiRep=3;}else//如果是第一次后移则询问是否退出回放{showText="是否退出?再次后移退出回放,其他键返回";ExiRep=1;//指示已经按下过后移}}void AddRecord()//添加记录{RecNow->X=Cx;//记录坐标RecNow->Y=Cy;RecNow->Next=newRecord();//创建下一个记录节点RecNow->Next->Back=RecNow;//完成双链表RecNow=RecNow->Next;//当前记录推至下一个记录节点}int DelRecord()//删除当前记录节点,1为删除成功,0为删除失败{Record b;//上一个节点if(RecNow->Back!=NULL)//越界检查{b=RecNow->Back;//缓存上一个节点free(RecNow);//释放当前节点RecNow=b;//当前记录回至上一个记录节点return 1;}else{return 0;//没有节点可删除时}}void CleanRecord()//清理所有记录{Record n;//下一个节点while(RecBeg->Next!=NULL)//删除所有记录,直到越界前为止{n=RecBeg->Next;//记下下一个节点free(RecBeg);//释放当前节点RecBeg=n;//当前记录推至下一个记录节点}}char* Copy(char* strDest,const char* strSrc)//修改过的字符串复制函数,会忽略末端的\0 {char* strDestCopy = strDest;while (*strSrc!='\0'){*strDest++=*strSrc++;}return strDestCopy;}void Initialize()//初始化一个对局函数{int i,j;//循环变量system("title 对局中(按方向键控制光标,空格走子),Esc撤销");showText="";//重置显示信息count=0;//回合数归零RecNow=RecBeg=newRecord();Exiting=0;for(i=0;i<MAXIMUS;i++)//重置对局数据{for(j=0;j<MAXIMUS;j++){p[i][j]=0;}}Cx=Cy=MAXIMUS/2;//重置光标到中央Now=1;//重置当前为黑方}char* getStyle(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘{if(p[i][j]==1)//1为黑子return "●";else if(p[i][j]==2)//2为白子return "○";else if(i==0&&j==0)//以下为边缘棋盘样式return "┏";else if(i==MAXIMUS-1&&j==0)return "┓";else if(i==MAXIMUS-1&&j==MAXIMUS-1)return "┛";else if(i==0&&j==MAXIMUS-1)return "┗";else if(i==0)return "┠";else if(i==MAXIMUS-1)return "┨";else if(j==0)return "┯";else if(j==MAXIMUS-1)return "┷";return "┼";//中间的空位}char* getCurse(int i,int j){//获得指定坐标交点位置左上格的样式,通过制表符来模拟光标的显示if(Putable)//可走棋时光标为粗线{if(i==Cx){if(j==Cy)return "┏";else if (j==Cy+1)return "┗";}else if(i==Cx+1){if(j==Cy)return "┓";else if (j==Cy+1)return "┛";}}else//不可走棋时光标为虚线{if(i==Cx){if(j==Cy)return "┌";else if (j==Cy+1)return "└";}else if(i==Cx+1){if(j==Cy)return "┐";else if (j==Cy+1)return "┘";}}return "";//如果不在光标附近则为空}void write(char* c)//向缓冲器写入字符串{Copy(buff[wl]+wp,c);wp+=strlen(c);}void ln()//缓冲器写入位置提行{wl+=1;wp=0;}void Display()//将缓冲器内容输出到屏幕{int i,l=strlen(showText);//循环变量,中间文字信息的长度int Offset=MAXIMUS*2+2-l/2;//算出中间文字信息居中显示所在的横坐标位置if(Offset%2==1)//如果位置为奇数,则移动到偶数,避免混乱{Offset--;}Copy(buff[MAXIMUS]+Offset,showText);//讲中间文字信息复制到缓冲器if(l%2==1)//如果中间文字长度为半角奇数,则补上空格,避免混乱{*(buff[MAXIMUS]+Offset+l)=0x20;}system("cls");//清理屏幕,准备写入for(i=0;i<MAXIMUS*2+1;i++){//循环写入每一行printf("%s",buff[i]);if(i<MAXIMUS*2)//写入完每一行需要换行printf("\n");}}void Print()//将整个棋盘算出并储存到缓冲器,然后调用Display函数显示出来{int i,j;//循环变量wl=0;wp=0;for(j=0;j<=MAXIMUS;j++)//写入出交点左上角的字符,因为需要打印棋盘右下角,所以很以横纵各多一次循环{for(i=0;i<=MAXIMUS;i++){write(getCurse(i,j));//写入左上角字符if(j==0||j==MAXIMUS)//如果是棋上下盘边缘则没有连接的竖线,用空格填充位置{if(i!=MAXIMUS)write("");}else//如果在棋盘中间则用竖线承接上下{if(i==0||i==MAXIMUS-1)//左右边缘的竖线更粗write("┃");else if(i!=MAXIMUS)//中间的竖线write("│");}}if(j==MAXIMUS)//如果是最后一次循环,则只需要处理边侧字符,交点要少一排{break;}ln();//提行开始打印交点内容write("");//用空位补齐位置for(i=0;i<MAXIMUS;i++)//按横坐标循环正常的次数{write(getStyle(i,j));//写入交点字符if(i!=MAXIMUS-1)//如果不在最右侧则补充一个横线承接左右{if(j==0||j==MAXIMUS-1){write("━");//上下边缘的横线更粗}else{write("─");//中间的横线}}}ln();//写完一行后提行}Display();//将缓冲器内容输出到屏幕}int Put(){//在当前光标位置走子,如果非空,则返回0表示失败if(Putable){p[Cx][Cy]=Now;//改变该位置数据AddRecord();return 1;//返回1表示成功}else{return 0;}}int Check()//胜负检查,即判断当前走子位置有没有造成五连珠的情况{int w=1,x=1,y=1,z=1,i;//累计横竖正斜反邪四个方向的连续相同棋子数目for(i=1;i<5;i++)if(Cy+i<MAXIMUS&&p[Cx][Cy+i]==Now)w++;else break;//向下检查for(i=1;i<5;i++)if(Cy-i>0&&p[Cx][Cy-i]==Now)w++;else break;//向上检查if(w>=5)return Now;//若果达到5个则判断当前走子玩家为赢家for(i=1;i<5;i++)if(Cx+i<MAXIMUS&&p[Cx+i][Cy]==Now)x++;else break;//向右检查for(i=1;i<5;i++)if(Cx-i>0&&p[Cx-i][Cy]==Now)x++;else break;//向左检查if(x>=5)return Now;//若果达到5个则判断当前走子玩家为赢家for(i=1;i<5;i++)if(Cx+i<MAXIMUS&&Cy+i<MAXIMUS&&p[Cx+i][Cy+i]==Now)y++;else break;//向右下检查for(i=1;i<5;i++)if(Cx-i>0&&Cy-i>0&&p[Cx-i][Cy-i]==Now)y++;else break;//向左上检查if(y>=5)return Now;//若果达到5个则判断当前走子玩家为赢家for(i=1;i<5;i++)if(Cx+i<MAXIMUS&&Cy-i>0&&p[Cx+i][Cy-i]==Now)z++;else break;//向右上检查for(i=1;i<5;i++)if(Cx-i>0&&Cy+i<MAXIMUS&&p[Cx-i][Cy+i]==Now)z++;else break;//向左下检查if(z>=5)return Now;//若果达到5个则判断当前走子玩家为赢家return 0;//若没有检查到五连珠,则返回0表示还没有玩家达成胜利}void ReplayMode(){int i,j;//循环变量system("title 回放中(按左键后退,右键或空格前进),Esc退出");showText="";//重置显示信息count=0;//回合数归零Putable=0;//不可走棋状态RecBeg->Back=newRecord();RecBeg->Back->Next=RecBeg;RecBeg=RecBeg->Back;for(i=0;i<MAXIMUS;i++)//重置对局数据{for(j=0;j<MAXIMUS;j++){p[i][j]=0;}}Now=1;//重置当前为黑方}void RepForward()//回放模式前进{if(RecNow->Next->Next!=NULL)//越界检查{RecNow=RecNow->Next;//当前节点推至下一个记录节点p[RecNow->X][RecNow->Y]=Now;//按照记录还原一个回合Cx=RecNow->X;//设置光标位置Cy=RecNow->Y;Now=3-Now;//转换当前的黑白方}else//若已达到最后则询问退出{ExitRep();}}void RepBackward()//回放模式后退{if(RecNow->Back!=NULL)//越界检查{p[RecNow->X][RecNow->Y]=0;//按照记录撤销一个回合if(RecNow->Back->Back==NULL)//在整个棋盘没有棋子时隐藏光标{Cx=-2;Cy=-2;}else if(RecNow->Back==NULL)//在只有一个棋子时移动光标到这个棋子的位置{Cx=RecNow->X;Cy=RecNow->Y;}else//正常情况下移动光标到上一回合的位置{Cx=RecNow->Back->X;Cy=RecNow->Back->Y;}RecNow=RecNow->Back;//当前节点后退至上一个记录节点Now=3-Now;//转换当前的黑白方}}void ShowReplay(){int input;//输入变量ReplayMode();//初始化回放模式RecNow=RecBeg;//当前观察从头开始RepForward();//显示第一次走棋while(1)//开始无限回合的死循环,直到Esc退出{if(ExiRep==3){ExiRep=0;break;}Print();//打印棋盘input=getch();//等待键盘按下一个字符if(input==27)//如果是ESC则退出回放{return;}else if(input==0x20)//如果是空格则前进{RepForward();continue;}else if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键{input=getch();//获得第二次输入信息switch(input)//判断方向键方向并移动光标位置{case 0x4B:RepBackward();//向左后退break;case 0x4D:RepForward();//向右前进continue;}}ExiRep=0;//未再次按后移则不准备退出showText="";}}void Regret()//悔棋撤销,如果棋盘上没有子即为退出{if(DelRecord()){//尝试删除当前节点,如果有节点可以删除则p[RecNow->X][RecNow->Y]=0;//撤除当前回合if(RecNow->Back==NULL)//如果删除的是第一颗子则将光标移动到第一颗子原来的位置上{Cx=RecNow->X;Cy=RecNow->Y;}else//否则将光标移动到上一颗子上{Cx=RecNow->Back->X;Cy=RecNow->Back->Y;}Now=3-Now;//反转当前黑白方}else{Exit();//如果没有棋子可以撤销,则询问退出}}int RunGame()//进行整个对局,返回赢家信息(虽然有用上){int input;//输入变量int victor;//赢家信息Initialize();//初始化对局while(1){//开始无限回合的死循环,直到出现胜利跳出Putable=p[Cx][Cy]==0;Print();//打印棋盘input=getch();//等待键盘按下一个字符if(input==27)//如果是ESC则悔棋或退出{Regret();Print();continue;}else if(input==0x20)//如果是空格则开始走子{if(Put())//如果走子成功则判断胜负{victor=Check();Now=3-Now;//轮换当前走子玩家count++;if(victor==1)//如果黑方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText="黑方胜利!按R查看回放,按其他键重新开局";Print();input=getch();if(input==0xE0){getch();}else if(input=='R'||input=='r'){ShowReplay();}return Now;}else if(victor==2)//如果白方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText="白方胜利!按R查看回放,按其他键重新开局";Print();input=getch();if(input==0xE0)getch();}else if(input=='R'||input=='r'){ShowReplay();}return Now;}else if(count==MAXIMUS*MAXIMUS)//如果回合数达到了棋盘总量,即棋盘充满,即为平局{showText="平局!按R查看回放,按其他键重新开局";Print();input=getch();if(input==0xE0){getch();}else if(input=='R'||input=='r'){ShowReplay();}CleanRecord();return 0;}}}else if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键{input=getch();//获得第二次输入信息switch(input)//判断方向键方向并移动光标位置{case 0x4B://Cx--;break;case 0x48:Cy--;break;case 0x4D:Cx++;break;case 0x50:Cy++;}if(Cx<0)Cx=MAXIMUS-1;//如果光标位置越界则移动到对侧if(Cy<0)Cy=MAXIMUS-1;if(Cx>MAXIMUS-1)Cx=0;if(Cy>MAXIMUS-1)Cy=0;}Exiting=0;//未再次按下ESC则不准备退出showText="";}}int main()//主函数{system("mode con cols=63 lines=32");//设置窗口大小system("color E0");//设置颜色while(1){//循环执行游戏RunGame();}}。
用C语言编写的五子棋游戏

Code1. #include <graphics.h>2. #include <conio.h>3. #include <dos.h>4. #include <bios.h>5. #include <malloc.h>6. #include <stdlib.h>7. #include <stdio.h>8. #define R 10 /*The size of mouse*/9. void init(void); /* BGI initialization */10. int cover(int);/*draw lines , set color, output the text*/11. void get_board(void);12. /*using the loop and the line function to draw the chessboard*/13. void word(int); /*input word,the color is come from the rand*/14. void getmouse(int *,int *,int *);15. /*get the location and the button of mouse,key=1 is the left button,key=2 is the rightbutton*/16. void visbilemouse(void);17. /*Display the mouse*//*after typing ,gets the x, y and mouse button,then return */18. void mouse(int *,int *,int *);19. /*drawing a mouse is to put an empty rectangular in the memory , then draw theshape of the mouse in the empty rectangular*/20. void change_word(int); /*show the black or the white*/21. void help(void); /*get playing help*/22. void prompt(int); /*the cancel or quit*/23. void game_player(void); /*how to realize the game*/24. int main()25. {26. int key;27. init();/*BGI initialization*/28. key=cover(0); /*the welcome interface*/29. while(key) /*only it is 1.it will running the loop*/30. {31. get_board(); /*draw the chessboard*/32. game_player(); /*control or play the games*/33. }34. closegraph();35. return 0;36. }37. void init() /* BGI initialization */38. {39. int graphdriver = DETECT, graphmode = 0;40. /* The same effect with gd =VGA and gm = VGAHI */41. registerbgidriver(EGAVGA_driver);/* After register the BGI driver needn't the support of running the BGI */42. initgraph(&graphdriver, &graphmode, "");43. return;44. }45. int cover(int choose)46. {47. int row,col,i;48. char answer;49. switch(choose)50. {51. case 0:52. setfillstyle(SOLID_FILL,BLUE); /*fill in the color*/53. bar(630,450,10,30);54. for(row=30;row<=180;row+=30) /*draw cross lines*/55. line(10,row,160,row);56. for(col=10;col<=180;col+=30) /*draw vertical lines*/57. line(col,30,col,180);58. setcolor(BLACK);59. settextstyle(0,0,3);60. outtextxy(200,200,"loading...");61. setfillstyle(1,BLACK);62. for(i=25;i<175;i+=30)63. {64. pieslice(i,i+20,0,360,14);65. sleep(1);66. }67. for(row=30;row<=180;row+=30)68. line(480,row,630,row);69. for(col=480;col<=630;col+=30)70. line(col,30,col,180);71. setcolor(WHITE);72. settextstyle(0,0,3);73. outtextxy(200,200,"loading...");74. setfillstyle(1,WHITE);75. for(i=495;i<=615;i+=30)76. {77. pieslice(i,660-i,0,360,14);78. sleep(1);79. }80. setcolor(BLUE);81. settextstyle(0,0,3);82. outtextxy(200,200,"loading...");83. settextstyle(0,0,5);84. /* fornt :DEFAULT_FONT, TRIPLEX_FONT,SMALL_FONT,SANSSERIF_FONT,GOTHIC_FONT /direction:lateral and vertical /size */85. for(i=1;i<=21;i++)86. {87. setcolor(i); /*the color of the text*/88. outtextxy(65,100,"FIVE IN A ROW"); /*output the text*/89. }90. setcolor(6);91. settextstyle(0,0,3);92. sleep(1);93. outtextxy(50,300,"Made by Hu yin feng");94. sleep(1);95. outtextxy(100,350," Xiao xin ran");96. sleep(1);97. outtextxy(100,400," Zheng yun");98. setcolor(7);99. settextstyle(0,0,2);100. sleep(2);101. outtextxy(20,430,"would you like to try?(Y/N:)");102. answer=getch();103. break;104. case 1:105. setfillstyle(SOLID_FILL,3);106. bar(640,400,451,220);107. setcolor(BLACK);108. settextstyle(0,0,2.5);109. outtextxy(455,280,"BLACK WIN!");110. sleep(1);111. setcolor(RED);112. settextstyle(0,0,2);113. outtextxy(451,320,"Try again?");114. answer=getch();115. break;116. case 2:117. setfillstyle(SOLID_FILL,3);118. bar(640,400,451,220);119. setcolor(WHITE);120. settextstyle(0,0,2.5);121. outtextxy(455,280,"WHITE WIN!");122. sleep(1);123. setcolor(RED);124. settextstyle(0,0,2);125. outtextxy(455,320,"Try again?");126. answer=getch();127. break;128. case 3:129. setfillstyle(SOLID_FILL,3);130. bar(640,400,451,220);131. settextstyle(0,0,2.5);132. setcolor(WHITE);133. outtextxy(455,280,"A Draw!");134. sleep(1);135. setcolor(RED);136. settextstyle(0,0,2);137. outtextxy(455,320,"Try again?");138. answer=getch();139. break;140. case 4:141. cleardevice();142. setbkcolor(GREEN);143. setfillstyle(SOLID_FILL,MAGENTA);144. bar(620,450,20,30);145. setcolor(RED);146. settextstyle(0,0,5);147. outtextxy(150,100,"Game Over!");148. sleep(2);149. break;150. }151. if(answer=='Y'||answer=='y')152. return 1;153. else154. exit(0);155. return 0;156. }157. void get_board()158. {159. int row,col;160. /*setbkcolor(YELLOW);set the color of background */ 161. setfillstyle(SOLID_FILL,YELLOW);162. bar(450,480,0,0);163. setcolor(BLACK); /*set the color of lines*/164. for(row=0;row<=450;row+=30) /*Draw lines*/165. line(0,row,450,row);166. for(col=0;col<=450;col+=30) /*Draw lines*/167. line(col,0,col,480);168. setcolor(BLACK);169. circle(90,90,2);170. circle(330,90,2); /*draw four small rounds in the chessboard */171. circle(90,330,2);172. circle(330,330,2);173. setfillstyle(SOLID_FILL,GREEN);174. bar(451,0,640,480); /*filling range*/175. return;176. }177. void word(int color)/*input word*/178. {179. settextstyle(0,0,4); /*display the characters of :‘five in a row’*/180. setcolor(color);181. outtextxy(461,5,"FIVE");182. setcolor(color+1);183. outtextxy(496,45,"IN");184. setcolor(color+4);185. outtextxy(500,80,"A");186. setcolor(color+4);187. outtextxy(540,80,"ROW");188. setcolor(YELLOW);189. settextstyle(0,0,1);190. rectangle(460,450,510,470);/* the help window */191. rectangle(590,450,630,470); /* the regret window */192. rectangle(520,450,580,470); /*the exit window */193. setcolor(BLACK);194. outtextxy(470,455,"help");195. outtextxy(525,455,"cancel");196. outtextxy(595,455,"exit");197. return;198. }199. void change_word(digit)200. {201. if(digit==0)202. {203. settextstyle(0,0,2); /*when choose white then hint the next one is black */ 204. setcolor(BLACK);205. outtextxy(459,130,"THE BLACK");206. setcolor(GREEN);207. outtextxy(459,180,"THE WHITE");208. }209. else if(digit==1)210. {211. settextstyle(0,0,2);212. setcolor(GREEN);213. outtextxy(459,130,"THE BLACK");214. setcolor(WHITE); /*when choose black then hint the next one is white*/ 215. outtextxy(459,180,"THE WHITE");216. }217. return;218. }219. void help()220. {221. setfillstyle(SOLID_FILL,BLUE);222. bar(640,480,0,0);223. setcolor(YELLOW);224. rectangle(0,0,639,479);225. settextstyle(0,0,3);226. outtextxy(50,10,"How to play the game");227. settextstyle(0,0,2);228. setcolor(WHITE);229. outtextxy(10,60,"1. Clicking the mouse in the chessboard"); 230. outtextxy(10,80," to start the game black is the first , "); 231. outtextxy(10,100," the changing word on the right will "); 232. outtextxy(10,120," remind you the next person to play ;"); 233. outtextxy(10,160,"2. The side will win who form a ");234. outtextxy(10,180," continuous five chess pieces in a "); 235. outtextxy(10,200," straight line no matter of horizontal,"); 236. outtextxy(10,220," vertical and oblique.");237. outtextxy(10,260,"3. Clicking the 'regret' on the right ");238. outtextxy(10,280," is to erase the last chess you'd just "); 239. outtextxy(10,300," played ;");240. outtextxy(10,340,"4. Clicking the 'exit' is to exit the ");241. outtextxy(10,360," game ,it'll jump out another window to "); 242. outtextxy(10,380," make sure if you decide to end the "); 243. outtextxy(10,400," game,if press'y'is closing the window "); 244. outtextxy(10,420," and press 'n' is to continue the game ;"); 245. settextstyle(0,0,2);246. setcolor(RED);247. outtextxy(100,450,"Please any key to continue!");248. getch();249. return;250. }251. void prompt(number)252. {253. if(number==1)254. {255. setcolor(RED); /*the exit window*/256. setfillstyle(SOLID_FILL,BLUE);257. bar(640,400,451,220);258. settextstyle(0,0,2.5);259. outtextxy(455,300,"quit?(Y/N)");260. }261. else if(number==0)262. {263. setcolor(RED); /* the regret window*/264. setfillstyle(SOLID_FILL,BLUE);265. bar(640,400,451,220);266. settextstyle(0,0,2);267. outtextxy(480,300,"Cancel?");268. }269. return;270. }271. /*get the location and the button of mouse,key=1 is the left button*/ 272. void getmouse(int *x,int *y,int *key)273. {274. union REGS inregs,outregs;275. inregs.x.ax=3; /*Obtain the position and the state of mouse can also use 3*/ 276. int86(0x33,&inregs,&outregs); /*Interrupt calls*/277. *x=outregs.x.cx; /*The X-axis is saved in cx register */278. *y=outregs.x.dx; /*The Y-axis is saved in dx register*/279. *key=outregs.x.bx;/*Bx registers are button states*/280. return;281. }282. void visbilemouse()283. {284. union REGS inregs,outregs;285. inregs.x.ax=0x01; /*Display the mouse*/286. int86(0x33,&inregs,&outregs);287. return;288. }/*after typing ,gets the x, y and mouse button,then return */289. void mouse(int *x,int *y,int *z)/*drawing a mouse is to put an empty rectangular in the memory , then draw shape of the mouse in the empty rectangular*/290. {291. int a=0,b=0,c=0,a_old=0,b_old=0; /*it's ok to be free of the value of a and b*/ 292. int color;293. float i=0;294. int *ball; /*define a pointer to point to the store of graphics*/295. ball=malloc(imagesize(a,b,a+R,b+R)); /*Returns the size of the rectangle*/ 296. getimage(a,b,a+R,b+R,ball);/*the first time to put graphics that save an empty rectangle into the memory */ 297. while(c==0)/*loop will be ended until typein */298. {299. getmouse(&a,&b,&c);/*a,is X-axis,b is Y-axis,c is the statement ofthe key */300. if(a<0) a=0; /*ensure the left of the mouse do not out of bound*/301. if(b<0) b=0; /*ensure the up of the mouse do not out of bound*/302. if(a>getmaxx()-R) a=getmaxx()-R;/*ensure the right of the mouse do not out of bound*//*ensure the down of the mouse do not out of bound*/303. if(b>getmaxy()-R) b=getmaxy()-R;304. if(a!=a_old || b!=b_old) /*When the mouse moves*/305. {306. putimage(a_old,b_old,ball,0); /*output the graphic in a_oldandb_old to erase originally mouse*/307. getimage(a,b,a+R,b+R,ball);/*the statement is to save the location of the graph of the mouse in the ball*/ 308. setcolor(BLACK);309. setlinestyle(0,0,1);310. line(a,b,a+R,b+R/2);311. line(a,b,a+R/2,b+R);312. line(a+R,b+R/2,a+R/2,b+R);313. line(a+R*3/4,b+R*3/4,a+R,b+R);/*draw the mouse*/314. }315. a_old=a;b_old=b;316. i++;317. if(i==200000) /*Flashing frequency*/318. {319. color=rand()%16;/*use the feature of the loop of mouse will get the randon color*/320. word(color);321. i=1;/*the value of the i return to 1*/322. }323. }324. /*end of while()*/325. *x=a;*y=b;*z=c; /*return the position of the mouse after typing*/326. putimage(a,b,ball,0);/*ease the mouse ,because it is a empty retangle of default-background save in the ball */ 327. free(ball);328. return;329. }/*the main idea is through storing the present graphic in the getimage,put image and imagesize to erase the preceding mouse's graphic, we also can use clearing parts of the screen */330. void game_player()331. {332. int x,y,z,row,col,i;333. char answer;334. int address[16][15]={NULL},count[2]={0};335. int temp=0,num=1;336. visbilemouse();337. do338. {339. mouse(&x,&y,&z);340. if(x<450)/*judge whether the location of the mouse is in the keyboard*/341. {342. col=x/30;343. row=y/30;344. x=30*col+15;345. y=30*row+15;346. if(address[row][col]==0) /*whether the position is available*/347. {348. temp++; /*only accumulate in no circumstance of the pieces*/ 349. count[0]=x;350. count[1]=y;/*save the coordinate of y in an array convenient for regret*/351. if(temp%2==1)352. {353. address[row][col]=1;354. setcolor(BLACK);355. setfillstyle(1,BLACK);356. pieslice(x,y,0,360,14);/*Using the method of painting fan-shaped to draw a circle*/357. change_word(1);358. }359. else360. {361. setcolor(WHITE);362. address[row][col]=2;363. setfillstyle(1,WHITE);364. pieslice(x,y,0,360,14);365. change_word(0);366. }367. /*Judgement of the situation in a row*/368. /*make the judgment of if there's five in a row*/369. for(i=1;i<=4;i++)370. {371. if(col+i<=14)372. {373. if(address[row][col]==address[row][col+i])374. num++;375. else376. break;377. }378. else379. break;380. }381. if(num!=5)382. for(i=1;i<=4;i++)383. {384. if(col-i>=0)385. {386. if(address[row][col]==address[row][col-i]) 387. num++;388. else if(num<5)389. {390. num=1; /*the num is reassigned to 1*/ 391. break;392. }393. else394. break;395. }396. else if(num<5)397. {398. num=1;399. break;400. }401. else402. break;403. }404. /*make the judgment of if there's five in a column*/405. for(i=1;i<=4;i++)406. {407. if(row+i<=15)408. {409. if(address[row][col]==address[row+i][col])410. num++;411. else412. break;413. }414. else415. break;416. }417. if(num!=5)418. for(i=1;i<=4;i++)419. {420. if(row-i>=0)421. {422. if(address[row][col]==address[row-i][col]) 423. num++;424. else if(num<5)425. {426. num=1;427. break;428. }429. else430. break;431. }432. else if(num<5)433. {434. num=1;435. break;436. }437. else438. break;439. }440. /*make judgment of if the main diagonal line have reached the five */ 441. for(i=1;i<=4;i++)442. { if(row-i>=0&&col+i<=14)443. {444. if(address[row][col]==address[row-i][col+i]) 445. num++;446. else447. break;448. }449. else450. break;451. }452. if(num!=5)453. for(i=1;i<=4;i++)454. {455. if(col-i>=0&&row+i<=15)456. {457. if(address[row][col]==address[row+i][col-i]) 458. num++;459. else if(num<5)460. {461. num=1;462. break;463. }464. else465. break;466. }467. else if(num<5)468. {469. num=1;470. break;471. }472. else473. break;474. }475. /*make judgment of if the main diagonal line have reached the five */ 476. for(i=1;i<=4;i++)477. {478. if(row-i>=0&&col-i>=0)479. {480. if(address[row][col]==address[row-i][col-i]) 481. num++;482. else483. break;484. }485. else486. break;487. }488. if(num!=5)489. for(i=1;i<=4;i++)490. {491. if(row+i<=16&&col+i<=14)492. {493. if(address[row][col]==address[row+i][col+i]) 494. num++;495. else if(num<5)496. {497. num=1;498. break;499. }500. else501. break;502. }503. else if(num<5)504. {505. num=1;506. break;507. }508. else509. break;510. }511. if(num>=5)512. {513. cover(address[row][col]);514. return;515. }516. else if(temp==240)517. {518. cover(3);519. return;520. }521. }522. }523. else if(x>460 && x<510&&y>450&&y<470)524. {525. help();526. get_board();527. game_player();528. }529. else if(x>590 && x<630&&y>450&&y<470)530. {531. prompt(1);532. answer=getch();533. if(answer=='Y'||answer=='y')534. {535. cover(4);536. exit(0);537. }538. else539. {540. setfillstyle(SOLID_FILL,GREEN);541. bar(640,400,451,220);542. continue;543. }544. }545. else if(x>520 && x<580&&y>450&&y<470)546. {547. prompt(0);548. answer=getch();549. setfillstyle(SOLID_FILL,GREEN);550. bar(640,400,451,220);551. if(answer=='Y'||answer=='y')552. {553. setcolor(YELLOW);554. setfillstyle(1,YELLOW);555. pieslice(count[0],count[1],0,360,14); /*only regrets once*/ 556. address[row][col]=0;557. temp--;558. }559. else560. continue;561. }562. else563. continue;564. }565. while(x<640 || x>0 || y<480 || y>0); /*the range of the mouse*/ 566. return;567. }。
五子棋C语言程序代码

五子棋C语言程序代码#include <graphics.h>#include <stdio.h>#include <conio.h>void drawPanel();int isWin(int,int);int color = 1; //1-红色2-白色int chessman[15][15];//主函数void main(){initgraph(620,620); //产生窗体//画棋盘drawPanel();//画棋子//1.定义鼠标事件MOUSEMSG m;HWND wnd = GetHWnd(); //定义当前窗体的句柄while(true){m = GetMouseMsg(); //获取鼠标事件对象if(m.uMsg == WM_LBUTTONDOWN){//获取点击的坐标int x = m.x;int y = m.y;//换算成二维数组中的下标int i = x/40;int j = y/40;//显示点击的坐标/*char msg[100];sprintf(msg,"%d,%d",i,j);MessageBoxA(wnd,msg,"消息",MB_OK);*/if(color==1){setfillstyle(RGB(255,0,0));fillcircle(40*i+20,40*j+20,20);chessman[i][j] = 1;}else if(color==2){setfillstyle(RGB(255,255,255));fillcircle(40*i+20,40*j+20,20);chessman[i][j] = 2;}//判断输赢result => 1int result = isWin(i,j);if(result ==1){if(color==1){MessageBoxA(wnd,_T("恭喜,红方获胜!"),"消息",MB_OK);}else if(color==2){MessageBoxA(wnd,_T("恭喜,白方获胜!"),"消息",MB_OK);}break;}//切换对方下子color = color == 1 ? 2 : 1 ;}}getch();closegraph(); //关闭窗体}int isWin(int x,int y){int count=0;//计数器int i;//横向for(i=0;i<15;i++){if(chessman[i][y]==color){count++;if(count==5)return 1;}else{count=0;}}//竖向for(i=0;i<15;i++){if(chessman[x][i]==color){count++;if(count==5)return 1;}else{count=0;}}return 0;}void drawPanel(){int i;//画横线for(i=0;i<15;i++){line(20,20+40*i,20+14*40,20+40*i);}//画竖线for(i=0;i<15;i++){line(20+40*i,20,20+40*i,20+14*40);}}。
五子棋单机版代码

一下代码为纯c写的单机版五子棋代码,可用vc6.0编译运行#include <stdio.h>#include <conio.h>#include <windows.h>#include <time.h>#define TEXTS 7 // 文本颜色#define CURSOR 48 // 光标颜色#define CHESSBOARD 352 // 棋盘颜色#define WHITECHESS 103 // 白棋颜色#define SELECTEDWHITE 55 // 白棋被选中时的颜色#define BLACKCHESS 96 // 黑棋颜色#define SELECTEDBLACK 48 // 黑棋被选中时的颜色#define qx1_num 27 // 防御棋形的数量#define qx2_num 26 // 攻击棋形的数量typedef struct node{ // 棋盘信息int step; // 步数,步数为0表示该位置为空int color; // 棋子的颜色} NODE;typedef struct point{ //点int x;int y;} _POINT;typedef struct qixing{ // 棋形信息char qx[8]; // 棋形int value; // 相应的权值}QIXING;HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE); // 得到标准输出的句柄_POINT cursor; // 游戏中,光标所在的当前位置int direction[8][2]={{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{1,1},{-1,1},{1,-1}};// 向量数组,依次为左、右、上、下、左上、右下、右上、左下QIXING qx1[qx1_num]={{"x1111",200000},{"1x111",200000},{"11x11",200000}, // 连五型{"0x1110",6000},{"01x110",6000},{"101x101",6000}, // 活四型{"0x111",1100},{"x0111",1100},{"0x1011",1100},{"0x1101",1100},{"01x11",1100}, // 冲四型{"011x1",1100},{"1x011",1100},{"10x11",1100},{"11x01",1100},{"1x101",1100}, // 冲四型{"x011102",250},{"0x110",250},{"01x10",250},{"0x01102",240},{"0x101",240}, // 活三型{"0x112",20},{"01x12",10},{"011x2",20},{"1x12",10},{"0x10",20},{"0x010",5}}; // 死三活二//防御的基本棋形及权值,0为空,1为对手,2为自己,x为下棋位置QIXING qx2[qx2_num]={{"x1111",2000000},{"1x111",2000000},{"11x11",2000000}, // 连五型{"0x1110",24000},{"01x110",24000}, {"101x101",24000}, //活四型{"0x111",2000},{"x0111",1900},{"0x1011",1900},{"0x1101",2000},{"01x11",2000}, // 冲四型{"011x1",2000},{"1x011",1900},{"10x11",2000},{"1x101",2000},{"x01112",2000}, // 冲四型{"0x110",850},{"01x10",850},{"0x0110",840},{"0x101",840},//活三型{"0x112",125},{"01x12",125},{"011x2",115},{"1x12",115},{"0x10",125},{"0x010",110}};// 死三活二// 攻击的基本棋形及权值,0为空,1为自己,2为对手,x为下棋位置//----------------------------------界面部分---------------------------------void textcolor(int color){// 更改字体颜色SetConsoleTextAttribute(hOutput,color);}void gotoxy(int x, int y){// 将光标移动到指定位置COORD coordScreen = { 0, 0 };coordScreen.X=x;coordScreen.Y=y;SetConsoleCursorPosition( hOutput, coordScreen );}void printnode(NODE chessboard[][15], int x, int y)// 打印棋盘上的一个点{textcolor(CHESSBOARD);if(chessboard[x][y].step==0) {if (x==cursor.x && y==cursor.y)textcolor(CURSOR); // 如果光标在这个点,改成光标颜色switch(x){case 0:if(y==0) printf("┏"); // 左上角else if(y==14) printf("┓"); // 右上角else printf("┯"); // 上边线break;case 3:if(y==0) printf("┠"); // 左边线else if(y==3 || y==11) printf("+"); // 星位else if(y==14) printf("┨"); // 右边线else printf("┼");// 交叉点break;case 7:if(y==0) printf("┠"); // 左边线else if(y==7) printf("+"); // 星位else if(y==14) printf("┨"); // 右边线else printf("┼");// 交叉点break;case 11:if(y==0) printf("┠"); // 左边线else if(y==3 || y==11) printf("+"); // 星位else if(y==14) printf("┨"); // 右边线else printf("┼");// 交叉点break;case 14:if(y==0) printf("┗"); // 左下角else if(y==14) printf("┛"); // 右下角else printf("┷"); // 下边线break;default:if(y==0) printf("┠"); // 左边线else if(y==14) printf("┨"); // 右边线else printf("┼");// 交叉点}}else if(chessboard[x][y].color==0){ // 如果是白棋if (x==cursor.x && y==cursor.y)textcolor(SELECTEDWHITE); // 被选中的白棋else textcolor(WHITECHESS); // 未被选中的白棋printf("●");} // 打印棋子else{if (x==cursor.x && y==cursor.y)textcolor(SELECTEDBLACK); // 被选中的黑子else textcolor(BLACKCHESS); // 未被选中的黑子printf("●");} // 打印棋子}void printchessboard(NODE chessboard[][15]){// 输出整个棋盘int i,j;char letter[]={"ABCDEFGHIJKLMNO\n"};for(i=0;i<15;i++){ // 行textcolor(TEXTS); // 改为文本颜色printf("%2d",15-i); // 打印行坐标for(j=0;j<15;j++) // 列printnode(chessboard,i,j); // 打印棋盘的每一块textcolor(TEXTS);printf("\n");}textcolor(TEXTS); //改为文本颜色printf(" %s",letter); //打印列坐标printf("移动:方向键下棋:ENTER 悔棋:U 退出:F12");}void renew(NODE chessboard[][15], int x, int y){// 更新棋盘指定位置的图像COORD coordScreen; // 系统提示符位置CONSOLE_SCREEN_BUFFER_INFO csbi; // 屏幕信息if(x<0 || x>14 || y<0 || y>14) return; // 如果不在棋盘上直接返回if( !GetConsoleScreenBufferInfo( hOutput, &csbi )) // 获取屏幕信息return; // 不成功则返回coordScreen=csbi.dwCursorPosition; // 获取系统提示符位置gotoxy((y-1)*2+4,x+1); // 将系统提示符移动到棋盘的(x,y)所在位置printnode(chessboard,x,y); // 重新打印这一块SetConsoleCursorPosition( hOutput, coordScreen );// 系统提示符回复到原来位置}void showmenu(){// 输出主菜单textcolor(TEXTS);system("cls");printf("1.人机对战\n");printf("2.双人对战\n");printf("3.退出\n");printf("\n请选择[1-3]:");}void showsubmenu(){// 打印子菜单textcolor(TEXTS);system("cls");printf("1.你先手\n");printf("2.电脑先手\n");printf("3.返回上级菜单\n");printf("\n请选择[1-3]:");}int getchoose(int min, int max){// 获取选项int choose;do{choose=getch()-48;}while(choose<min || choose>max); //过滤不在min到max之间的字符printf("%d",choose); //屏幕回显return choose;}//----------------------------------控制部分---------------------------------bool quit; //是否按下了退出热键bool regret; //是否按下了悔棋热键bool getmove(NODE chessboard[][15]){// 获取光标移动,并响应// 当按下悔棋、下子、退出热键时,返回truechar c;for(;;){c=getch();if(c==-32)switch(getch()){case 72: // 上cursor.x--;if(cursor.x<0) cursor.x=0;renew(chessboard,cursor.x+1,cursor.y);renew(chessboard,cursor.x,cursor.y);break;case 80: // 下cursor.x++;if(cursor.x>14) cursor.x=14;renew(chessboard,cursor.x-1,cursor.y);renew(chessboard,cursor.x,cursor.y);break;case 75: // 左cursor.y--;if(cursor.y<0) cursor.y=0;renew(chessboard,cursor.x,cursor.y+1);renew(chessboard,cursor.x,cursor.y);break;case 77: // 右cursor.y++;if(cursor.y>14) cursor.y=14;renew(chessboard,cursor.x,cursor.y-1);renew(chessboard,cursor.x,cursor.y);break;case 134: // 退出quit=true;return true;}else if(c==13 && chessboard[cursor.x][cursor.y].step==0)return true; // 下子else if(c=='U' || c=='u'){ // 悔棋regret=true;return true;}}}void beback(NODE chessboard[][15], int step){//悔棋int i,j,tempx,tempy;if(step==1) return; // 如果才开始,直接返回if(step>2){ // 如果下了多于两步for(i=0;i<15;i++) // 搜索前两步所下的位置for(j=0;j<15;j++) {if(chessboard[i][j].step==step-1){ // 找到上一步chessboard[i][j].step=0; // 清空棋子标志renew(chessboard,i,j);} // 重绘棋盘else if(chessboard[i][j].step==step-2){ // 找到上两步chessboard[i][j].step=0; // 清空棋子标志tempx=cursor.x; // 记录光标位置tempy=cursor.y;cursor.x=i; // 将光标回复到两步前的位置cursor.y=j;renew(chessboard,i,j); // 重绘棋盘renew(chessboard,tempx,tempy); // 重绘光标原本所在处}}}else if(step==2){ //如果下了一步for(i=0;i<15;i++) //搜索上一步所下的位置for(j=0;j<15;j++)if(chessboard[i][j].step==step-1){ // 找到上一步chessboard[i][j].step=0; // 清空棋子标志renew(chessboard,i,j);} // 重绘棋盘tempx=cursor.x; // 记录光标位置tempy=cursor.y;cursor.x=7; // 将光标移回棋盘中央cursor.y=7;renew(chessboard,i,j); // 重绘棋盘renew(chessboard,tempx,tempy); // 重绘光标原本所在处}}//------------------------------判断部分-------------------------------bool inside(int x, int y){// 如果不在棋盘内返回false,否则返回trueif(x<0 || x>14 || y<0 || y>14) return false;return(true);}int line(NODE chessboard[][15], int dirt, int x, int y, int color){// 判断颜色为color的点(x,y),在dirt方向上有多少相连的同色棋子int i;for(i=0;chessboard[x+direction[dirt][0]][y+direction[dirt][1]].step>0 && chessboard[x+direction[dirt][0]][y+direction[dirt][1]].color==color;i++) { x=x+direction[dirt][0];y=y+direction[dirt][1];if(!inside(x,y)) return i;}return i;}bool win(NODE chessboard[][15], int x, int y, int color){// 判断是否有人赢棋if(line(chessboard,0,x,y,color)+line(chessboard,1,x,y,color)>3)return true;if(line(chessboard,2,x,y,color)+line(chessboard,3,x,y,color)>3)return true;if(line(chessboard,4,x,y,color)+line(chessboard,5,x,y,color)>3)return true;if(line(chessboard,6,x,y,color)+line(chessboard,7,x,y,color)>3)return true;return false;}//--------------------------------AI部分----------------------------------int attacktrend,defenttrend; // 攻击防御平衡权值bool macth1(NODE chessboard[][15], int x, int y, int dirt, int kind, int color){/* 匹配在颜色为color的点(x,y),在dirt方向上的第kind种防守棋形,成功返回true,否则返回false */int k;char c;char *p;p=strchr(qx1[kind].qx,'x');for(k=0;k<=p-qx1[kind].qx;k++) {x-=direction[dirt][0];y-=direction[dirt][1];}for(k=0;(unsigned)k<strlen(qx1[kind].qx);k++) {x+=direction[dirt][0];y+=direction[dirt][1];if(!inside(x,y)) return(false);if(chessboard[x][y].step>0 && chessboard[x][y].color==color) c='2';else if(chessboard[x][y].step>0) c='1';else c='0';if(c=='0' && qx1[kind].qx[k]=='x') continue;if(c!=qx1[kind].qx[k]) return(false);}return true;}int value_qx1(NODE chessboard[][15], int x, int y, int dirt, int color){// 计算颜色为color的点8个方向上的防守权值之和int i;for(i=0;i<qx1_num;i++)if(macth1(chessboard,x,y,dirt,i,color))return qx1[i].value;return 0;}bool macth2(NODE chessboard[][15], int x, int y, int dirt, int kind, int color){/* 匹配在颜色为color的点(x,y),在dirt方向上的第kind种进攻棋形,成功返回true,否则返回false */int k;char c;char *p;p=strchr(qx2[kind].qx,'x');for(k=0;k<=p-qx2[kind].qx;k++) {x-=direction[dirt][0];y-=direction[dirt][1];}for(k=0;(unsigned)k<strlen(qx2[kind].qx);k++) {x+=direction[dirt][0];y+=direction[dirt][1];if(!inside(x,y)) return false;if(chessboard[x][y].step>0 && chessboard[x][y].color==color) c='2';else if(chessboard[x][y].step>0) c='1';else c='0';if(c=='0' && qx2[kind].qx[k]=='x') continue;if(c!=qx2[kind].qx[k]) return false;}return true;}int value_qx2(NODE chessboard[][15], int x, int y, int dirt, int color){// 计算颜色为color的点8个方向上的进攻权值之和int i;for(i=0;i<qx2_num;i++)if(macth2(chessboard,x,y,dirt,i,color))return qx2[i].value ;return 0;}void AI(NODE chessboard[][15], int *x, int *y, int color){// AI的主要函数,将思考后的结果返回给*x和*yint max=0; // 价值的最大值int maxi,maxj; // 获得最大值时所对应的坐标int i,j,k; // 循环控制变量int probability=1; // 几率参数int value[15][15]={0}; // 棋盘上各点的价值int valueattack[15][15]={{0}}; // 棋盘上各点的进攻价值int valuedefent[15][15]={{0}}; // 棋盘上各点的防守价值for(i=0;i<15;i++) // 计算棋盘上各点的防守价值for(j=0;j<15;j++) {if(chessboard[i][j].step>0) continue;for(k=0;k<8;k++)valuedefent[i][j]+=value_qx1(chessboard,i,j,k,color);if(maxi<valuedefent[i][j])maxi=valuedefent[i][j]; // 记录防守价值的最大值}for(i=0;i<15;i++) // 计算棋盘上各点的进攻价值for(j=0;j<15;j++) {if(chessboard[i][j].step>0) continue;for(k=0;k<8;k++)valueattack[i][j]+=value_qx2(chessboard,i,j,k,(color+1) % 2);if(maxj<valuedefent[i][j])maxj=valuedefent[i][j]; // 记录进攻价值的最大值}if(rand()%(maxi+maxj+1)>maxi){ // 如果防守价值的最大值比较低attacktrend=1; // AI优先进攻defenttrend=1;}else{ // 相反attacktrend=1; // AI优先防守defenttrend=2;}for(i=0;i<15;i++) // 根据各点的进攻和防守价值for(j=0;j<15;j++){ // 计算最终价值value[i][j]=valuedefent[i][j]*defenttrend+valueattack[i][j]*attacktrend;if(max<value[i][j]){max=value[i][j]; // 记录价值的最大值maxi=i; // 以及相应的坐标maxj=j;probability=1;}else if(max==value[i][j]){ // 如果出现相同价值的最大点if(rand()%(probability+1)<probability) // 随机决定选取哪一个probability++; /* 由于前面的点容易被淘汰,所以相应提高前面的点的被选择的几率*/else{probability=1; // 选择后面的点,则几率权值回复max=value[i][j]; // 记录maxi=i;maxj=j;}}}*x=maxi; // 返回价值最大的点*y=maxj;}//-----------------------主要部分------------------------------------bool vshuman; //对手是否是人void Vs(bool human){//对局主要函数int i,j;int color=1; // 黑棋先走int lastx,lasty; // 光标的上一个位置int computer; // 电脑执黑还是执白NODE chessboard[15][15]={{0,0}}; // 棋盘if(!human){ // 对手是电脑showsubmenu(); // 选择谁是先手switch(getchoose(1,3)){case 1:computer=0;attacktrend=1; // 电脑先手则优先进攻defenttrend=1;break;case 2:computer=1; // 电脑后手则优先防御attacktrend=1;defenttrend=2;break;case 3:return; // 返回上级菜单}}for(i=0;i<15;i++) // 清空棋盘for(j=0;j<15;j++)chessboard[i][j].step=0;cursor.x=7; // 光标居中cursor.y=7;quit=false; // 清空退出标志system("cls"); // 清屏printf("\n");printchessboard(chessboard); // 打印棋盘for(i=1;i<=225;){ // 行棋主循环gotoxy(0,0); // 系统光标移到屏幕左上角textcolor(TEXTS);printf(" 第%03d手,",i);if(color==1) printf("黑棋下");else printf("白棋下");regret=false; // 清空悔棋标志if(i>1){ // 第一子必须下在棋盘中央if(color!=computer || human) getmove(chessboard); // 该人走棋else{ // 该电脑走棋lastx=cursor.x; // 记录光标位置lasty=cursor.y;AI(chessboard,&cursor.x,&cursor.y,color); // 电脑走棋renew(chessboard,lastx,lasty); // 更新棋盘}}if(quit) return; // 如果按了退出热键则返回if(regret){ // 如果按了悔棋热键则调用悔棋函数beback(chessboard,i);if(i>2) i-=2;else if(i==2){i=1; color=(color+1) % 2 ;}}else{ // 如果没有按热键,则在光标位置下子chessboard[cursor.x][cursor.y].step=i++;chessboard[cursor.x][cursor.y].color=color;renew(chessboard,cursor.x,cursor.y);color=(color+1) % 2 ;}if(win(chessboard,cursor.x,cursor.y,(color+1) % 2) && !regret){// 有人赢textcolor(TEXTS);gotoxy(0,0);printf(" ");gotoxy(0,0);if(color==1) printf(" 白棋赢了!");else printf(" 黑棋赢了!");getch();return;}}gotoxy(0,0); // 如果下了225步还没人赢棋则平局printf(" ");gotoxy(0,0);printf(" 平局!");}void main(void){// 主函数srand((unsigned)time(NULL)); // 初始化随机种子for(;;){showmenu(); // 输出主菜单switch(getchoose(1,3)){case 1:Vs(false); break;case 2:Vs(true); break;case 3: printf("\n"); return;}} }。
c语言 五子棋 代码

settextstyle(2,0,8);
getch();
cleardevice();
setviewport(100,100,540,380,1);
/*定义一个图形窗口*/
setfillstyle(1,2);
break ;
}
case DOWN :
if((step_y+1)>18)
break ;
else
{
for(i=step_x,j=step_y+1;j<=18;j++)
break ;
else if(ch=='N'||ch=='n')
{
window(1,1,80,25);
textbackground(BLACK);
textcolor(LIGHTGRAY);
clrscr();
for(i=step_x,j=step_y-1;j>=1;j--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
judgewho(step_x,step_y);
break ;
}
case UP :
if((step_y-1)<0)
break ;
else
{
void attentoin();
void attention()
{
char ch ;
window(1,1,80,25);
textbackground(LIGHTBLUE);
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "graphics.h" /*图形系统头文件*/#define LEFT 0x4b00 /*光标左键值*/#define RIGHT 0x4d00 /*光标右键值*/#define DOWN 0x5000 /*光标下键值*/#define UP 0x4800 /*光标上键值*/#define ESC 0x011b /* ESC键值*/#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;/*棋手一开始得分都为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;/*每次棋子一开始出来的坐标,x为行坐标,y为列坐标*/ while(1) /*具体一个棋手走棋的过程*/{PrintScore(1);/*输出棋手1的成绩*/PrintScore(2);/*输出棋手2的成绩*/SetPlayColor(t);/*t变量是用来判断棋手所执棋子的颜色*/fillellipse(x,y,15,15);key=bioskey(0);/*接收按键*/if(key==ESC)/*跳出游戏*/break;elseif(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)/*如果是棋手1移动*/a[(x-120)/40][(y-120)/40]=1;else/*否则棋手2移动*/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;}elsecontinue;/*如果按键无效*/}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);}elseif(key==RIGHT&&x<400&&y>80)/*右方向键*/ {MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x+=40;fillellipse(x,y,15,15);}elseif(key==UP&&y>120)/*上方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y-=40;fillellipse(x,y,15,15);}elseif(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)/*格子已经占满或一方棋子为0判断胜负*/{playWin();/*输出最后结果*/break;}t=t%2+1; /*一方走后,改变棋子颜色即轮对方走*/cc=0; /*计数值恢复为0*/} /*endwhile*/}void SetPlayColor(int t)/*设置棋子颜色*/{if(t%2==1)setfillstyle(SOLID_FILL,15);/*白色*/elsesetfillstyle(SOLID_FILL,8);/*灰色*/}void MoveColor(int x,int y)/*走了一步后恢复原来格子的状态*/{if(y<100)/*如果是从起点出发就恢复蓝色*/setfillstyle(SOLID_FILL,BLUE);else/*其他情况如果是1就恢复白色棋子,2恢复黑色棋子,或恢复蓝色棋盘*/ 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);/*设置棋子变化的颜色*//*开始往8个方向判断变化*/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<k&&k<8;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<k&&k<8;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<k&&k<8;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>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<k&&k<8;ii++,jj++){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++;elseif(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!");elseif(score2<score1)outtextxy(100,50,"white win!");elseouttextxy(60,50,"you all win!");}。