You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fc001/五子棋课程设计最终版.cpp

383 lines
7.3 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define SIZE 19
int jushu=0;
int q1=0,q2=0;
int k[SIZE][SIZE];
int WANJIA;
int count;
int X,Y;
char* Text;
int welcome(void)
{
printf("\n\n\n\n\n\n");
printf("\t\t\t~*************欢迎进入五子棋游戏*************~\n");
printf("\n\n\n");
printf("\t\t\t~*************制作者:冯程*************~\n");
printf("\n\n\n");
printf("\t\t\t~*************按回车键进入帮助*************~");
while(getchar() == 0);
system("CLS");
return 0;
}
int help(void)
{
printf("\n\n\n\n\n\n");
printf("\t\t\t~************************帮助**************************~\n");
printf("\n");
printf("\t\t\t~*************p1玩家wasd控制光标Tab键下棋************~\n");
printf("\n\n\n");
printf("\t\t\t~************p2玩家方向键控制光标空格键下棋**********~\n");
printf("\n\n\n");
printf("\t\t\t~********************按回车键开始游戏******************~");
while(getchar() == 0);
system("CLS");
return 0;
}
int blackwin(void)
{
printf("\n\n\n\n\n\n");
printf("\t\t\t~*************恭喜黑方胜利************~\n");
printf("\n\n\n");
printf("\t\t\t~************当前总局数:%d*************~\n",jushu);
printf("\n\n\n");
printf("\t\t\t~******黑方胜利:%d局白方胜利%d局******~",q1,q2);
printf("\n\n\n");
printf("\t\t\t~**********按任意键开启下一局***********~\n");
}
int whitewin(void)
{
printf("\n\n\n\n\n\n");
printf("\t\t\t~*************恭喜白方胜利************~\n");
printf("\n\n\n");
printf("\t\t\t~************当前总局数:%d*************~\n",jushu);
printf("\n\n\n");
printf("\t\t\t~******黑方胜利:%d局白方胜利%d局******~",q1,q2);
printf("\n\n\n");
printf("\t\t\t~**********按任意键开启下一局***********~\n");
}
int pingju(void)
{
printf("\n\n\n\n\n\n");
printf("\t\t\t~***************恭喜平局************~\n");
printf("\n\n\n");
printf("\t\t\t~************当前总局数:%d*************~\n",jushu);
printf("\n\n\n");
printf("\t\t\t~******黑方胜利:%d局白方胜利%d局******~",q1,q2);
printf("\n\n\n");
printf("\t\t\t~**********按任意键开启下一局***********~\n");
}
void Chushihua()//棋盘初始化
{
int i,j;
X=Y=SIZE/2;
Text="";
count=0;
WANJIA=1;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
k[i][j]=0;
}
}
}
char* getqipan(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘
{
if(k[i][j]==1)//1为黑子
printf ("") ;
else if(k[i][j]==2)//2为白子
printf ("");
else if(i==0&&j==0)//以下为边缘棋盘样式
printf ("");
else if(i==SIZE-1&&j==0)
printf ("");
else if(i==SIZE-1&&j==SIZE-1)
printf ("");
else if(i==0&&j==SIZE-1)
printf ("");
else if(i==0)
printf ("");
else if(i==SIZE-1)
printf ("");
else if(j==0)
printf ("");
else if(j==SIZE-1)
printf ("");
else
printf ("");//中间的空位
}
char* getguangbiao(int i,int j){//获得指定坐标交点位置左上格的样式,通过制表符来模拟光标的显示
if(i==X){
if(j==Y)
printf("");
else if (j==Y+1)
printf ("");
}
else if(i==X+1)
{
if(j==Y)
printf ("");
else if (j==Y+1)
printf ("");
}
else;
printf (" ");//如果不在光标附近则为空
}
void printqipan()//绘制棋盘
{
int i,j;
for(j=0;j<=SIZE;j++)
{
for(i=0;i<=SIZE;i++)
{
getguangbiao(i,j);//写入左上角字符
if(j==0||j==SIZE)//如果是棋上下盘边缘则用空格填充位置
{
if(i!=SIZE)
printf(" ");
}
else//如果在棋盘中间则用竖线承接上下
{
if(i==0||i==SIZE-1)//左右边缘的竖线更粗
printf("");
else if(i!=SIZE)//中间的竖线
printf("");
}
}
if(j==SIZE)//如果是最后一次循环,则只需要处理边侧字符,交点要少一排
{
break;
}
printf("\n");
printf(" ");
for(i=0;i<SIZE;i++)
{
getqipan(i,j);//写入交点字符
if(i!=SIZE-1)//如果不在最右侧则补充一个横线承接左右
{
if(j==0||j==SIZE-1)
{
printf("");//上下边缘的横线更粗
}
else
{
printf("");//中间的横线
}
}
}
printf("\n");
}
}
int Put(){//在当前光标位置走子如果非空则返回0表示失败
if(k[X][Y]==0)
{
k[X][Y]=WANJIA;//改变该位置数据
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(Y+i<SIZE&&k[X][Y+i]==WANJIA)w++;else break;//向下检查
for(i=1;i<5;i++)if(Y-i>0&&k[X][Y-i]==WANJIA)w++;else break;//向上检查
for(i=1;i<5;i++)if(X+i<SIZE&&k[X+i][Y]==WANJIA)x++;else break;//向右检查
for(i=1;i<5;i++)if(X-i>0&&k[X-i][Y]==WANJIA)x++;else break;//向左检查
for(i=1;i<5;i++)if(X+i<SIZE&&Y+i<SIZE&&k[X+i][Y+i]==WANJIA)y++;else break;//向右下检查
for(i=1;i<5;i++)if(X-i>0&&Y-i>0&&k[X-i][Y-i]==WANJIA)y++;else break;//向左上检查
for(i=1;i<5;i++)if(X+i<SIZE&&Y-i>0&&k[X+i][Y-i]==WANJIA)z++;else break;//向右上检查
for(i=1;i<5;i++)if(X-i>0&&Y+i<SIZE&&k[X-i][Y+i]==WANJIA)z++;else break;//向左下检查
if(z>=5)
{return WANJIA;}
else if(w>=5)
{return WANJIA;}
if(x>=5)
{return WANJIA;}
else if(y>=5)
{return WANJIA;}
else
return 0;
}
int run()//跑游戏
{
jushu+=1;
int input;
int whichwin;
Chushihua();
while(1)
{
printqipan();
input=getch();
if(input==27)
{
exit(0);
}
else if(input==32 ||input==0x0009)
{
if(Put())
{
whichwin=Check();
count++;
WANJIA=3-WANJIA;
if(whichwin==1)
{
q1+=1;
system("cls");
blackwin();
if (getch()==0xE0)
{
getch();
}
return WANJIA;
}
else if(whichwin==2)
{
q2+=1;
system("cls");
whitewin();
if (getch()==0xE0)
{
getch();
}
return WANJIA;
}
else if (count==SIZE*SIZE)
{
system("cls");
pingju();
if (getch()==0xE0)
{
getch();
}
return 0;
}
}
}
else if(input==0xE0)
{
input=getch();
switch(input)
{
case 0x4B:
X--;
break;
case 0x48:
Y--;
break;
case 0x4D:
X++;
break;
case 0x50:
Y++;
break;
}
if(X<0)X=SIZE-1;//如果光标位置越界则移动到对侧
if(Y<0)Y=SIZE-1;
if(X>SIZE-1)X=0;
if(Y>SIZE-1)Y=0;
}
else if(input=='a')
{
X--;
if(X<0)X=SIZE-1;//如果光标位置越界则移动到对侧
if(Y<0)Y=SIZE-1;
if(X>SIZE-1)X=0;
if(Y>SIZE-1)Y=0;
}
else if(input=='d')
{
X++;
if(X<0)X=SIZE-1;//如果光标位置越界则移动到对侧
if(Y<0)Y=SIZE-1;
if(X>SIZE-1)X=0;
if(Y>SIZE-1)Y=0;
}
else if(input=='s')
{
Y++;
if(X<0)X=SIZE-1;//如果光标位置越界则移动到对侧
if(Y<0)Y=SIZE-1;
if(X>SIZE-1)X=0;
if(Y>SIZE-1)Y=0;
}
else if(input=='w')
{
Y--;
if(X<0)X=SIZE-1;//如果光标位置越界则移动到对侧
if(Y<0)Y=SIZE-1;
if(X>SIZE-1)X=0;
if(Y>SIZE-1)Y=0;
}
}
}
int main()//主函数
{
system("title 五子棋课程设计-冯程");//设置标题
system("mode con cols=100 lines=64");//设置窗口大小
system("color E5");//设置颜色
welcome();
help();
while(1){//循环执行游戏
run();
}
}