c语言数据结构五子棋实验报告,数据结构课程设计——五子棋

#includeusing namespace std;

int x,y,Step=0;

bool flag=0;//0时表示白子,1时表示黑子

char Switch;

char wmap[20][20][3]={

{"┌","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┐"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"},

{"└","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┘"}};

char copymap[20][20][2];

int Showboard(),Play(),Interface(),Initboard(),Judgewin(),CopyB();

bool Existed(),Overflow();

//游戏用户界面接口

int Interface()

{

char a;//定义了游戏的一些功能

cout << "\n\n 五子棋\n";

cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

cout << " " ; cout << " ~~ 1:开始游戏 ~~" << endl;

cout << " " ; cout << " ~~ 2:退出游戏 ~~" << endl;

cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

cout << "\n 请选择:";

cin >> a;

if(a=='2')

{ cout << "\n谢谢使用!\n";

cout << "按任意键退出!";

getch();//输入了任意一个键,但并不会显示出该键

return 0;

}

if(a=='1') Play();

return 0;

}

//初始化棋盘

int Initboard()

{

Step=0;

for(int i=0;i<20;i++)

for(int j=0;j<20;j++)

{

wmap[i][j][0]=copymap[i][j][0];

wmap[i][j][1]=copymap[i][j][1];

}

return 0;

}

//当前棋局结束后,若想接着再下一局,则需要CopyB()将棋盘复制显现出来

int CopyB()

{

for(int i=0;i<20;i++)

for(int j=0;j<20;j++)

{

copymap[i][j][0]=wmap[i][j][0];

copymap[i][j][1]=wmap[i][j][1];

}

return 0;

}

//判断本局游戏的输赢

int Judgewin()

{

int cx,cy,dx,dy;

char a0=-95;

char a1=-16;

if(flag==0)

{

a1=-15;

}

cx=x;cy=y;dx=x;dy=y;

//左上的斜向判断

while(wmap[cx][cy][0]==a0&&wmap[cx][cy][1]==a1)

{

cx--;cy--;

if(cx<0||cy<0) break;

}

//右下的斜向判断

while(wmap[dx][dy][0]==a0&&wmap[dx][dy][1]==a1)

{

dx++;dy++;

if(dx>15||dy>15) break;

}

if(dx>=(cx+6)||dy>=(cy+6)) return 1;

cx=x;cy=y;dx=x;dy=y;

//横坐标左边判断

while(wmap[cx][cy][0]==a0&&wmap[cx][cy][1]==a1)

{

cx--;

if(cx<0) break;

}

//横坐标右边判断

while(wmap[dx][dy][0]==a0&&wmap[dx][dy][1]==a1)

{

dx++;

if(dx>15) break;

}

if(dx>=(cx+6)) return 1;

cx=x;cy=y;dx=x;dy=y;

//纵坐标上方判断

while(wmap[cx][cy][0]==a0&&wmap[cx][cy][1]==a1)

{

cy--;

if(cy<0) break;

}

//纵坐标下方判断

while(wmap[dx][dy][0]==a0&&wmap[dx][dy][1]==a1)

{

dy++;

if(dy>15) break;

}

if(dy>=(cy+6)) return 1;

cx=x;cy=y;dx=x;dy=y;

//右上的斜向判断

while(wmap[cx][cy][0]==a0&&wmap[cx][cy][1]==a1)

{

cx++;cy--;

if(cx>15||cy<0) break;

}

//左下的斜向判断

while(wmap[dx][dy][0]==a0&&wmap[dx][dy][1]==a1)

{

dx--;dy++;

if(dx<0||dy>15) break;

}

if(cx>=(dx+6)||dy>=(cy+6)) return 1;

return 0;

}

//显示出棋盘的内容

int Showboard()

{

system("CLS");//每下完一步棋,棋盘就刷新一次

cout << " ";

for(int s=1;s<=20;s++)

{

if(s>=10) cout << s<< "";

else cout << s << " ";

}

cout << endl;

for(int i=0;i<20;i++)

{

printf("%2d",i+1);

for(int j=0;j<20;j++)

cout << wmap[i][j];

if(i==6) cout << "退出:输入: 0 0 ";

if(i==8) cout << "步数(Step):" << Step;

if(i==10) cout << "请输入棋子的纵坐标、横坐标下子!" ;

cout << endl;

}

return(1);

}

//判断黑白双方的落子的地方是否已有子

bool Existed()

{

if((wmap[x-1][y-1][0]==-95&&wmap[x-1][y-1][1]==-16)||

(wmap[x-1][y-1][0]==-95&&wmap[x-1][y-1][1]==-15))

return false;

else

return true;

}

//判断输入的横纵坐标是否超出棋盘界面

bool Overflow()

{

if(x>20||x<1||y>20||y<1)

{

cout << "此位置不合法!请重新输入:";

return true;

}

else

return false;

}

//玩五子棋的过程

int Play()

{

Showboard();//先显示棋盘

cout << "白方下:";

while(cin >> x >> y)

{

if(x==0&&y==0)

{

Initboard();

Interface();

break;

}

if(Overflow()) continue;

if(Existed())

{

x--;y--;//数组的下标是从0开始算的

wmap[x][y][0]=-95;

if(flag==1)

{

wmap[x][y][1]=-16;

Step++;

Showboard();

if(Judgewin())

{

cout << "\n黑方胜利了!\n" ;

cout << "游戏结束!再来一局?(y/n)";

cin >> Switch;

if(Switch!='n')

{

Initboard();

Play();

}

else return 1;

}

cout << "白方下:";

flag=0;

}

else

{

wmap[x][y][1]=-15;

Step++;

Showboard();

if(Judgewin())

{

cout << "\n白方胜利了!" << endl;

cout << "游戏结束!再来一局?(y/n)";

cin >> Switch;

if(Switch!='n')

{

Initboard();

Play();

}

else return 1;

}

cout << "黑方下:";

flag=1;

}

}

else

{

cout << "此位置已有子!请重新输入:";

}

}

return 0;

}

int main()

{

CopyB();//本局结束,再来一局时将之前棋盘肤质显现出来

Interface();//创建一个游戏用户界面

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值