如何编写一个五子棋C或C++程序,跟同学对站用的,没有图形界面的

2024-11-15 12:16:49
推荐回答(3个)
回答(1):

/* 头文件声明 */
#include "conio.h"
#include "graphics.h"
#include "dos.h"

/* 宏定义,键盘扫描码 */
#define closegr closegraph
#define w 0x157
#define s 0xf53
#define a 0xe41
#define d 0x2044
#define q 0x011b
#define j 0x244a

/* 全局变量 */
int Chess[8][8];
int Current_x=50,Current_y=50; /* 当前棋子坐标 */
int Player=1; /* 切换下棋手标志 1为红方,2为黄方 */
int Result=0; /* 判断输赢标志 */
int Chess_minx=50,Chess_miny=50,Chess_maxx=400,Chess_maxy=400; /* 定义棋盘的最值 */
int CountPlayer[2]={0,0}; /* Player连成一线棋子的个数,CountPlayer[0]表示1,CountPlayer[1]表示2 */
int Flag=0; /* 判断是否按下退出键 */
int M,N,X,Y; /* 在Judgement()函数中用到的变量,临时变量 */

/* 绘制棋盘并输出提示信息 */
void Draw_chess_background()
{
int i;
char *p="w: up";
cleardevice();
outtextxy(500,150,p);
p="s: down";
outtextxy(500,170,p);
p="a: left";
outtextxy(500,190,p);
p="d: right";
outtextxy(500,210,p);
p="Exit: q";
outtextxy(500,250,p);
p="Five Chess";
outtextxy(200,20,p);
p="j: put_chess";
outtextxy(500,230,p);
rectangle(Chess_minx,Chess_miny,Chess_maxx,Chess_maxy);
for(i=1;i<8;i++)
{
line(Chess_minx+i*50,50,Chess_minx+i*50,400);
line(50,Chess_miny+i*50,400,Chess_miny+i*50);
}
}

/* 绘制当前坐标方框 */
void Draw_chess_box()
{
setcolor(GREEN);
line(Current_x-25,Current_y-25,Current_x-20,Current_y-25);
line(Current_x+20,Current_y-25,Current_x+25,Current_y-25);
line(Current_x-25,Current_y+25,Current_x-20,Current_y+25);
line(Current_x+20,Current_y+25,Current_x+25,Current_y+25);
line(Current_x-25,Current_y-25,Current_x-25,Current_y-20);
line(Current_x-25,Current_y+20,Current_x-25,Current_y+25);
line(Current_x+25,Current_y-25,Current_x+25,Current_y-20);
line(Current_x+25,Current_y+20,Current_x+25,Current_y+25);
}

/* 清除移动前的坐标方框 */
void Clear_chess_box()
{
setcolor(BLACK);
line(Current_x-25,Current_y-25,Current_x-20,Current_y-25);
line(Current_x+20,Current_y-25,Current_x+25,Current_y-25);
line(Current_x-25,Current_y+25,Current_x-20,Current_y+25);
line(Current_x+20,Current_y+25,Current_x+25,Current_y+25);
line(Current_x-25,Current_y-25,Current_x-25,Current_y-20);
line(Current_x-25,Current_y+20,Current_x-25,Current_y+25);
line(Current_x+25,Current_y-25,Current_x+25,Current_y-20);
line(Current_x+25,Current_y+20,Current_x+25,Current_y+25);
}

/* 绘制棋子 */
void Draw_Player_circle(int Player)
{
if(Player%2==1)
{
setcolor(RED);
setfillstyle(1,RED);
}
else if(Player%2==0)
{
setcolor(YELLOW);
setfillstyle(1,YELLOW);
}
pieslice(Current_x,Current_y,0,360,10);
}

/* 根据下棋手更新棋盘数组 */
void Update_chess()
{
if(Player==1)
Chess[Current_x/50-1][Current_y/50-1]=1;
if(Player==2)
Chess[Current_x/50-1][Current_y/50-1]=2;
}

