https://www.myziyuan.com/
- dfgj
- 首先申明,这不是俺写的,只是帮你找的,简单的人人对战,没有AI功能,其实百度一下,很多问题能自己解决的!#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <string.h>#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;//回合数char* Copy ( char* strDest, const char* strSrc ) //修改过的字符串复制函数,会忽略末端的\0{char* strDestCopy = strDest;while ( *strSrc != '\0' ){*strDest++ = *strSrc++;}return strDestCopy;}void Initialize() //初始化一个对局函数{int i, j; //循环变量showText = ""; //重置显示信息count = 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 ( 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 ( p[Cx][Cy] == 0 ){p[Cx][Cy] = Now; //改变该位置数据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表示还没有玩家达成胜利}int RunGame() //进行整个对局,返回赢家信息(虽然有用上){int input;//输入变量int victor;//赢家信息Initialize();//初始化对局while ( 1 ) //开始无限回合的死循环,直到出现胜利跳出{Print();//打印棋盘input = getch(); //等待键盘按下一个字符if ( input == 27 ) //如果是ESC则退出程序{exit ( 0 );}else if ( input == 0x20 ) //如果是空格则开始走子{if ( Put() ) //如果走子成功则判断胜负{victor = Check();Now = 3 - Now; //轮换当前走子玩家count++;if ( victor == 1 ) //如果黑方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText = "黑方获得了胜利!";Print();if ( getch() == 0xE0 ){getch();}return Now;}else if ( victor == 2 ) //如果白方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText = "白方获得了胜利!";Display();if ( getch() == 0xE0 ){getch();}return Now;}else if ( count == MAXIMUS * MAXIMUS ) //如果回合数达到了棋盘总量,即棋盘充满,即为平局{showText = "平局!";Display();if ( getch() == 0xE0 ){getch();}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++;break;}if ( Cx < 0 ) Cx = MAXIMUS - 1; //如果光标位置越界则移动到对侧if ( Cy < 0 ) Cy = MAXIMUS - 1;if ( Cx > MAXIMUS - 1 ) Cx = 0;if ( Cy > MAXIMUS - 1 ) Cy = 0;}}}int main() //主函数{system ( "title 简易五子棋 ——Etsnarl制作" ); //设置标题system ( "mode con cols=63 lines=32" ); //设置窗口大小system ( "color E0" ); //设置颜色while ( 1 ) //循环执行游戏{RunGame();}}
- 2021-02-20 11:10:01
- 893811145
- 我自己找的:#include<ctype.h> #include<stdio.h> #include<dos.h> #include<conio.h> #include<ctype.h> #include<bios.h> #define SHURU 1 #define FANGXIANG 2 #define WUXIAO 0 #define TUICHU 3 /****************************************************/ static int ii=0,jj=0,wanjia=1; /****************************************************/ void hqp(int a[][20],int y); int anjian(char an); int panduan(int a[][20]); void pingmu(void); void guangbiaoyd(char an); void jh(int a[][20]); /****************************************************/ void main() { int a[20][20]={0},tuichu=0,ying; char an; hqp(a,0); pingmu(); while(1) { ying=panduan(a); if(ying!=0) hqp(a,ying); an=getch(); switch(anjian(an)) { case TUICHU:clrscr(); tuichu=1; break; case FANGXIANG:guangbiaoyd(an); break; case SHURU: switch(ying) { case 1:hqp(a,1);tuichu=1;break; case 2:hqp(a,2);tuichu=1;break; case 0:jh(a);break; } break; case WUXIAO:break; } hqp(a,0); pingmu(); if(tuichu==1) break; } } /****************************************************/ void hqp(int a[][20],int y) { int i,j; clrscr(); if(y!=0) { textcolor(RED); printf("WAN JIA %d SHENG LI! ! ! ! ! \n",y); } for(i=0;i<20;i++) { for(j=0;j<20;j++) { switch(a[i][j]) { case 1: textcolor(YELLOW); putch('X'); break; case 2: textcolor(BLUE); putch('0'); break; case 0: textcolor(GREEN); if(j==0) { if(i==0) { putch(0xda); break; } if(i==19) { putch(0xc0); break; } putch(0xc3); break; } if(j==19) { if(i==0) { putch(0xbf); break; } if(i==19) { putch(0xd9); break; } putch(0xb4); break; } if(i==0&&j!=0&&j!=19) { putch(0xc2); break; } if(i==19&&j!=0&&j!=19) { putch(0xc1); break; } putch(0xc5); break; } } printf("\n"); } } /*****************************************************************/ int anjian(char an) { if(an==32||an==13) return(SHURU); else if(an=='a'||an=='w'||an=='s'||an=='d'|| an=='j'||an=='i'||an=='k'||an=='l') return(FANGXIANG); else if(an==27) return(TUICHU); else return(WUXIAO); } /******************************************************************/ int panduan(int a[][20]) { int i,j; for(i=0;i<20;i++) { for(j=0;j<15;j++) { if(a[i][j]==1&&a[i][j+1]==1&&a[i][j+2]==1&&a[i][j+3]==1&&a[i][j+4]==1) return(1); else if(a[i][j]==2&&a[i][j+1]==2&&a[i][j+2]==2&&a[i][j+3]==2&&a[i][j+4]==2) return(2); } } for(i=0;i<15;i++) { for(j=0;j<20;j++) { if(a[i][j]==1&&a[i+1][j]==1&&a[i+2][j]==1&&a[i+3][j]==1&&a[i+4][j]==1) return(1); else if(a[i][j]==2&&a[i+1][j]==2&&a[i+2][j]==2&&a[i+3][j]==2&&a[i+4][j]==2) return(2); } } for(i=0;i<15;i++) { for(j=0;j<15;j++) { if(a[i][j]==1&&a[i+1][j+1]==1&&a[i+2][j+2]==1&&a[i+3][j+3]==1&&a[i+4][j+4]==1) return(1); else if(a[i][j]==2&&a[i+1][j+1]==2&&a[i+2][j+2]==2&&a[i+3][j+3]==2&&a[i+4][j+4]==2) return(2); } } for(i=4;i<20;i++) { for(j=4;j<20;j++) { if(a[i][j]==1&&a[i-1][j-1]==1&&a[i-2][j-2]==1&&a[i-3][j-3]==1&&a[i-4][j-4]==1) return(1); else if(a[i][j]==2&&a[i-1][j-1]==2&&a[i-2][j-2]==2&&a[i-3][j-3]==2&&a[i-4][j-4]==2) return(2); } } return(0); } /**************************************************************************/ void pingmu(void) { int i,j; char *Msg[]= { "Wan Jia1 an jian:", " shang: w", " xia: s", " zuo: a", " you: d", " fang zi: space", "", "Wan Jia2 an jian:", " shang: i", " xia: k", " zuo: j", " you: l", " fang zi: ENTER", "tui chu:", " ESC\n", NULL, }; i=0; textcolor(RED); while(Msg[i]!=NULL) { gotoxy(25,3+i); cputs(Msg[i]); i++; } gotoxy(25,20); printf("\nqing wan jia %d chu ru:",wanjia); gotoxy(jj+1,ii+1); } /**********************************************************************/ void guangbiaoyd(char an) { if(wanjia==1) { switch(an) { case 'a': jj--;break; case 'd': jj++;break; case 'w': ii--;break; case 's': ii++;break; } } else { switch(an) { case 'j': jj--;break; case 'l': jj++;break; case 'i': ii--;break; case 'k': ii++;break; } } if(ii<0) ii++; if(ii>19) ii--; if(jj<0) jj++; if(jj>19) jj--; gotoxy(ii+1,jj+1); } /***********************************************************************/ void jh(int a[][20]) { int k=0; if(wanjia==1&&a[ii][jj]==0&&k==0) { a[ii][jj]=1; wanjia=2; k=1; } else { if(a[ii][jj]==0&&k==0) { a[ii][jj]=2; wanjia=1; } } } /***********************************************************************/
- 2021-02-11 18:23:38
- 11212121
- 求一个用C语言编写五子棋游戏的全部代码.,这个程序还需要两个文件,告诉我你邮箱,我发给你源程序前半部分/*加载头文件*/#include#include#include#include#include/*编译预处理,定义按键码*/#define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 /*若想在游戏中途退出, 可按 Esc 键*/#define ESC 0x011b /*SPACE键表示落子*/#define SPACE 0x3920/*设置偏移量*/#define OFFSET 20#define OFFSET_x 4#define OFFSET_y 3/*定义数组大小*/#define N 19/*定义全局变量*/int status[N][N]; /*定义的数组,保存状态*/int step_x,step_y;/*行走的坐标*/int key ; /*获取按下的键盘的键*/int flag; /*玩家标志*//*自定义函数原型*/void DrawBoard();void DrawCircle(int x,int y,int color);void Alternation();void JudgePlayer(int x,int y);void Done();int ResultCheck(int x,int y);void WelcomeInfo();void ShowMessage();/*定义函数*//*显示欢迎信息函数*/void WelcomeInfo(){char ch ;/*移动光标到指定位置*/gotoxy(12,4);/*显示欢迎信息*/printf("Welcome you to gobang word!");gotoxy(12,6);printf("1.You can use the up,down,left and right key to move the chessman,");gotoxy(12,8);printf(" and you can press Space key to enter after you move it !");gotoxy(12,10);printf("2.You can use Esc key to exit the game too !");gotoxy(12,12);
- 2021-02-11 18:23:38