|
|
|
|
|
|
|
|
//五子棋小游戏
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include<windows.h>
|
|
|
#include <stdlib.h>
|
|
|
#include<conio.h> //使用getch()函数
|
|
|
|
|
|
int startchoice,winner,player,Q[200][200]= {0}; //Q数组记录旗子
|
|
|
char button; //读入键盘输入的指令,如→
|
|
|
|
|
|
struct Point //点坐标的结构体
|
|
|
{
|
|
|
int x,y;
|
|
|
} point,game_xy;
|
|
|
|
|
|
struct Piece //棋子的坐标记录
|
|
|
{
|
|
|
struct Point coord;
|
|
|
struct Piece *fore;
|
|
|
};
|
|
|
|
|
|
struct Piece *head,*p,*ptr,*ptr1;
|
|
|
|
|
|
void goto_xy(int x, int y) //光标移动函数
|
|
|
{
|
|
|
COORD c;
|
|
|
c.X=2*x;
|
|
|
c.Y=y;
|
|
|
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
|
|
|
}
|
|
|
|
|
|
void Init() //初始化函数,将记录棋子的数组初始化
|
|
|
{
|
|
|
for(int i=0; i<200; i++)
|
|
|
for(int j=0; j<200; j++)
|
|
|
Q[i][j]=0;
|
|
|
|
|
|
startchoice=0;
|
|
|
player=1;
|
|
|
p=(struct Piece *)malloc(sizeof(struct Piece));
|
|
|
head=p;
|
|
|
}
|
|
|
|
|
|
void Welcome() //欢迎界面
|
|
|
{
|
|
|
system("color B0"); //#include<windows.h> B 背景淡浅绿色 0 字体黑色
|
|
|
goto_xy(10,3);
|
|
|
printf("-→");
|
|
|
goto_xy(17,1);
|
|
|
printf("主菜单");
|
|
|
goto_xy(13,3);
|
|
|
printf(" ***人人对战***");
|
|
|
goto_xy(13,4);
|
|
|
printf(" ***退出***");
|
|
|
point.x=12;
|
|
|
point.y=3;
|
|
|
goto_xy(0,0);
|
|
|
}
|
|
|
|
|
|
void ShowWho() //显示轮到哪一方下棋
|
|
|
{
|
|
|
goto_xy(17,24);
|
|
|
if(player==0)
|
|
|
printf("轮到甲方落子");
|
|
|
else
|
|
|
printf("轮到乙方落子");
|
|
|
goto_xy(point.x,point.y);
|
|
|
}
|
|
|
|
|
|
void Draw() //绘画游戏界面
|
|
|
{
|
|
|
game_xy.x=10;
|
|
|
game_xy.y=3;
|
|
|
system("cls");
|
|
|
system("color 3F"); //3 湖蓝色 F 亮白色
|
|
|
goto_xy(15,1);
|
|
|
printf("欢迎play our 五子棋!");
|
|
|
/*goto_xy(29,22);
|
|
|
printf("重新开始 r");*/
|
|
|
goto_xy(1,24);
|
|
|
printf("悔棋 b");
|
|
|
goto_xy(1,25);
|
|
|
printf("退出 ESC");
|
|
|
|
|
|
const int i=8; //const 定义的数据不可以被改变 而且修改数据比较方便
|
|
|
const int j=19;
|
|
|
const int k=3;
|
|
|
goto_xy(game_xy.x-i,game_xy.y+k); //输出甲方的下棋方法
|
|
|
printf("甲方: ●");
|
|
|
goto_xy(game_xy.x-i,game_xy.y+k+2);
|
|
|
printf("移动: 上 W");
|
|
|
goto_xy(game_xy.x-i,game_xy.y+k+4);
|
|
|
printf(" 下 S");
|
|
|
goto_xy(game_xy.x-i,game_xy.y+k+6);
|
|
|
printf(" 左 A");
|
|
|
goto_xy(game_xy.x-i,game_xy.y+k+8);
|
|
|
printf(" 右 D ");
|
|
|
goto_xy(game_xy.x-i,game_xy.y+k+10);
|
|
|
printf("落子: 空 格");
|
|
|
|
|
|
goto_xy(game_xy.x+j,game_xy.y+k); //输出乙方的下棋方法
|
|
|
printf("乙方: ○");
|
|
|
goto_xy(game_xy.x+j,game_xy.y+k+2);
|
|
|
printf("移动: 上 ↑");
|
|
|
goto_xy(game_xy.x+j,game_xy.y+k+4);
|
|
|
printf(" 下 ↓");
|
|
|
goto_xy(game_xy.x+j,game_xy.y+k+6);
|
|
|
printf(" 左 ←");
|
|
|
goto_xy(game_xy.x+j,game_xy.y+k+8);
|
|
|
printf(" 右 →");
|
|
|
goto_xy(game_xy.x+j,game_xy.y+k+10);
|
|
|
printf("落子: Enter");
|
|
|
|
|
|
for(int k1=0; k1<200; k1++) //初始化棋子记录,在第二局时有明确的作用
|
|
|
for(int k2=0; k2<200; k2++)
|
|
|
Q[k1][k2]=0;
|
|
|
|
|
|
for(int i=0; i<20; i++) //画棋盘
|
|
|
{
|
|
|
|
|
|
if(i==0) //画第一行
|
|
|
{
|
|
|
goto_xy(10,i+3);
|
|
|
printf("┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐");
|
|
|
|
|
|
}
|
|
|
if(i!=0&&i!=19) //画出中间16行
|
|
|
{
|
|
|
goto_xy(10,i+3);
|
|
|
printf("├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤");
|
|
|
}
|
|
|
if(i==19) //画最后一行
|
|
|
{
|
|
|
goto_xy(10,i+3);
|
|
|
printf("└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘");
|
|
|
}
|
|
|
}
|
|
|
point.x=19;
|
|
|
point.y=12;
|
|
|
goto_xy(19,12);
|
|
|
}
|
|
|
|
|
|
void Clean() //清除-→运动的轨迹
|
|
|
{
|
|
|
goto_xy(10,3);
|
|
|
printf(" ");
|
|
|
goto_xy(24,3);
|
|
|
printf(" ");
|
|
|
goto_xy(10,4);
|
|
|
printf(" ");
|
|
|
goto_xy(24,4);
|
|
|
printf(" ");
|
|
|
}
|
|
|
|
|
|
void menu_choose(char press) //选择游戏还是退出
|
|
|
{
|
|
|
if(press==72) //↑的ASCLL码
|
|
|
{
|
|
|
if(point.y==3)
|
|
|
point.y=4;
|
|
|
else
|
|
|
point.y=3;
|
|
|
Clean();
|
|
|
goto_xy(10,point.y);
|
|
|
printf("-→");
|
|
|
}
|
|
|
if(press==80) //↓的ASCLL码
|
|
|
{
|
|
|
if(point.y==4)
|
|
|
point.y=3;
|
|
|
else
|
|
|
point.y=4;
|
|
|
Clean();
|
|
|
goto_xy(10,point.y);
|
|
|
printf("-→");
|
|
|
}
|
|
|
if(press==13) // 13:回车键的ASCLL码
|
|
|
{
|
|
|
startchoice=point.y-2; //startchoice 为1或2
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void go_back(int x1,int y1) //悔棋函数
|
|
|
{
|
|
|
goto_xy(x1,y1);
|
|
|
if(x1==10)
|
|
|
{
|
|
|
if(y1==3)
|
|
|
{
|
|
|
printf("┌");
|
|
|
}
|
|
|
else if(y1==20)
|
|
|
{
|
|
|
printf("└");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("├");
|
|
|
}
|
|
|
}
|
|
|
else if(x1==27)
|
|
|
{
|
|
|
if(y1==3)
|
|
|
{
|
|
|
printf("┐");
|
|
|
}
|
|
|
else if(y1==20)
|
|
|
{
|
|
|
printf("┘");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("┤");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if(y1==3)
|
|
|
{
|
|
|
printf("┬");
|
|
|
}
|
|
|
else if(y1==20)
|
|
|
{
|
|
|
printf("┴");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("┼");
|
|
|
}
|
|
|
}
|
|
|
Q[point.x][point.y]=0; //在数组中将弹出的棋子对应的数据设为0
|
|
|
goto_xy(x1,y1);
|
|
|
}
|