/* 判断输赢 */
int Judgement()
{
for(M=Current_x/50-1,X=1;M-X>=0&&X<5;X++) /* 判断垂直方向 */
if(Chess[M-X][Current_y/50-1]==Player)
CountPlayer[Player-1]++;
else break;
for(M=Current_x/50-1,X=1;M+X<8&&X<5;X++)
if(Chess[M+X][Current_y/50-1]==Player)
CountPlayer[Player-1]++;
else break;
if(CountPlayer[Player-1]+1>=5)
goto end;
else
CountPlayer[Player-1]=0;

for(N=Current_y/50-1,X=1;N-X>=0&&X<5;X++) /* 判断水平方向 */
if(Chess[Current_x/50-1][N-X]==Player)
CountPlayer[Player-1]++;
else break;
for(N=Current_y/50-1,X=1;N+X<8&&X<5;X++)
if(Chess[Current_x/50-1][N+X]==Player)
CountPlayer[Player-1]++;
else break;
if(CountPlayer[Player-1]+1>=5)
goto end;
else
CountPlayer[Player-1]=0;

for(M=Current_x/50-1,N=Current_y/50-1,X=1,Y=1;M-X>=0&&X<5&&N-Y>=0&&Y<5;X++,Y++) /* 判断住对角线方向 */
if(Chess[M-X][N-Y]==Player)
CountPlayer[Player-1]++;
else break;
for(M=Current_x/50-1,N=Current_y/50-1,X=1,Y=1;M+X<8&&X<5&&N+Y<8&&Y<5;X++,Y++)
if(Chess[M+X][N+Y]==Player)
CountPlayer[Player-1]++;
else break;
if(CountPlayer[Player-1]+1>=5)
goto end;
else
CountPlayer[Player-1]=0;

for(M=Current_x/50-1,N=Current_y/50-1,X=1,Y=1;M-X>=0&&X<5&&N+Y<8&&X<5;X++,Y++) /* 判断从对角线方向 */
if(Chess[M-X][N+Y]==Player)
CountPlayer[Player-1]++;
else break;
for(M=Current_x/50-1,N=Current_y/50-1,X=1,Y=1;M+X<8&&X<5&&N-Y>=0&&Y<5;X++,Y++)
if(Chess[M+X][N-Y]==Player)
CountPlayer[Player-1]++;
else break;
if(CountPlayer[Player-1]+1>=5)
goto end;
else
CountPlayer[Player-1]=0;

end:
if(CountPlayer[Player-1]+1>=5)
Result=1;
else
Result=0;
return Result;
}

/* 游戏开始 */
void Game_start()
{
char c;
while(1)
{
c=bioskey(0);
switch (c)
{
case 'w':Clear_chess_box(); if(Current_y>=100) Current_y-=50;Draw_chess_box();break;
case 's':Clear_chess_box(); if(Current_y<=350) Current_y+=50;Draw_chess_box();break;
case 'a':Clear_chess_box(); if(Current_x>=100) Current_x-=50;Draw_chess_box();break;
case 'd':Clear_chess_box(); if(Current_x<=350) Current_x+=50;Draw_chess_box();break;
case 'q':Flag=1;break;
case 'j':Draw_Player_circle(Player);Update_chess();Result=Judgement();Player=Player%2+1;/* 切换棋手 */
default: ;
}
if(Flag)
break;
if(Result==1)
break;
}
}

/* 提前退出游戏 */
void Output_exit()
{
char *p="BYE - - - BYE ^_^ ";
setcolor(RED);
settextstyle(1,0,4);
outtextxy(150,430,p);
}

/* 输出输赢信息 */
void Output_win()
{
char *p="You Win !";
outtextxy(190,430,p);
}

/* BGI初始化 */
void initgr(void)
{
int gd = DETECT, gm = 0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "");
}

/* 主函数 */
int main(void)
{
initgr(); /* BGI初始化 */
Draw_chess_background();
Draw_chess_box();
Game_start();
if(Flag==1)
{
Output_exit();
printf("\a");
}
if(Result==1)
Output_win();
getch();
closegr(); /* 恢复TEXT屏幕模式 */
return 0;
}

这个是以前编写的,你可以参考着改改棋盘。。。
这个是图形界面的。

回答(2):

LS的可能看错了

LZ只要对战的

不明白你要什么算法,建议你将棋盘看成一个整数矩阵

里面放了黑的赋值为1,白的赋值为2,没赋值的为0

每次下子时判断落子位置是否为0,如果是则改变该位置的值为1或2(通过一个变量来判断现在是谁在下棋)

接着再扫描整张棋盘,看是否存在连续5个相同颜色的子,如果存在则游戏结束,否则轮到另一方走棋

回答(3):

人工智能的东西
你认为20分就能解决,这玩意要费力学一下人工智能才能做好的