这里分享一个自己写的小游戏,刚刚开始有点复杂,正在改进:
#include<stdio.h>
#include<windows.h>//窗口程序头文件
#include<conio.h>
#include<stdlib.h>//清屏函数头文件
char qipan[16][16];
int x,y;
void Pos(int x,int y);
void initQipan();
void printQipan();
void startGame();
int panduan(int x,int y);
int showwhowin();
void whiteplay();
void blackplay();
void printshuzi();
void printshuzi()//输出数字
{int i;Pos(2,0);//在坐标(2.0)处输出数字 for(i=0;i<16;i++)printf("%2d",i);Pos(1,1);for(i=0;i<16;i++){Pos(0,1+i);printf("%2d",i);//“%2d”数字之间的距离 }}
void Pos(int x,int y)//句柄
{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);
}
void initQipan()
{int i,j;for(i=0;i<16;i++)for(j=0;j<16;j++)qipan[i][j]='*';
}
void printQipan()//打印棋盘
{int i,j;printshuzi(); //打印数字 for(i=0;i<16;i++){ Pos(2,1+i);//打印十六个*符号后换行 for(j=0;j<16;j++)printf("%2c",qipan[i][j]);}
}
int showwhowin()//判断胜利者
{int overleap=0;int leap;//接受谁赢leap=panduan(x,y); //int panduan()函数设置返回值,返回值为1则白方胜利,2则黑方胜利 if(leap==1){overleap=1;system("cls");//清屏 printQipan();printf("n白方胜利n");system("pause");//进行一次停止,回车键继续进行 }if(leap==2){overleap=1;system("cls");printQipan();printf("n黑方胜利n");system("pause");}return overleap;
}
void whiteplay()
{printf("n请白方落字:");scanf("%d%d",&x,&y);while(1){if(qipan[x][y]=='*')//判断是否已经落子 {qipan[x][y]='w';break;}else{printf("n你瞎啊n");printf("n请白方落字:");//system("pause");//break;}scanf("%d%d",&x,&y);//接收输入的落子位置 }printQipan(); showwhowin();}
void blackplay()
{printf("n请黑方落字:");scanf("%d%d",&x,&y);while(1){if(qipan[x][y]=='*'){qipan[x][y]='B';break;}else{printf("n你瞎子啊n");printf("n请黑方落字:");//system("pause");//break;}scanf("%d%d",&x,&y);}qipan[x][y]='B';printQipan(); showwhowin();}
void startGame()
{initQipan();printQipan();while(1){whiteplay();if(showwhowin()==1)// int showwhowin();返回值为1,则代表有人胜利,跳出循环, {break;}system("cls");printQipan();blackplay();if(showwhowin()==1){break;}system("cls");printQipan();}printf("客官,还来不?y or nn");if(getch()=='n');//接收一个字符,判断是否继续进行游戏 exit(0);if(getch()=='y'){system("cls");startGame();}
}
int panduan(int x,int y)
{char temp;//保存下棋的颜色 int j;int count=1;int i=1;int whowin=0;//1white 2blacktemp=qipan[x][y];while(temp==qipan[x][y-i]&&x>0&&x<16&&y>0&&y<15&&count<5)//控制接收到的位置坐标是否超出棋盘大小 , {i++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}i=1;//重置为1 while(temp==qipan[x][y+i]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}//在上面两段函数中,i值 曾重置为1,count不进行重置。因为 上面两段函数代表同一行,水平方向坐标不变,竖直发生变化,i用来统计左右挪移距离, count统计相同的子数 count=1;i=1;while(temp==qipan[x+i][y]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}i=1;while(temp==qipan[x-i][y]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}i=1;j=1;count=1;while(temp==qipan[x-i][y-j]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;j++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}i=1;j=1;while(temp==qipan[x+i][y+j]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;j++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}i=1;j=1;count=1;while(temp==qipan[x+i][y-j]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;j++;count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}i=1;j=1;while(temp==qipan[x-i][y+j]&&x>0&&x<16&&y>0&&y<15&&count<5){i++;j++; count++;if(count==5){if(temp=='w')whowin=1;elsewhowin=2;}}return whowin;
}
int main()
{startGame();return 0;
}
本文发布于:2024-01-29 10:27:11,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170649523614636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |