https://www.myziyuan.com/
- 11212121
- 这个程序还需要两个文件,告诉我你邮箱,我发给你源程序前半部分/*加载头文件*/#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 /*若想在游戏中途退出, 可按 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); printf("3.Don not move the pieces out of the chessboard !"); gotoxy(12,14); printf("DO you want to continue ?(Y/N)"); ch=getchar(); /*判断程序是否要继续进行*/ if(ch=='n'||ch=='N') /*如果不继续进行,则推出程序*/ exit(0);}/*画棋盘函数*/void DrawBoard(){ int x1,x2; int y1,y2; /*设置背景色*/ setbkcolor(2); /*设置线条颜色*/ setcolor(1); /*设置线条风格、宽度*/ setlinestyle(DOTTED_LINE,1,1); /*按照预设的偏移量开始画棋盘*/ for(x1=1,y1=1,y2=18;x1<=18;x1++) line((x1+OFFSET_x)*OFFSET,(y1+OFFSET_y)*OFFSET,(x1+OFFSET_x)*OFFSET,(y2+OFFSET_y)*OFFSET); for(x1=1,y1=1,x2=18;y1<=18;y1++) line((x1+OFFSET_x)*OFFSET,(y1+OFFSET_y)*OFFSET,(x2+OFFSET_x)*OFFSET,(y1+OFFSET_y)*OFFSET); /*将各个点的状态设置为0*/ for(x1=1;x1<=18;x1++) for(y1=1;y1<=18;y1++) status[x1][y1]=0; /*显示帮助信息*/ setcolor(14); /*设置字体、大小*/ settextstyle(1,0,1); outtextxy((19+OFFSET_x)*OFFSET,(2+OFFSET_y)*OFFSET,"Player key:"); setcolor(9); settextstyle(3,0,1); outtextxy((19+OFFSET_x)*OFFSET,(4+OFFSET_y)*OFFSET,"UP--up "); outtextxy((19+OFFSET_x)*OFFSET,(6+OFFSET_y)*OFFSET,"DOWN--down "); outtextxy((19+OFFSET_x)*OFFSET,(8+OFFSET_y)*OFFSET,"LEFT--left"); outtextxy((19+OFFSET_x)*OFFSET,(10+OFFSET_y)*OFFSET,"RIGHT--right"); outtextxy((19+OFFSET_x)*OFFSET,(12+OFFSET_y)*OFFSET,"ENTER--space"); setcolor(14); settextstyle(1,0,1); outtextxy((19+OFFSET_x)*OFFSET,(14+OFFSET_y)*OFFSET,"Exit:"); setcolor(9); settextstyle(3,0,1); outtextxy((19+OFFSET_x)*OFFSET,(16+OFFSET_y)*OFFSET,"ESC");}/*画圆函数*/void DrawCircle(int x,int y,int color){ setcolor(color); /*设置画圆线条的风格,宽度,这里设置为虚线*/ setlinestyle(SOLID_LINE,0,1); x=(x+OFFSET_x)*OFFSET; y=(y+OFFSET_y)*OFFSET; /*以(x,y)为圆心,8为半径画圆*/ circle(x,y,8);} /*交换行棋方函数*/void Alternation(){ if(flag==1) flag=2 ; else flag=1 ;} /*对不同的行棋方画不同颜色的圆函数*/void JudgePlayer(int x,int y){ if(flag==1) DrawCircle(x,y,15); if(flag==2) DrawCircle(x,y,4);}/*判断当前行棋方是否获胜函数*/int ResultCheck(int x,int y){ int j,k; int n1,n2 ; while(1) { /*对水平方向进行判断是否有5个同色的圆*/ n1=0; n2=0; /*水平向左数*/ for(j=x,k=y;j>=1;j--) { if(status[j][k]==flag) n1++; else break; } /*水平向右数*/ for(j=x,k=y;j<=18;j++) { if(status[j][k]==flag) n2++; else break; } if(n1+n2-1>=5) { return(1); } /*对垂直方向进行判断是否有5个同色的圆*/ n1=0; n2=0; /*垂直向上数*/ for(j=x,k=y;k>=1;k--) { if(status[j][k]==flag) n1++; else break ; } /*垂直向下数*/ for(j=x,k=y;k<=18;k++) { if(status[j][k]==flag) n2++; else break ; } if(n1+n2-1>=5) { return(1); } /*从左上方到右下方进行判断是否有5个同色的圆*/ n1=0; n2=0; /*向左上方数*/ for(j=x,k=y;(j>=1)&&(k>=1);j--,k--) { if(status[j][k]==flag) n1++; else break; } /*向右下方数*/ for(j=x,k=y;(j<=18)&&(k<=18);j++,k++) { if(status[j][k]==flag) n2++; else break; } if(n1+n2-1>=5) { return(1); } /*从右上方到左下方进行判断是否有5个同色的圆*/ n1=0; n2=0; /*向右上方数*/ for(j=x,k=y;(j<=18)&&(k>=1);j++,k--) { if(status[j][k]==flag) n1++; else break; } /*向左下方数*/ for(j=x,k=y;(j>=1)&&(k<=18);j--,k++) { if(status[j][k]==flag) n2++; else break; } if(n1+n2-1>=5) { return(1); } return(0); }}
- 2021-03-25 02:35:01
- hz
- #include<iostream> using namespace std; int Hsheng(char a[][15]);//判断o子是否获胜的函数 int Bsheng(char a[][15]);//判断x子是否获胜的函数 int he(char a[][15]);//判断是否平局(也就是棋盘下满了)的函数 void qipan(char a[15][15])//执行输出棋盘命令 { for(int i=0;i<15;i++) //打印棋盘 { for(int j=0;j<15;j++) cout<<a[i][j]; cout<<endl; } } int main() { char a[15][15]; int x,y; for(int i=0;i<15;i++) for(int j=0;j<15;j++) a[i][j]=' '; qipan(a); while(1)//用循环语句执行o,x交替下子,这些while语句看起来似乎是个死循环~实际上都会经过break结束 { int a1=1; while(1) { for(;a1;) { cout<<"请输入o子下的位置:"; //输入o子的位置 cin>>x>>y; if(a[x][y]=='o'||a[x][y]=='x')//判断是否已有子 {cout<<"已有子请重下"<<",";continue;} else if(x>=15||y>=15){cout<<"输入错误请重输"<<",";continue;} else { a[x][y]='o'; a1=0;} } break;} qipan(a);//下好o子后将棋盘显示 if(Hsheng(a))//判断o子是否已经获胜 { cout<<"o子获胜"<<endl; break; } while(1)//下x子 { cout<<"请输入x子下的位置:"; cin>>x>>y; if(a[x][y]=='o'||a[x][y]=='x'||x>=15||y>=15) { for( ; a[x][y]=='o'||a[x][y]=='x'; ) { cout<<"已有子请重下"; cout<<"请输入x子下的位置:"; cin>>x>>y;continue; } for ( ; x>=15||y>=15||x; ) { cout<<"输入错误请重输"<<","; //判断输入棋子位置是否正确 cout<<"请输入x子下的位置:"; cin>>x>>y;continue ;} a[x][y]='x';break; } else { a[x][y]='x'; break; } } qipan(a);//再一次输出棋盘 if(Bsheng(a))//判断x子是否已经获胜 { cout<<"x子获胜"<<endl; break; } if(he(a))//判断是否平局 { cout<<"平局"<<endl; break; } } return 0; } int Hsheng(char a[][15]) { int i,j;//判断横着的5个是否都相等 for(i=0;i<15;i++) for(j=0;j<15;j++) if(a[i][j]=='o'&&a[i][j+1]=='o'&&a[i][j+2]=='o'&&a[i][j+3]=='o'&&a[i][j+4]=='o') return 1; for(j=0;j<15;j++)//判断竖着的5个是否都相等 for(i=0;i<15;i++) if(a[i][j]=='o'&&a[i+1][j]=='o'&&a[i+2][j]=='o'&&a[i+3][j]=='o'&&a[i+4][j]=='o') return 1; for(i=0;i<15;i++)//判断左斜5个 for(j=0;j<15;j++) if(a[i][j]=='o'&&a[i+1][j+1]=='o'&&a[i+2][j+2]=='o'&&a[i+3][j+3]=='o'&&a[i+4][j+4]=='o') return 1; for(i=0;i<15;i++)//右斜5个 for(j=14;j>3;j--) if(a[i][j]=='H'&&a[i+1][j-1]=='o'&&a[i+2][j-2]=='o'&&a[i+3][j-3]=='o'&&a[i+4][j-4]=='o') return 1; return 0; } int Bsheng(char a[][15])//同o,只是改字符 { int i,j; for(i=0;i<15;i++) for(j=0;j<15;j++) if(a[i][j]=='x'&&a[i][j+1]=='x'&&a[i][j+2]=='x'&&a[i][j+3]=='x'&&a[i][j+4]=='x') return 1; for(j=0;j<15;j++) for(i=0;i<15;i++) if(a[i][j]=='x'&&a[i+1][j]=='x'&&a[i+2][j]=='x'&&a[i+3][j]=='x'&&a[i+4][j]=='x') return 1; for(i=0;i<15;i++) for(j=0;j<15;j++) if(a[i][j]=='x'&&a[i+1][j+1]=='x'&&a[i+2][j+2]=='x'&&a[i+3][j+3]=='x'&&a[i+4][j+4]=='x') return 1; for(i=0;i<15;i++) for(j=14;j>3;j--) if(a[i][j]=='x'&&a[i+1][j-1]=='x'&&a[i+2][j-2]=='x'&&a[i+3][j-3]=='x'&&a[i+4][j-4]=='x') return 1; return 0; } int he(char a[][15]) { for(int i=0;i<15;i++) for(int j=0;j<15;j++) { if(a[i][j]==' ')//当棋盘全部子都不是' '时才能return 1,即棋盘已下满 return 0; } return 1; }
- 2021-02-12 13:06:38
- dsadasd
- 跪求 c++编写的小游戏源代码 像扫雷,五子棋...... 提供相关网站也可,#include <stdio.h>#include <conio.h>#include <windows.h>#include <time.h>#define Height 25 //迷宫的高度,必须为奇数#define Width 25 //迷宫的宽度,必须为奇数#define Wall 1#define Road 0#define Start 2#define End 3#define Esc 5#define Up 1#define Down 2#define Left 3#define Right 4int map[Height+2][Width+2];void gotoxy(int x,int y) //移动坐标{COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );}void hidden()//隐藏光标{HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci);cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}void create(int x,int y) //随机生成迷{int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向int i,j,t;//将方向打乱for(i=0;i<4;i++){j=rand()%4;t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;}map[x][y]=Road;for(i=0;i<4;i++)if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall){map[x+c[i][0]][y+c[i][1]]=Road;create(x+2*c[i][0],y+2*c[i][1]);}}int get_key() //接收按键{char c;while(c=getch()){if(c==27) return Esc; //Escif(c!=-32)continue;c=getch();if(c==72) return Up; //上if(c==80) return Down; //下if(c==75) return Left; //左if(c==77) return Right; //右}return 0;}void paint(int x,int y) //画迷宫{gotoxy(2*y-2,x-1);switch(map[x][y]){case Start:printf("入");break; //画入口case End:printf("出");break; //画出口case Wall:printf("▇");break; //画墙case Road:printf(" ");break; //画路}}void game(){int x=2,y=1; //玩家当前位置,刚开始在入口处int c; //用来接收按键while(1){gotoxy(2*y-2,x-1);printf("●"); //画出玩家当前位置if(map[x][y]==End) //判断是否到达出口{gotoxy(30,24);printf("到达终点,按任意键结束");getch();break;}c=get_key();if(c==Esc){gotoxy(0,24);break;}switch(c){case Up: //向上走if(map[x-1][y]!=Wall){paint(x,y);x--;}break;case Down: //向下走if(map[x+1][y]!=Wall){paint(x,y);x++;}break;case Left: //向左走if(map[x][y-1]!=Wall){paint(x,y);y--;}break;case Right: //向右走if(map[x][y+1]!=Wall){paint(x,y);y++;}break;}}}int main(){int i,j;srand((unsigned)time(NULL)); //初始化随即种子hidden(); //隐藏光标for(i=0;i<=Height+1;i++)for(j=0;j<=Width+1;j++)if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫map[i][j]=Road;else map[i][j]=Wall;create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数for(i=0;i<=Height+1;i++) //边界处理 {map[i][0]=Wall;map[i][Width+1]=Wall;}for(j=0;j<=Width+1;j++) //边界处理{map[0][j]=Wall;map[Height+1][j]=Wall;}map[2][1]=Start; //给定入口map[Height-1][Width]=End; //给定出口for(i=1;i<=Height;i++)for(j=1;j<=Width;j++) //画出迷宫paint(i,j);game(); //开始游戏getch();return 0;}
- 2021-02-12 13:06:38