五子棋C++代码
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语言程序代码
五子棋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做的五子棋源码
using System;using ;using System.Text;namespace wuziqi{class Program{public static void Main(string[] args){int maxsize = 19;//棋盘行列最大值int x, y;//用于定位下棋位置string x1, y1;//临时变量获取棋手输入的坐标值(此时坐标值属于字符串类型)int col_up = 0, col_down = 0;//用于统计输入坐标值的上下相同棋子的个数int row_left = 0, row_right = 0;//用于统计输入坐标值的左右相同棋子的个数int dia_left_up = 0, dia_left_down = 0, dia_right_up = 0, dia_right_down = 0;//对角线的英文拼写(diagonal line)用于统计对角线上相同的棋子数目int m;//临时变量,用于控制判断相同棋子比较的次数int judgement;//用于获取判断是否要结束棋局或继续棋局的变量string[,] qipan = new string[24, 24];//定义二维棋盘//初始化棋盘label0:{Console.Clear();//五子棋规则Console.WriteLine("===============================================");Console.WriteLine("五子棋规则说明如下:");Console.WriteLine("1、下棋坐标是从1-15");Console.WriteLine("2、下棋坐标值为整型数据类型");Console.WriteLine("3、下棋双方先构成五子连线者胜");Console.WriteLine("===============================================");for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){qipan[i, j] = "╋";}}}//输出棋盘for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){Console.Write(qipan[i, j]);}Console.Write("\n");}//走棋label2:{//甲方走棋Console.WriteLine("===============================================");Console.WriteLine("请甲方走棋(棋盘为15x15)");Console.Write("x=");x1 = Console.ReadLine();//判断输入的坐标值是否为字母foreach(char ch in x1){if ((ch >'a'&&ch <'z')||(ch >'A'&&ch <'Z')){Console.WriteLine("你输入的坐标值非法!请正确输入你的坐标值");goto label2;}}//判断输入的棋子坐标是否为空if (x1 == "" || x1 == null){Console.WriteLine("===============================================");Console.WriteLine("你输入的棋盘坐标为空!");goto label2;}Console.Write("y=");y1 = Console.ReadLine();//判断输入的坐标值是否为字母foreach (char ch in y1){if ((ch > 'a' && ch < 'z') || (ch > 'A' && ch < 'Z')){Console.WriteLine("你输入的坐标值非法!请正确输入你的坐标值");goto label2;}}//判断输入的棋子坐标是否为空if (y1 == "" || y1 == null){Console.WriteLine("===============================================");Console.WriteLine("你输入的棋盘坐标为空!");goto label2;}x = int.Parse(x1);y = int.Parse(y1);if (x >= 16 || y >= 16){Console.WriteLine("===============================================");Console.WriteLine("你输入的坐标超出了范围!");goto label2;}if (qipan[x + 4, y + 4] == "╋"){qipan[x + 4, y + 4] = "●";//清屏Console.Clear();//输出棋走后棋盘//五子棋规则Console.WriteLine("===============================================");Console.WriteLine("五子棋规则说明如下:");Console.WriteLine("1、下棋坐标是从1-15");Console.WriteLine("2、下棋坐标值为整型数据类型");Console.WriteLine("3、下棋双方先构成五子连线者胜");Console.WriteLine("===============================================");for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){Console.Write(qipan[i, j]);}Console.Write("\n");}}else{Console.WriteLine("===============================================");Console.WriteLine("此处已经有棋子了");goto label2;}// goto label;//判断是否可以连成五子//判断上下col_down = 0;col_up = 0;row_left = 0;row_right = 0;dia_left_down = 0;dia_left_up = 0;dia_right_down = 0;dia_right_up = 0;for (m = 1; m <= 4; m++){//判断上下if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){col_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){col_down++;}//判断左右if (qipan[x + 4, y + 4] == qipan[x + 4, y - m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){row_left++;}if (qipan[x + 4, y + 4] == qipan[x + 4, y + m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){row_right++;}//判断左对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y - m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_left_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_left_down++;}//判断右对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_right_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y - m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_right_down++;}}//判断甲方是否有构成五子if (dia_left_down == 4 || dia_left_up == 4 || col_up == 4 || col_down == 4 || row_right == 4 || row_left == 4 || dia_right_up == 4 || dia_right_down == 4){Console.WriteLine("甲方先构成五子连线,恭喜甲获胜!");Console.WriteLine("请选择:");Console.WriteLine("1、继续下一局");Console.WriteLine("2、结束棋局");judgement = int.Parse(Console.ReadLine());switch (judgement){case 1: goto label0;default: goto label3;}}if(((col_down + col_up) == 4) || ((row_left + row_right == 4)) || (dia_left_down + dia_left_up == 4) || (dia_right_down + dia_right_up == 4)){Console.WriteLine("甲方先构成五子连线,恭喜甲获胜!");Console.WriteLine("请选择:");Console.WriteLine("1、继续下一局");Console.WriteLine("2、结束棋局");judgement = int.Parse(Console.ReadLine());switch (judgement){case 1: goto label0;default: goto label3;}}//乙方走棋label1:{Console.WriteLine("===============================================");Console.WriteLine("请已方走棋(棋盘为15x15)");//判断坐标值是否为空Console.Write("x=");x1 = Console.ReadLine();//判断输入的坐标值是否为字母foreach (char ch in x1){if ((ch > 'a' && ch < 'z') || (ch > 'A' && ch < 'Z')){Console.WriteLine("你输入的坐标值非法!请正确输入你的坐标值");goto label1;}}if (x1 == "" || x1 == null){Console.WriteLine("你输入的棋盘坐标值为空!");goto label1;}x = int.Parse(x1);Console.Write("y=");y1 = Console.ReadLine();//判断输入的坐标值是否为字母foreach (char ch in y1){if ((ch > 'a' && ch < 'z') || (ch > 'A' && ch < 'Z')){Console.WriteLine("你输入的坐标值非法!请正确输入你的坐标值");goto label1;}}//判断是否为空if (y1 == "" || y1 == null){Console.WriteLine("你输入的棋盘坐标值为空!");goto label1;}y = int.Parse(y1);//对输入的棋子进行判断是否合法if (x >= 16 || y >= 16){Console.WriteLine("你输入的坐标超出了范围!");goto label1;}if (qipan[x + 4, y + 4] == "╋"){qipan[x + 4, y + 4] = "○";//输出棋走后棋盘//清屏Console.Clear();//五子棋规则Console.WriteLine("===============================================");Console.WriteLine("五子棋规则说明如下:");Console.WriteLine("1、下棋坐标是从1-15");Console.WriteLine("2、下棋坐标值为整型数据类型");Console.WriteLine("3、下棋双方先构成五子连线者胜");Console.WriteLine("===============================================");for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){Console.Write(qipan[i, j]);}Console.Write("\n");}}else{Console.WriteLine("此处已经有棋子了");goto label1;}//判断是否可以连成五子col_down = 0;col_up = 0;row_left = 0;row_right = 0;dia_left_down = 0;dia_left_up = 0;dia_right_down = 0;dia_right_up = 0;for (m = 1; m <= 4; m++){//判断上下if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + 4] && qipan[x - m + 4, y + 4] != "●" && qipan[x - m + 4, y + 4] != "╋"){col_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + 4] && qipan[x + m + 4, y + 4] != "●" && qipan[x + m + 4, y + 4] != "╋"){col_down++;}//判断左右if (qipan[x + 4, y + 4] == qipan[x + 4, y - m + 4] && qipan[x + 4, y - m + 4] != "●" && qipan[x + 4, y - m + 4] != "╋"){row_left++;}if (qipan[x + 4, y + 4] == qipan[x + 4, y + m + 4] && qipan[x + 4, y + m + 4] != "●" && qipan[x + 4, y + m + 4] != "╋"){row_right++;}//判断左对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y - m + 4] && qipan[x - m + 4, y - m + 4] != "●" && qipan[x - m + 4, y - m + 4] != "╋"){dia_left_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + m + 4] && qipan[x + m + 4, y + m + 4] != "●" && qipan[x + m + 4, y + m + 4] != "╋"){dia_left_down++;}//判断右对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + m + 4] && qipan[x - m + 4, y + m + 4] != "●" && qipan[x - m + 4, y + m + 4] != "╋"){dia_right_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y - m + 4] && qipan[x + m + 4, y - m + 4] != "●" && qipan[x + m + 4, y - m + 4] != "╋"){dia_right_down++;}}//判断甲方是否有构成五子if (dia_left_down == 4 || dia_left_up == 4 || col_up == 4 || col_down == 4 || row_right == 4 || row_left == 4 || dia_right_up == 4 || dia_right_down == 4){Console.WriteLine("===============================================");Console.WriteLine("乙方先构成五子连线,恭喜乙获胜!");Console.WriteLine("请选择:");Console.WriteLine("1、继续下一局");Console.WriteLine("2、结束棋局");judgement = int.Parse(Console.ReadLine());switch (judgement){case 1: goto label0;default: goto label3;}}if (((col_down + col_up) == 4) || ((row_left + row_right == 4)) ||(dia_left_down + dia_left_up == 4) || (dia_right_down + dia_right_up == 4)){Console.WriteLine("===============================================");Console.WriteLine("乙方先构成五子连线,恭喜乙获胜!");Console.WriteLine("请选择:");Console.WriteLine("1、继续下一局");Console.WriteLine("2、结束棋局");judgement = int.Parse(Console.ReadLine());switch (judgement){case 1: goto label0;default: goto label3;}}goto label2;}}//用于结束棋局label3:{}}}}。
c语言设计五子棋代码
c语言设计五子棋代码五子棋,亦称五目、五子、顺手拍子、连珠、下五子、悔棋、先手必胜等,是一种两人对弈的纯策略型棋类游戏。
通常用黑白两色的棋子在白色棋盘上进行游戏,以先把五个棋子连成一线者获胜。
实现五子棋游戏需要完成以下步骤:1. 棋盘的绘制2. 棋子的下落判断,包括判断是否胜利3. 玩家之间的交替下棋首先是棋盘的绘制。
采用二维数组来表示棋盘,用“+”、“-”、“|”等符号来绘制边框和交叉点。
同时还要用“ ”表示空白区域,用“●”和“○”代表黑白棋子。
然后是棋子的下落判断。
需要记录下棋子的位置,并判断落子是否在棋盘范围内,以及是否与已落下的棋子位置冲突。
在判断胜负时,需要遍历棋盘上所有的落子情况,检查是否存在连续的五个同色棋子。
最后是玩家之间的交替下棋。
可以使用一个计数器来记录当前下棋者,每次下完一子后,计数器加1,判断下一个下棋者,直到游戏结束为止。
下面是一份实现五子棋游戏的 C 代码:#include <stdio.h>#include <stdlib.h>#define ROW 15 // 棋盘行数#define COL 15 // 棋盘列数#define BLACK '●' // 黑色棋子#define WHITE '○' // 白色棋子#define SPACE ' ' // 空白区域char board[ROW][COL]; // 棋盘二维数组// 初始化棋盘void init_board() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {board[i][j] = SPACE;}}}// 打印棋盘void print_board() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {if (j == 0) printf("%2d ", i + 1); printf("%c ", board[i][j]);}printf("\n");}for (int i = 0; i < COL; i++) {if (i == 0) printf(" ");printf("%c ", 'A' + i);}printf("\n");}// 判断是否胜利int is_win(int row, int col, char ch) {int count = 1;// 判断横向for (int i = col - 1; i >= 0; i--) { if (board[row][i] == ch) {count++;} else {break;}}for (int i = col + 1; i < COL; i++) { if (board[row][i] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断纵向count = 1;for (int i = row - 1; i >= 0; i--) { if (board[i][col] == ch) {count++;} else {break;}}for (int i = row + 1; i < ROW; i++) {if (board[i][col] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断左上到右下斜向count = 1;for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == ch) {count++;} else {break;}}for (int i = row + 1, j = col + 1; i < ROW && j < COL; i++, j++) { if (board[i][j] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断左下到右上斜向count = 1;for (int i = row + 1, j = col - 1; i < ROW && j >= 0; i++, j--) { if (board[i][j] == ch) {count++;} else {break;}}for (int i = row - 1, j = col + 1; i >= 0 && j < COL; i--, j++) { if (board[i][j] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}return 0;}。
五子棋代码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语言五子棋代码
//五子棋小游戏纯C语言代码#include <stdio.h>#define N 14char state[N][N];void init(void);void printState(void);bool isWin(bool isBlack,int x,int y);bool isLevelWin(bool isBlack,int x,int y);bool isVerticalWin(bool isBlack,int x,int y);bool isLeftInclinedWin(bool isBlack,int x,int y);bool isRightObliqueWin(bool isBlack,int x,int y);bool isWin(bool isBlack,int x,int y)//是否有获胜{return isLevelWin(isBlack,x,y)||isVerticalWin(isBlack,x,y)||isLeftInclinedWin(isBlack,x,y)||isRightObliqueWin(isBlack,x,y);}bool isLevelWin(bool isBlack,int x,int y)//确定水平直线上是否有五子连珠{char c = isBlack ? '@':'O';int count;while(y>0 && state[x][y] == c){y--;}count =0;if(state[x][y] == c) count = 1;y++;while(y < N && state[x][y] == c){count++;if(count == 5){return true;}y++;}return false;}bool isVerticalWin(bool isBlack,int x,int y)//确定竖直直线是否有五子连珠{char c = isBlack ? '@':'O';int count;while(x>0 && state[x][y] == c){x--;}count =0;if(state[x][y] == c) count = 1;x++;while(x < N && state[x][y] == c){count++;if(count == 5){return true;}x++;}return false;}bool isLeftInclinedWin(bool isBlack,int x,int y)//确定左斜线是否有五子连珠{char c = isBlack ? '@':'O';int count;while(x>0 && y>0 && state[x][y] == c){y--;x--;}count =0;if(state[x][y] == c) count = 1;x++;y++;while(x < N && y < N && state[x][y] == c){count++;if(count == 5){return true;}x++;y++;}return false;}bool isRightObliqueWin(bool isBlack,int x,int y)//确定右斜线是否有五子连珠{char c = isBlack ? '@':'O';int count;while(x>0 && y<N && state[x][y] == c){y++;x--;}count =0;if(state[x][y] == c) count = 1;x++;y--;while(x < N && y >= 0 && state[x][y] == c){count++;if(count == 5){return true;}x++;y--;}return false;}void init(void)//开局初始化数组{int i,j;for(i=0;i<N;i++){for(j=0;j<N;j++){state[i][j] = '*';}}}void printState(void)//打印棋盘{int i,j;printf("%3c",' ');for(i=0;i<N;i++)printf("%3d",i);printf("\n");for(i=0;i<N;i++){printf("%3d",i);for(j=0;j<N;j++){printf("%3c",state[i][j]);}printf("\n");}}int main(void){int x,y;bool isBlack = true;init();printf("五子棋小游戏\n\n@代表黑子,0代表白子,*代表棋盘空白\n");printf("------------------------------------------------------\n");printState();while(1){printf("请%s 方走棋:\n",(isBlack?"黑":"白"));//请黑(白)方走棋的说明printf("输入所下位置坐标,如: 1-2\n");//走棋方法示例scanf("%d-%d",&x,&y);if(state[x][y]=='@' || state[x][y]=='O')//若此点已经存在棋子,则重新下一步棋在别处{printf("this position to have pieces\n");continue;}state[x][y] = (isBlack?'@':'O');//规定@代表黑子,0代表白子printState();//打印棋盘情况if(isWin(isBlack,x,y))//每下一步棋,判断一次是否有人获胜{printf("%s 方胜利\n",(isBlack?"黑":"白"));break;}isBlack = !isBlack;}}。
五子棋C语言代码
#include<stdio.h>int hen(char a[10][10]);int shu(char a[10][10]);int xie1(char a[10][10]);int xie2(char a[10][10]);int main(){char a[10][10];int x=0,y=0;for(y=0;y<10;y++)a[x][y]=y+'0';for(x=0,y=0;x<10;x++)a[x][y]=x+'0';for(x=1;x<10;x++)for(y=1;y<10;y++)a[x][y]='*';for(x=0;x<10;x++){for(y=0;y<10;y++)printf("%3c",a[x][y]);printf("\n");}printf("------------------------------\n");while(1){while(1){printf("请@号玩家下子\n");scanf("%d.%d",&x,&y);if(a[x][y]=='@'||a[x][y]=='O')printf("棋盘上已有棋子\n"); else{a[x][y]='@';break;}}for(x=0;x<10;x++){for(y=0;y<10;y++)printf("%3c",a[x][y]);printf("\n");}printf("------------------------------\n");if(hen(a)==1||shu(a)==1||xie1(a)==1||xie2(a)==1){printf("@玩家赢\n" );break;}while(1){printf("请O号玩家下子\n");scanf("%d.%d",&x,&y);if(a[x][y]=='@'||a[x][y]=='O')printf("棋盘上已有棋子\n"); else{a[x][y]='O';break;}}for(x=0;x<10;x++){for(y=0;y<10;y++)printf("%3c",a[x][y]);printf("\n");}printf("------------------------------\n");if(hen(a)==2||shu(a)==2||xie1(a)==2||xie2(a)==2){printf("O玩家赢了\n" );break;}}return 0;}int hen(char a[10][10])//横函数{int x,y,k=0,l=0;for(x=1;x<10;x++)for(y=1;y<10;y++){if(a[x][y]=='@')k=k+1;if(y==9)k=0;if(k==5)return 1;if(a[x][y]=='O')l=l+1;if(y==9)l=0;if(l==5)return 2;}}int shu(char a[10][10])//竖函数{int x,y,k=0,l=0;for(y=1;y<10;y++)for(x=1;x<10;x++){if(a[x][y]=='@')k=k+1;if(x==9)k=0;if(k==5)return 1;if(a[x][y]=='O')l=l+1;if(x==9)l=0;if(l==5)return 2;}}int xie1(char a[10][10])//斜函数{int x,y,m,k=0,l=0;for(m=1;m<10-4;m++)for(x=1,y=m;y<10;x++,y++){if(a[x][y]=='@')k=k+1;if(x==9)k=0;if(k==5)return 1;if(a[x][y]=='O')l=l+1;if(x==9)l=0;if(l==5)return 2;}}int xie2(char a[10][10])//斜函数{int x,y,n,k=0,l=0;for(n=1;n<10-4;n++)for(y=1,x=n;x<10;x++,y++){if(a[x][y]=='@')k=k+1;if(x==9)k=0;if(k==5)return 1;if(a[x][y]=='O')l=l+1;if(x==9)l=0;if(l==5)return 2;}}。
五子棋C语言程序
# include<stdio.h># include<string.h># include<stdlib.h># define SPA 0# define MAN 1# define COM 2 /* 空位置设为0 ,玩家下的位置设为1 ,电脑下的位置设为2 */int qipan[15][15]; /* 15*15的棋盘*/int a,b,c,d,x; /* a b为玩家下子坐标,c d为电脑下子坐标x为剩余空位置*/void start(); /* 程序的主要控制函数*/void draw(); /* 画棋盘*/int win(int p,int q); /* 判断胜利p q为判断点坐标*/void AI(int *p,int *q); /* 电脑下子p q返回下子坐标*/int value(int p,int q); /* 计算空点p q的价值*/int qixing(int n,int p,int q); /* 返回空点p q在n方向上的棋型n为1-8方向从右顺时针开始数*/void yiwei(int n,int *i,int *j); /* 在n方向上对坐标i j 移位n为1-8方向从右顺时针开始数*/void main(){char k;do{x=225;start();printf("还要再来一把吗?输入y或n:"); getchar(); scanf("%c",&k);while(k!='y'&&k!='n'){ printf("输入错误,请重新输入\n"); scanf("%c",&k); }system("cls");}while(k=='y');printf("谢谢使用!\n");}void start(){int i,j,a1,b1,c1,d1,choice; /* a1 b1储存玩家上手坐标c1 d1储存电脑上手坐标*/char ch;printf("\t╔═══════════════════════════════╗\n");printf("\t║║\n");printf("\t║ 欢迎使用五子棋对战程序祝您玩的愉快挑战无极限║\n");printf("\t║║\n");printf("\t║ ._______________________. ║\n");printf("\t║ | _____________________ | ║\n");printf("\t║ | I I | ║\n");printf("\t║ | I 五子棋I | ║\n");printf("\t║ | I I | ║\n");printf("\t║ | I made by 晓之蓬I | ║\n");printf("\t║ | I___________________I | ║\n");printf("\t║ !_______________________! ║\n");printf("\t║ ._[__________]_. ║\n");printf("\t║ .___|_______________|___. ║\n");printf("\t║ |::: ____ | ║\n");printf("\t║ | ~~~~ [CD-ROM] | ║\n");printf("\t║ !_____________________! ║\n");printf("\t║║\n");printf("\t║║\n");printf("\t║ 寒星溪月疏星首,花残二月并白莲。
五子棋C语言代码
#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!");}。
五子棋游戏程序(c)
closegraph(); //按Esc键结束
}
char Inputbox(void)
{
char a;
a=getch(); //读入用户输入
if(a!='0')
return a;
else
return '0';
}
////////////////////////////////
//判断五子连珠
////////////////////////////////
{
m++;
n=0;
if(m>=5)
{
drawtext("play1 win!", &r, DT_LEFT);
break;
}
}
else if(*(*(p+i*20)+i*20)=='W')
{
n++;
m=0;
if(n>=5)
{
drawtext("play2 win!", &r, DT_LEFT);
break;
//开始游戏模块
/////////////////////////////////
void start(char (*p)[400])
{
void win(char (*p)[400]);
int x,y,m,n,flag=0,e=1;
char a;
char Inputbox(void);
char s6[]="Input error!";
五子棋游戏程序
规则:1.执黑先行
2.最先在棋盘横向、纵向或斜向形成连续的
c语言五子棋代码
c语言五子棋代码#include <windows.h>#define MAX 20//绘制20X20的棋盘#define TextWidth 200//棋盘右边的宽度#define ERROR 0#define NO 0#define OK 1#define DEFAULT 0#define ICO_CUR 0x1000 //预定义光标的idint leng=1;HDC hdc,hdc1,hdc2;int xw,yw;int iGame[MAX][MAX];POINT point;//鼠标点击位置enum {Default,Player1,Player2}play; enum {Stop,Play,Paush}plays;void Init(HWND hwnd);void paint(int play,int x,int y); void chagePlayer();int Look(int x,int y,int play); void over(HWND hwnd,int play); LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);char szClassName[] = "超简单的五子棋";int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)//主函数{HWND hwnd;MSG messages;WNDCLASSEX wincl;wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS;wincl.cbSize = sizeof (WNDCLASSEX);wincl.hIcon = LoadIcon(hThisInstance,MAKEINTRESOURCE(ICO_CUR));wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);wincl.hCursor = LoadCursor (NULL, IDC_ARROW);wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl)) return 0;hwnd = CreateWindowEx (1, szClassName,szClassName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,800,600,HWND_DESKTOP,NULL,hThisInstance,NULL);ShowWindow (hwnd, nFunsterStil);//该函数设置指定窗口的显示状态 while (GetMessage (&messages, NULL, 0, 0)){TranslateMessage(&messages); DispatchMessage(&messages); }return messages.wParam; }LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam) //处理窗口事件{PAINTSTRUCT ps;static HPEN hpen,hpen1,hpen2; //设备static HBRUSH hbrush,hbrush1,hbrush2; //画刷的句柄int x,y;switch (message) //消息{case WM_KEYDOWN:switch (wParam){case VK_F2: //F2重新开始游戏Init(hwnd); //初始绘制游戏界面Rectangle(hdc,0,0,xw,yw); for(x=0;x<MAX;x++) //绘制棋盘for(y=0;y<MAX;y++) {Rectangle(hdc,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;iGame[x][y]=Default; }leng=1;plays=Play;break;case VK_F1:leng=1;plays=Play;break;}break;case WM_CREATE: //初始化窗体时调用plays=Stop;play=Player1;break;case WM_SIZE:xw=LOWORD(lParam); //获取窗体的宽度 yw=HIWORD(lParam); //获取窗体的高度 //TCHAR xx[20];//wsprintf(xx,"%d",xw); //MessageBox(hwnd,xx,xx,MB_OK);xw-=TextWidth; //xw保存棋盘的宽度InvalidateRect(hwnd,NULL,TRUE);//向窗体绘制一个矩形,支持重新绘制break;case WM_LBUTTONDOWN: //响应用户鼠标左键 //获取但前鼠标坐标point.x=LOWORD(lParam); point.y=HIWORD(lParam); //初始化设备DC Init(hwnd);//鼠标坐标换为数组坐标x=(point.x)/(xw/MAX); //定位到小狂格中去y=(point.y)/(yw/MAX);if(plays==Stop)break;if(x<MAX&&y<MAX) //未超出棋盘{if(iGame[x][y]==Default&&plays==Play)//判断但前位置是否有棋子覆盖是否是下棋模式 {leng=1;paint(play,x,y);if(Look(x,y,play))over(hwnd,play);chagePlayer();}}break;case WM_PAINT://开始绘制{hdc=BeginPaint(hwnd,&ps);Init(hwnd);Rectangle(hdc,0,0,xw,yw);for(x=0;x<MAX;x++)for(y=0;y<MAX;y++){Rectangle(hdc,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;paint(iGame[x][y],x,y);}EndPaint(hwnd,&ps);}break;case WM_DESTROY: //当用户关闭游戏PostQuitMessage (0); // 发送一个消息给message queue break;default: //什么都不做return DefWindowProc (hwnd, message, wParam, lParam);}return 0;}void Init(HWND hwnd) //初始化方法{hdc1=GetDC(hwnd);hdc2=GetDC(hwnd);//初始背景 (游戏的界面)SelectObject(hdc,CreatePen(0,1,RGB(0,0,0))); //小矩形框的边框的颜色SelectObject(hdc,CreateSolidBrush(RGB(255,218,185))); //填充的颜色 //初始玩家一图形SelectObject(hdc1,CreatePen(0,1,RGB(255,255,255))); //玩家一的棋子颜色 SelectObject(hdc1,CreateSolidBrush(RGB(255,255,255))); //填充白色 //初始玩家二图形SelectObject(hdc2,CreatePen(0,1,RGB(255,255,255))); //玩家二的棋子颜色 SelectObject(hdc2,CreateSolidBrush(RGB(0,0,0))); //填充黑色 //该背景分为两层背景,一层为游戏界面(hdc),一层为玩家的棋子(hdc1,hdc2) //SetBkMode(hdc,0);该方法好像没什么用//绘制右边操作提示区域Rectangle(hdc,xw,29,xw+200,30);//从xw开始绘制到xw+200结束(绘制长200宽为30-29的矩形)的线条TextOut(hdc,xw+45,13,"超简单的五子棋",14); //从坐标(xw+45)(13)开始绘制“超简单的五子棋”宽为14TextOut(hdc,xw+45,40,"F1 - 开始游戏",13);TextOut(hdc,xw+45,60,"F2 -重新开始游戏",16);Rectangle(hdc,xw,80,xw+200,81); TextOut(hdc,xw+45,150,"白棋为第二玩家",14);TextOut(hdc,xw+45,250,"黑棋为第二玩家",14);Rectangle(hdc,xw,330,xw+200,331); }void paint(int play,int x,int y) //画棋子{switch (play){case Player1:if(iGame[x][y]!=Default){//该地方已经有棋子了}else{Ellipse(hdc1,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;//Ellipse (圆心x坐标,圆心y坐标,起始角度,终止角度,x轴半径,y轴半径) }//绘制玩家player1的棋子iGame[x][y]=Player1; //将该地方标准已经有棋子break;case Player2:if(iGame[x][y]!=Default){}else{Ellipse(hdc2,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;}//绘制玩家player2的棋子iGame[x][y]=Player2; //将该地方标准已经有棋子break;}}void chagePlayer() //改变玩家{if(play==Player1) play=Player2; elseplay=Player1;}int Look(int x,int y,int play) // 检查模块{int i,tempx,tempy;tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempy--,i++); if(i>=5) return OK; //向上核对(y轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempy++,i++); if(i>=5) return OK; //向下核对(y轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx--,i++); if(i>=5) return OK; //向左核对(x轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx++,i++); if(i>=5) return OK; //向右核对(y轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx--,tempy--,i++); if(i>=5) return OK; //向左上核对(x,y轴的对角线向上\)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx++,tempy--,i++); if(i>=5) return OK; //向右上核对(x,y轴的对角线向下\)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx--,tempy++,i++); if(i>=5) return OK; //向左下核对(x,y轴的对角线向上/)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx++,tempy++,i++); if(i>=5) return OK; //向右下核对(x,y轴的对角线向上/)return NO;}void over(HWND hwnd,int play) //判断胜负{switch(play){case Player1:MessageBox(hwnd,"恭喜白棋玩家获得胜利","胜利",0);SendMessage(hwnd,WM_KEYDOWN,VK_F2,NULL); //重新开始游戏 plays=Stop;break;case Player2:MessageBox(hwnd,"恭喜黑棋玩家获得胜利","胜利",0);SendMessage(hwnd,WM_KEYDOWN,VK_F2,NULL);plays=Stop;break;}}最终效果图:。
如何用c语言做五子棋
如何用c语言做五子棋五子棋主要流行于华人和汉字文化圈的国家以及欧美一些地区,是世界上最古老的棋。
下面店铺给你介绍如何用c语言做五子棋,欢迎阅读。
用c语言做五子棋的方法五子棋游戏是一款很经典的智力游戏,只有学过编程语言的人,把五子棋的编程原理弄懂了,就能用自己熟悉的语言实现出来。
五子棋C语言代码如下#include#include#include#include#include#define CROSSRU 0xbf /*右上角点*/#define CROSSLU 0xda /*左上角点*/#define CROSSLD 0xc0 /*左下角点*/#define CROSSRD 0xd9 /*右下角点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*十字交叉点*//*定义棋盘左上角点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落子--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--方向键up*/#define PLAY2DOWN 0x5000/*下移--方向键down*/#define PLAY2LEFT 0x4b00/*左移--方向键left*/#define PLAY2RIGHT 0x4d00/*右移--方向键right*/#define PLAY2DO 0x1c0d/*落子--回车键Enter*//*若想在游戏中途退出, 可按 Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有无棋子 *//*若有棋子, 还应能指出是哪个玩家的棋子 */#define CHESSNULL 0 /*没有棋子*/#define CHESS1 'O'/*一号玩家的棋子*/#define CHESS2 'X'/*二号玩家的棋子*//*定义按键类别*/#define KEYEXIT 0/*退出键*/#define KEYFALLCHESS 1/*落子键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINVALID 3/*无效键*//*定义符号常量: 真, 假 --- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************/ /* 定义数据结构 *//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};/**********************************************************/ /*自定义函数原型说明 */void Init(void);int GetKey(void);int CheckKey(int press);int ChangeOrder(void);int ChessGo(int Order,struct point Cursor);void DoError(void);void DoOK(void);void DoWin(int Order);void MoveCursor(int Order,int press);void DrawCross(int x,int y);void DrawMap(void);int JudgeWin(int Order,struct point Cursor);int JudgeWinLine(int Order,struct point Cursor,int direction); void ShowOrderMsg(int Order);void EndGame(void);/**********************************************************/ /**********************************************************/ /* 定义全局变量 */int gPlayOrder; /*指示当前行棋方 */struct point gCursor; /*光标在棋盘上的位置 */char gChessBoard[19][19];/*用于记录棋盘上各点的状态*//**********************************************************/ /**********************************************************/ /*主函数*/void main(){int press;int bOutWhile=FALSE;/*退出循环标志*/printf("Welcome ");Init();/*初始化图象,数据*/while(1){press=GetKey();/*获取用户的按键值*/switch(CheckKey(press))/*判断按键类别*/{/*是退出键*/case KEYEXIT:clrscr();/*清屏*/bOutWhile = TRUE;break;/*是落子键*/case KEYFALLCHESS:if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/ DoError();/*落子错误*/else{DoOK();/*落子正确*//*如果当前行棋方赢棋*/if(JudgeWin(gPlayOrder,gCursor)==TRUE){DoWin(gPlayOrder);bOutWhile = TRUE;/*退出循环标志置为真*/}/*否则*/else/*交换行棋方*/ChangeOrder();ShowOrderMsg(gPlayOrder);}break;/*是光标移动键*/case KEYMOVECURSOR:MoveCursor(gPlayOrder,press);break;/*是无效键*/case KEYINVALID:break;}if(bOutWhile==TRUE)break;}/*游戏结束*/EndGame();}/**********************************************************/ /*界面初始化,数据初始化*/void Init(void){int i,j;char *Msg[]={"Player1 key:"," UP----w"," DOWN--s"," LEFT--a"," RIGHT-d"," DO----space","","Player2 key:"," UP----up"," DOWN--down"," LEFT--left"," RIGHT-right"," DO----ENTER","","exit game:"," ESC",NULL,};/* 先手方为1号玩家 */gPlayOrder = CHESS1;/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋子 */ for(i=0;i<19;i++)for(j=0;j<19;j++)gChessBoard[i][j]=CHESSNULL;/*光标初始位置*/gCursor.x=gCursor.y=0;/*画棋盘*/textmode(C40);DrawMap();/*显示操作键说明*/i=0;textcolor(BROWN);while(Msg[i]!=NULL){gotoxy(25,3+i);cputs(Msg[i]);i++;}/*显示当前行棋方*/ShowOrderMsg(gPlayOrder);/*光标移至棋盘的左上角点处*/gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*画棋盘*/void DrawMap(void){int i,j;clrscr();for(i=0;i<19;i++)for(j=0;j<19;j++)DrawCross(i,j);}/*画棋盘上的交叉点*/void DrawCross(int x,int y){gotoxy(x+MAPXOFT,y+MAPYOFT);/*交叉点上是一号玩家的棋子*/if(gChessBoard[x][y]==CHESS1){textcolor(LIGHTBLUE);putch(CHESS1);return;}/*交叉点上是二号玩家的棋子*/if(gChessBoard[x][y]==CHESS2){textcolor(LIGHTBLUE);putch(CHESS2); return;}textcolor(GREEN); /*左上角交叉点*/if(x==0&&y==0) {putch(CROSSLU); return;}/*左下角交叉点*/if(x==0&&y==18) {putch(CROSSLD); return;}/*右上角交叉点*/if(x==18&&y==0) {putch(CROSSRU); return;}/*右下角交叉点*/if(x==18&&y==18) {putch(CROSSRD); return;}/*左边界交叉点*/if(x==0){putch(CROSSL); return;}/*右边界交叉点*/if(x==18){putch(CROSSR); return;}/*上边界交叉点*/if(y==0){putch(CROSSU); return;}/*下边界交叉点*/if(y==18){putch(CROSSD); return;}/*棋盘中间的交叉点*/ putch(CROSS);}/*交换行棋方*/int ChangeOrder(void) {if(gPlayOrder==CHESS1) gPlayOrder=CHESS2;elsegPlayOrder=CHESS1;return(gPlayOrder);}/*获取按键值*/int GetKey(void){char lowbyte;int press;while (bioskey(1) == 0);/*如果用户没有按键,空循环*/press=bioskey(0);lowbyte=press&0xff;press=press&0xff00 + toupper(lowbyte); return(press);}/*落子错误处理*/void DoError(void){sound(1200);delay(50);nosound();}/*赢棋处理*/void DoWin(int Order){sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);nosound();textcolor(RED+BLINK);gotoxy(25,20);if(Order==CHESS1)cputs("PLAYER1 WIN!");elsecputs("PLAYER2 WIN!");gotoxy(25,21);cputs(" \\<^+^>/");getch();}/*走棋*/int ChessGo(int Order,struct point Cursor){/*判断交叉点上有无棋子*/if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL) {/*若没有棋子, 则可以落子*/gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); textcolor(LIGHTBLUE);putch(Order);gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); gChessBoard[Cursor.x][Cursor.y]=Order;return TRUE;}elsereturn FALSE;}/*判断当前行棋方落子后是否赢棋*/int JudgeWin(int Order,struct point Cursor){int i;for(i=0;i<4;i++)/*判断在指定方向上是否有连续5个行棋方的棋子*/if(JudgeWinLine(Order,Cursor,i))return TRUE;return FALSE;}/*判断在指定方向上是否有连续5个行棋方的棋子*/int JudgeWinLine(int Order,struct point Cursor,int direction) {int i;struct point pos,dpos;const int testnum = 5;int count;switch(direction){case 0:/*在水平方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y;dpos.x=1;dpos.y=0;break;case 1:/*在垂直方向*/pos.x=Cursor.x;pos.y=Cursor.y-(testnum-1);dpos.x=0;dpos.y=1;break;case 2:/*在左下至右上的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y+(testnum-1);dpos.x=1;dpos.y=-1;break;case 3:/*在左上至右下的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y-(testnum-1);dpos.x=1;dpos.y=1;break;}count=0;for(i=0;i{if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18) {if(gChessBoard[pos.x][pos.y]==Order){count++;if(count>=testnum)return TRUE;}elsecount=0;}pos.x+=dpos.x;pos.y+=dpos.y;}return FALSE;}/*移动光标*/void MoveCursor(int Order,int press) {switch(press){case PLAY1UP:if(Order==CHESS1&&gCursor.y>0) gCursor.y--;break;case PLAY1DOWN:if(Order==CHESS1&&gCursor.y<18) gCursor.y++;break;case PLAY1LEFT:if(Order==CHESS1&&gCursor.x>0) gCursor.x--;break;case PLAY1RIGHT:if(Order==CHESS1&&gCursor.x<18) gCursor.x++;break;case PLAY2UP:if(Order==CHESS2&&gCursor.y>0) gCursor.y--;break;case PLAY2DOWN:if(Order==CHESS2&&gCursor.y<18)gCursor.y++;break;case PLAY2LEFT:if(Order==CHESS2&&gCursor.x>0)gCursor.x--;break;case PLAY2RIGHT:if(Order==CHESS2&&gCursor.x<18)gCursor.x++;break;}gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*游戏结束处理*/void EndGame(void){textmode(C80);}/*显示当前行棋方*/void ShowOrderMsg(int Order){gotoxy(6,MAPYOFT+20);textcolor(LIGHTRED);if(Order==CHESS1)cputs("Player1 go!");elsecputs("Player2 go!");gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);}/*落子正确处理*/void DoOK(void){sound(500);delay(70);sound(600);delay(50);sound(1000);delay(100);nosound();}/*检查用户的按键类别*/int CheckKey(int press){if(press==ESCAPE)return KEYEXIT;/*是退出键*/elseif( ( press==PLAY1DO && gPlayOrder==CHESS1) || ( press==PLAY2DO && gPlayOrder==CHESS2) )return KEYFALLCHESS;/*是落子键*/elseif( press==PLAY1UP || press==PLAY1DOWN || press==PLAY1LEFT || press==PLAY1RIGHT || press==PLAY2UP || press==PLAY2DOWN || press==PLAY2LEFT || press==PLAY2RIGHT)return KEYMOVECURSOR;/*是光标移动键*/ elsereturn KEYINVALID;/*按键无效*/}。
五子棋游戏 C语言 代码 源代码
#include <stdio.h>#include <stdlib.h>#define m 30int main (void){int count;//计数器算横纵行的结果int w,h;int u;int l;int i,size;//i声明步数。
size声明int r[m][m] = {0};//数组声明(棋子位置)int x, y;//声明落子坐标int n;//声明棋盘大小nchar a[20],b[20];printf ("请输入棋盘大小n\n");//编辑棋盘直到棋盘长度宽度大于4小于30 scanf ("%d", &n);if (n<=4 || n>m){do{printf ("输入的棋盘大小:4<n<%d\n", m);scanf ("%d", &n);}while (n<=4 || n>m);}getchar ();//声明玩家printf ("请输入玩家1姓名:\n");gets(a);printf ("请输入玩家2姓名:\n");gets(b);for ( i = 1, size = n*n;i <= size; i++)//编辑棋盘{if (i%2 == 1)//如果i能被2整除,为玩家a相关信息{do//玩家a棋子信息{printf ("%s该你下棋了,第%d个棋子\n", a, i);scanf ("%d%d", &x, &y);if (x > n || x < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>m || x<0);}if (y > n || y < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 1;for (u = 0;u < n; u++)//不同情况下判断玩家a获胜方式{for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 1 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;//直接跳转,其余代码不在运行count = 0;for (w = u, h = l; r[w][h] == 1 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w = u,h = l; r[w][h] == 1 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 1 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}}}}system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++)//画棋盘,声明两玩家棋子图片{if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●"); }printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k);}else if (i%2 == 0)//如果i不能被2整除,为玩家b相关信息{do{printf ("\n%s该你下棋了,第%d个棋子\n", b, i);scanf ("%d%d", &x, &y);if (x > n || x < 0){do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>n || x<0);}if (y >n|| y < 0){do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 2;system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");count = 0;for (u = 0;u < n; u++){for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 2 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u, h = l; r[w][h] == 2 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u,h = l; r[w][h] == 2 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 2 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}}}}}e: for (int j = n;j>=0;j--)//游戏结束界面棋盘固定重新显示{printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");printf ("\a游戏愉快,Powered by Techmessager\n");//结束语句return 0;}。
c++五子棋程序代码
#include<iostream>#include<cstdlib>using namespace std;const char outstr[11][4]={"┌","┬","┐","├","┼","┤","└","┴","┘","○","●"};int com[15][15],luozi[15][15];int c,d;void jushi() //这是判断落子重要性的函数{int x,y,i,m,n,lianzi1=0,lianzi2=0,life=0;memset(com,0,sizeof(com));for(x=0;x<=14;x++)for(y=0;y<=14;y++){if(luozi[x][y]==0){for(m=-1;m<=1;m++)for(n=-1;n<=1;n++){if(m!=0 || n!=0){for(i=1;i<=4;i++){if( x+i*m>=0 && x+i*m<=14 && y+i*n>=0 && y+i*n<=14 && luozi[x+i*m][y+i*n]==1 ){lianzi1++;}elseif(luozi[x+i*m][y+i*n]==0){life++; break;}else{break;}}for(i=-1;i>=-4;i--){if( x+i*m>=0 && x+i*m<=14 && y+i*n>=0 && y+i*n<=14 && luozi[x+i*m][y+i*n]==1 ){lianzi1++;}elseif(luozi[x+i*m][y+i*n]==0){life++; break;}else{break;}}if(lianzi1==1) {com[x][y]+=1;}else if(lianzi1==2){if(life==1) {com[x][y]+=5;}else if(life==2) {com[x][y]+=10;} }else if(lianzi1==3){if(life==1) {com[x][y]+=20;}else if(life==2) {com[x][y]+=100;}}else if(lianzi1==4) {com[x][y]+=500;}life=0;for(i=1;i<=4;i++){if( x+i*m>=0 && x+i*m<=14 && y+i*n>=0 && y+i*n<=14 && luozi[x+i*m][y+i*n]==2 ){lianzi2++;}elseif(luozi[x+i*m][y+i*n]==0){life++; break;}else{break;}}for(i=-1;i>=-4;i--){if( x+i*m>=0 && x+i*m<=14 && y+i*n>=0 && y+i*n<=14 && luozi[x+i*m][y+i*n]==2 ){lianzi2++;}elseif(luozi[x+i*m][y+i*n]==0){life++; break;}else{break;}}if(lianzi2==1) {com[x][y]+=2;}else if(lianzi2==2){if(life==1) {com[x][y]+=8;}else if(life==2) {com[x][y]+=30;}}else if(lianzi2==3){if(life==1) {com[x][y]+=50;}else if(life==2) {com[x][y]+=200;}}else if(lianzi2==4) {com[x][y]+=1000;}lianzi1=0;lianzi2=0;life=0;}}}}}void computer(int a[15][15]) //这是电脑落子的函数{int i,j,max=0;jushi();for(i=0;i<=14;i++)for(j=0;j<=14;j++){if(com[i][j]>max){max=com[i][j];c=i;d=j;}}a[c][d]=10;luozi[c][d]=2;}int panduan(int x,int y,int sum) //判断胜负的函数{int m,n,i,lianzi=0;for(m=-1;m<=1;m++)for(n=-1;n<=1;n++){if(m!=0 || n!=0){for(i=1;i<=4;i++){if( x+i*m>=0 && x+i*m<=14 && y+i*n>=0 && y+i*n<=14 && luozi[x+i*m][y+i*n]==sum ){lianzi++;}else{break;}}for(i=-1;i>=-4;i--){if( x+i*m>=0 && x+i*m<=14 && y+i*n>=0 && y+i*n<=14 && luozi[x+i*m][y+i*n]==sum ){lianzi++;}else{break;}}if(lianzi>=4){return 1;}else{lianzi=0;}}}return 0;}int main() //主函数,主要进行人落子工作{system ("color 2f");system ("mode con cols=50 lines=25");cout<<"******************游戏规则*********************"<<endl;cout<<"(1)哪一方先让五个棋子形成一条直线(包括横、竖、对角线3个方向),即为获胜"<<endl;cout<<"(2)按左右上下方向键移动棋子"<<endl;cout<<"(3)按空格键确定落棋子"<<endl;cout<<"(4)禁止在棋盘外按空格"<<endl;cout<<"你是否接受上述的游戏规则(Y/N),请按y或n"<<endl;char c;cin>>c;if(c=='Y'||c=='y'){/*system ("color 2f");system ("mode con cols=50 lines=25");*/system ("title 欢迎使用!");int a[15][15]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,8};int x,y,i,j;memset(luozi,0,sizeof(luozi));cout<<" 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4"<<endl;for(i=0;i<=14;i++){if(i!=0)cout<<endl;cout<<i%10;for(j=0;j<=14;j++){cout<<outstr[a[i][j]];}}cout<<endl;cout<<"请输入您要下的棋子行\\列用空格隔开:";while(cin>>x>>y){if(luozi[x][y]==0 && x>=0 && x<=14 && y>=0 && y<=14){a[x][y]=9;luozi[x][y]=1;if(panduan(x,y,1)==0){system ("cls");computer(a);if(panduan(c,d,2)==0){cout<<" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4"<<endl;for(i=0;i<=14;i++){if(i!=0)cout<<endl;cout<<i%10;for(j=0;j<=14;j++){cout<<outstr[a[i][j]];}}cout<<endl;cout<<"您的棋子落在"<<x<<"行"<<" "<<y<<"列"<<endl;cout<<"电脑的棋子落在"<<c<<"行"<<" "<<d<<"列"<<endl; cout<<"请输入您要下的棋子";}else{cout<<" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4"<<endl;for(i=0;i<=14;i++){if(i!=0)cout<<endl;cout<<i%10;for(j=0;j<=14;j++){cout<<outstr[a[i][j]];}}cout<<endl;cout<<"电脑的棋子落在"<<c<<"行"<<" "<<d<<"列"<<endl;cout<<"电脑获胜!"<<endl;break;}}else{system ("cls"); //清屏cout<<" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4"<<endl;for(i=0;i<=14;i++){if(i!=0)cout<<endl;cout<<i%10;for(j=0;j<=14;j++){cout<<outstr[a[i][j]];}}cout<<endl;cout<<"您获胜啦!"<<endl;break;}}else{cout<<"输入错误,请重新输入:"<<endl;}}system ("title 谢谢使用!");system ("color 03");cout<<"谢谢使用"<<endl;system("pause>nul");}return 0;}。
c语言简易五子棋cmd程序代码
p=0;
}
}
p=0;
k=panding(i,j,h,s);/*判定*//*用上面定义的panding()判定这个子是否与其他子相连大于等于5个*/
}
while(b==2&&k==0)/*白棋步骤与黑棋类似*/
char h=2,w=1;/*用于上面panding()函数的输入,在上一个函数最后需要输入来判定胜利的是黑子还是白子*/
while(i<25)/*将横向数组用‘+’和‘ ’间隔表示*/
{
while(j<=50)
{
s[i][j]='+';/*将可以下子的位置显示为‘+’方便计算和观察*/
while(b==1&&k==0)/*在黑子步骤且无人胜利的情况下进行黑子步骤*/
{
printf("请下黑棋\n");
while(p==0)/*下子之前计算好输入值如实际值的对应,这里用循环是需要判定输入的坐标是否符合要求,如不符合则循环输入直到符合为止*/
{
while(p=;p);
if(p==1)
{
k=0;
b=1;
p=1;
i=0;
j=0;
while(i<25)
{
{
printf("请下白棋\n");
while(p==0)
{
scanf("%d%d",&j,&i);
if(s[24-i][j*2]=='+'&&i>0&&i<25&&j>0&&j<25)
五子棋游戏代码—C语言
C语言游戏之五子棋源代码#include<stdio.h>#include<stdlib.h>#include<graphics.h>#include<bios.h>#include<conio.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b#define SPACE 0x3920#define BILI 20#define JZ 4#define JS 3#define N 19int box[N][N];int step_x,step_y ;int key ;int flag=1 ;void draw_box();void draw_cicle(int x,int y,int color); void change();void judgewho(int x,int y);void judgekey();int judgeresult(int x,int y);void attentoin();void attention(){char ch ;window(1,1,80,25);textbackground(LIGHTBLUE);textcolor(YELLOW);clrscr();gotoxy(15,2);printf("游戏操作规则:");gotoxy(15,4);printf("Play Rules:");gotoxy(15,6);printf("1、按左右上下方向键移动棋子");gotoxy(15,8);printf("1. Press Left,Right,Up,Down Key to move Piece");gotoxy(15,10);printf("2、按空格确定落棋子");gotoxy(15,12);printf("2. Press Space to place the Piece");gotoxy(15,14);printf("3、禁止在棋盘外按空格");gotoxy(15,16);printf("3. DO NOT press Space outside of the chessboard");gotoxy(15,18);printf("你是否接受上述的游戏规则(Y/N)");gotoxy(15,20);printf("Do you accept the above Playing Rules? [Y/N]:");while(1){gotoxy(60,20);ch=getche();if(ch=='Y'||ch=='y')break ;else if(ch=='N'||ch=='n'){window(1,1,80,25);textbackground(BLACK);textcolor(LIGHTGRAY);clrscr();exit(0);}gotoxy(51,12);printf(" ");}}void draw_box(){int x1,x2,y1,y2 ;setbkcolor(LIGHTBLUE);setcolor(YELLOW);gotoxy(7,2);printf("Left, Right, Up, Down KEY to move, Space to put, ESC-quit.");for(x1=1,y1=1,y2=18;x1<=18;x1++) line((x1+JZ)*BILI,(y1+JS)*BILI,(x 1+JZ)*BILI,(y2+JS)*BILI);for(x1=1,y1=1,x2=18;y1<=18;y1++) line((x1+JZ)*BILI,(y1+JS)*BILI,(x 2+JZ)*BILI,(y1+JS)*BILI);for(x1=1;x1<=18;x1++)for(y1=1;y1<=18;y1++)box[x1][y1]=0 ;}void draw_circle(int x,int y,int color) {setcolor(color);setlinestyle(SOLID_LINE,0,1);x=(x+JZ)*BILI ;y=(y+JS)*BILI ;circle(x,y,8);}void judgekey(){int i ;int j ;switch(key){case LEFT :if(step_x-1<0)break ;else{for(i=step_x-1,j=step_y;i>=1;i --)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLU E);break ;}if(i<1)break ;step_x=i ;judgewho(step_x,step_y);break ;}case RIGHT :if(step_x+1>18)break ;else{for(i=step_x+1,j=step_y;i<=18 ;i++)if(box[i][j]==0){draw_circle(step_x,step_y,LIGH TBLUE);break ;}if(i>18)break ;step_x=i ;judgewho(step_x,step_y);break ;}case DOWN :if((step_y+1)>18)break ;else{for(i=step_x,j=step_y+1;j<=18 ;j++)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLU E);break ;}if(j>18)break ;step_y=j ;judgewho(step_x,step_y);break ;}case UP :if((step_y-1)<0)break ;else{for(i=step_x,j=step_y-1;j>=1;j --)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLU E);break ;}if(j<1)break ;step_y=j ;judgewho(step_x,step_y);break ;}case ESC :break ;case SPACE :if(step_x>=1&&step_x<=18&&s tep_y>=1&&step_y<=18){if(box[step_x][step_y]==0){box[step_x][step_y]=flag ; if(judgeresult(step_x,step_y)==1){sound(1000);delay(1000);nosound();gotoxy(30,4);if(flag==1){setbkcolor(BLUE);cleardevice(); setviewport(100,100,540,380,1);/*定义一个图形窗口*/setfillstyle(1,2);/*绿色以实填充*/setcolor(YELLOW);rectangle(0,0,439,279);floodfill(50,50,14);setcolor(12);settextstyle(1,0,5);/*三重笔划字体, 水平放?5倍*/ outtextxy(20,20,"The White Win !");setcolor(15);settextstyle(3,0,5);*无衬笔划字体, 水平放大5倍*/ uttextxy(120,120,"The White Win !");setcolor(14);settextstyle(2,0,8);getch();closegraph();exit(0);}if(flag==2){setbkcolor(BLUE);cleardevice();setviewport(100,100,540,380,1);/*定义一个图形窗口*/setfillstyle(1,2);/*绿色以实填充*/setcolor(YELLOW);rectangle(0,0,439,279);floodfill(50,50,14);setcolor(12);settextstyle(1,0,8);笔划字体, 水平放大8倍*/ outtextxy(20,20,"The Red Win !");setcolor(15);settextstyle(3,0,5);/*无衬笔划字体, 水平放大5倍*/outtextxy(120,120,"The Red Win !");setcolor(14);settextstyle(2,0,8);getch();closegraph();exit(0);}}change();break ;}}elsebreak ;}}void change(){if(flag==1)flag=2 ;elseflag=1 ;}void judgewho(int x,int y){if(flag==1)draw_circle(x,y,15);if(flag==2)draw_circle(x,y,4);}int judgeresult(int x,int y){ int j,k,n1,n2 ;while(1){ n1=0 ;n2=0 ;/*水平向左数*/for(j=x,k=y;j>=1;j--){ if(box[j][k]==flag)n1++;elsebreak ;}/*水平向右数*/for(j=x,k=y;j<=18;j++) {if(box[j][k]==flag)n2++;elsebreak ;}if(n1+n2-1>=5){return(1);break ;}/*垂直向上数*/n1=0 ;n2=0 ;for(j=x,k=y;k>=1;k--) {if(box[j][k]==flag)n1++;elsebreak ;}/*垂直向下数*/for(j=x,k=y;k<=18;k++) {if(box[j][k]==flag)n2++;elsebreak ;}if(n1+n2-1>=5){return(1);break ;}/*向左上方数*/n1=0 ;n2=0 ;for(j=x,k=y;j>=1,k>=1;j--,k--){if(box[j][k]==flag)n1++;elsebreak ;}/*向右下方数*/for(j=x,k=y;j<=18,k<=18;j++,k+ +){if(box[j][k]==flag)n2++;elsebreak ;}if(n1+n2-1>=5){return(1);break ;}/*向右上方数*/n1=0 ;n2=0 ;for(j=x,k=y;j<=18,k>=1;j++,k--){if(box[j][k]==flag)n1++;elsebreak ;}/*向左下方数*/for(j=x,k=y;j>=1,k<=18;j--,k++){if(box[j][k]==flag)n2++;elsebreak ;}if(n1+n2-1>=5){ return(1);break ;}return(0);break ;} }void main(){int gdriver=VGA,gmode=VGAHI;clrscr();attention();initgraph(&gdriver,&gmode,"c:\\tc" );/* setwritemode(XOR_PUT);*/flag=1 ;draw_box();do{step_x=0 ;step_y=0 ;/*draw_circle(step_x,step_y,8); */judgewho(step_x-1,step_y-1);do{while(bioskey(1)==0);key=bioskey(0);judgekey();}while(key!=SPACE&&key!=ESC);}while(key!=ESC);closegraph();}。
C语言五子棋游戏源代码
i n c l u d e<> define N 10void welcome;void initqipan;void showqiint i;void saveint p;void panduanint p;void heqi;void over;int zouqihang;int zouqilie;/结构体/struct zuobiao{int xNN;int yNN;}weizhiNN;/主函数/void main{int p=0;welcome;initqipan;forp=1;p<=NN;p++{weizhip.xp=zouqihang; weizhip.yp=zouqilie; savep;showqip;panduanp;}ifp==NNheqi;over;}/建立棋盘/void initqipan{int i,j;fori=0;i<N;i++{printf"%d",i;printf" ";}printf"\n";fori=1;i<N;i++{forj=0;j<N;j++{ifj==0printf"%d",i; elseprintf"·";}printf"\n";}}/显示棋子/void showqiint p{int i,j,k,m;int aNN,bNN;FILE fp;fp=fopen"wuzi_list","rb";fori=1;i<=NN;i++{fread&weizhii,sizeofstruct zuobiao,1,fp; ai=weizhii.xi;bi=weizhii.yi;}form=1;m<p;m++{whileweizhip.xp==am&&weizhip.yp==bm{printf"error\n";weizhip.xp=zouqihang;weizhip.yp=zouqilie;m=1;}}fori=0;i<N;i++{printf"%d",i;printf" ";}printf"\n";fori=1;i<N;i++{forj=1;j<N;j++{ifj==1printf"%d",i;fork=1;k<=p;k++{ifi==weizhik.xk&&j==weizhik.yk {ifk%2==1{printf"○";break;}else ifk%2==0{printf"●";break;}}}ifk>pprintf"·";else continue;}printf"\n";}}/走棋行/int zouqihang{int x;printf"请输入要走棋子所在行数\n";printf"x=";scanf"%d",&x;whilex>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;whiley>N-1||y<1{printf"error\n";printf"请输入要走棋子所在列数\n";printf"y=";scanf"%d",&y;}return y;}/文件保存/void saveint i{FILE fp;fp=fopen"wuzi_list","wb";fwrite&weizhii,sizeofstruct zuobiao,1,fp; }/判断输赢/void panduanint p{int i,j,k8={1,1,1,1,1,1,1,1,};int aNN,bNN;FILE fp;fp=fopen"wuzi_list","rb";fori=1;i<=p;i++{fread&weizhii,sizeofstruct zuobiao,1,fp; ai=weizhii.xi;bi=weizhii.yi;}/判断行/fori=1;i<=p;i++{ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj&&bi==bj-1{k0++;continue;}else ifai==aj&&bi==bj-2{k0++;continue;}else ifai==aj&&bi==bj-3{k0++;continue;}else ifai==aj&&bi==bj-4{k0++;continue;}else ifk0==5{printf"Player 1 wins\n"; }elsecontinue;}ifk0==5break;k0=1;}else ifk0==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj&&bi==bj-1{k1++;continue;}else ifai==aj&&bi==bj-2 {k1++;continue;}else ifai==aj&&bi==bj-3 {k1++;continue;}else ifai==aj&&bi==bj-4{k1++;continue;}else ifk1==5{printf"Player 2 wins\n"; }elsecontinue;}ifk1==5break;k1=1;}}/判断列/fori=1;i<=p;i++{ifk0==5||k1==5break;else ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj-1&&bi==bj{k2++;continue;}else ifai==aj-2&&bi==bj {k2++;continue;}else ifai==aj-3&&bi==bj {k2++;continue;}else ifai==aj-4&&bi==bj {k2++;continue;else ifk2==5{printf"Player 1 wins\n"; }elsecontinue;}ifk2==5break;k2=1;}else ifk2==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj-1&&bi==bj{k3++;continue;else ifai==aj-2&&bi==bj{k3++;continue;}else ifai==aj-3&&bi==bj{k3++;continue;}else ifai==aj-4&&bi==bj{k3++;continue;}else ifk3==5{printf"Player 2 wins\n"; }elsecontinue;}ifk3==5break;k3=1;}}/判断对角左上-右下/fori=1;i<=p;i++{ifk0==5||k1==5||k2==5||k3==5break;else ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj-1&&bi==bj-1{k4++;continue;}else ifai==aj-2&&bi==bj-2 {k4++;continue;}else ifai==aj-3&&bi==bj-3{k4++;continue;}else ifai==aj-4&&bi==bj-4{k4++;continue;}else ifk4==5{printf"Player 1 wins\n"; }elsecontinue;}ifk4==5break;}else ifk2==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj-1&&bi==bj-1{k5++;continue;}else ifai==aj-2&&bi==bj-2 {k5++;continue;}else ifai==aj-3&&bi==bj-3 {k5++;continue;else ifai==aj-4&&bi==bj-4{k5++;continue;}else ifk5==5{printf"Player 2 wins\n";}elsecontinue;}ifk5==5break;k5=1;}}/判断对角左下-右上/fori=1;i<=p;i++{ifk0==5||k1==5||k2==5||k3==5||k4==5||k5==5else ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj+1&&bi==bj-1{k6++;continue;}else ifai==aj+2&&bi==bj-2 {k6++;continue;}else ifai==aj+3&&bi==bj-3 {k6++;continue;}else ifai==aj+4&&bi==bj-4 {k6++;continue;}else ifk6==5{printf"Player 1 wins\n"; }elsecontinue;}ifk6==5break;k6=1;}else ifk6==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj+1&&bi==bj-1{k7++;continue;}else ifai==aj+2&&bi==bj-2{k7++;continue;}else ifai==aj+3&&bi==bj-3{k7++;continue;}else ifai==aj+4&&bi==bj-4{k7++;continue;}else ifk7==5{printf"Player 2 wins\n"; }elsecontinue;}ifk7==5break;k7=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";}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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(Cx-i>0&&Cy+i<MAXIMUS&&p[Cx-i][Cy+i]==Now)z++;else break;//向左下检查
if(z>=5)return Now;//若果达到5个则判断当前走子玩家为赢家
return 0;//若没有检查到五连珠,则返回0表示还没有玩家达成胜利
{
if(i!=MAXIMUS)
write(" ");
}
else//如果在棋盘中间则用竖线承接上下
{
if(i==0||i==MAXIMUS-1)//左右边缘的竖线更粗
}
system("cls");//清理屏幕,准备写入
for(i=0;i<MAXIMUS*2+1;i++)
{//循环写入每一行
printf("%s",buff[i]);
if(i<MAXIMUS*2)//写入完每一行需要换行
printf("\n");
if(i!=MAXIMUS-1)//如果不在最右侧则补充一个横线承接左右
{
if(j==0||j==MAXIMUS-1)
{
write("━");//上下边缘的横线更粗
{st;
while (*strSrc)
{
*strDest++=*strSrc++;
}
return strDestCopy;
}
void Initialize()//初始化一个对局函数
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
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(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;//向右检查
{
Offset--;
}
Copy(buff[MAXIMUS]+Offset,showText);//讲中间文字信息复制到缓冲器
if(l%2==1)//如果中间文字长度为半角奇数,则补上空格,避免混乱
{
*(buff[MAXIMUS]+Offset+l)=0x20;
}
}
void Print()//将整个棋盘算出并储存到缓冲器,然后调用Display函数显示出来
{
int i,j;//循环变量
wl=0;
wp=0;
for(j=0;j<=MAXIMUS;j++)//写入出交点左上角的字符,因为需要打印棋盘右下角,所以很以横纵各多一次循环
return "┠";
else if(i==MAXIMUS-1)
return "┨";
else if(j==0)
return "┯";
else if(j==MAXIMUS-1)
return "┷";
#include <string.h>
#define MAXIMUS 15 //定义棋盘大小
int p[MAXIMUS][MAXIMUS];//存储对局信息
char buff[MAXIMUS*2+1][MAXIMUS*4+3];//输出缓冲器
int Cx,Cy;//当前光标位置
printf("\n ***********\n Game over!\n ***********\n ");
system("pause");
exit(0);
}
char* Copy(char* strDest,const char* strSrc)//修改过的字符串复制函数,会忽略末端的\0
}
}
Cx=Cy=MAXIMUS/2;//重置光标到中央
Now=1;//重置当前为黑方
}
char* getStyle(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘
{
if(p[i][j]==1)//1为黑子
{
for(i=0;i<=MAXIMUS;i++)
{
write(getCurse(i,j));//写入左上角字符
if(j==0||j==MAXIMUS)//如果是棋上下盘边缘则没有连接的竖线,用空格填充位置
else
return "┼";//中间的空位
}
char* getCurse(int i,int j)
{//获得指定坐标交点位置左上格的样式,通过制表符来模拟光标的显示
if(i==Cx)
{
if(j==Cy)
return "┏";
}
return " ";//如果不在光标附近则为空
}
void write(char* c)//向缓冲器写入字符串
{
Copy(buff[wl]+wp,c);
wp+=strlen(c);
}
void ln()//缓冲器写入位置提行
{
wl++;
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;//向右上检查
}
int RunGame()//进行整个对局,返回赢家信息(虽然有用上)
{
write("┃");
else if(i!=MAXIMUS)//中间的竖线
write("│");
}
}
if(j==MAXIMUS)//如果是最后一次循环,则只需要处理边侧字符,交点要少一
}
else
{
write("—");//中间的横线
}
}
}
ln();//写完一行后提行
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)
else if (j==Cy+1)
return "┗";