当前位置:文档之家› 数据结构课程设计实验报告五子棋

数据结构课程设计实验报告五子棋

湖南科技学院实验报告系部:计算机与通信工程系专业:软件工程姓名:范清文学号:201008003113班级:软件1001班课程名称:数据结构程序课程设计指导老师:卿武明实验项目:制作简单的五子棋课程设计目的:1:对本学期所学的专业知识进行实践运用2:制作五子棋并实现它的图示化实验环境:Visual C++6.0实验内容(算法、程序、步骤和方法):#include<iostream>#include<conio.h>using namespace std;int x,y,Step=0;bool flag=0;char Switch;char map[20][20][3]={ {"┌","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┐"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤"}, {"└","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┘"}}; char copymap[20][20][2];float Player[20][20][1];int Table[20][20] ,Showboard(),Play(),Interface(),Initboard(),Judgewin(),Deuce() ,Initdata();bool legal(),Overflow();int Interface(){char a;cout << "\n\n五子棋\n \t\t─\n 制作人:范清文(201008003113)\n\t\t────\n";cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;cout << " ~~ ~~" << endl;cout << " ~~ 1:人人对战~~" << endl;cout << " ~~ ~~" << endl;cout << " ~~ 2:退出~~" << endl;cout << " ~~ ~~" << endl;cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;cout << "\n 请选择:";cin >> a;if(a=='2'){cout << "\n谢谢使用!\n";cout << "按任意键退出!";getch();return 0;}if(a=='1'){Play();}return 0;}int Deuce(){for(int i=0;i<20;i++)for(int j=0;j<20;j++)if(Table[i][j]==0)return 0;return 1;}int Initboard(){Step=0;for(int i=0;i<20;i++)for(int j=0;j<20;j++){map[i][j][0]=copymap[i][j][0];map[i][j][1]=copymap[i][j][1];}return 0;}int CopyB(){for(int i=0;i<20;i++)for(int j=0;j<20;j++){copymap[i][j][0]=map[i][j][0];copymap[i][j][1]=map[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(map[cx][cy][0]==a0&&map[cx][cy][1]==a1){cx--;cy--;if(cx<0||cy<0) break;}while(map[dx][dy][0]==a0&&map[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(map[cx][cy][0]==a0&&map[cx][cy][1]==a1){cx--;if(cx<0) break;}while(map[dx][dy][0]==a0&&map[dx][dy][1]==a1){dx++;if(dx>15) break;}if(dx>=(cx+6)) return 1;cx=x;cy=y;dx=x;dy=y;while(map[cx][cy][0]==a0&&map[cx][cy][1]==a1){cy--;if(cy<0) break;}while(map[dx][dy][0]==a0&&map[dx][dy][1]==a1){dy++;if(dy>15) break;}if(dy>=(cy+6)) return 1;cx=x;cy=y;dx=x;dy=y;while(map[cx][cy][0]==a0&&map[cx][cy][1]==a1){cx++;cy--;if(cx>15||cy<0) break;}while(map[dx][dy][0]==a0&&map[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 << map[i][j];if(i==6) cout << "退出:输入: 0 0 ";if(i==8) cout << "步数(Step):" << Step;if(i==10) cout << "请输入棋子的纵坐标、横坐标下子!" ;cout << endl;}return(1);}bool legal(){if((map[x-1][y-1][0]==-95&&map[x-1][y-1][1]==-16)||(map[x-1][y-1][0]==-95&&map[x-1][y-1][1]==-15)) return 0;elsereturn 1;}bool Overflow(){if(x>20||x<1||y>20||y<1){cout << "此位置不合法!请重新输入:";return 1;}elsereturn 0;}int Play(){Showboard();cout << "白方下:";while(cin >> x >> y){if(x==0&&y==0){Initboard();Interface();break;}if(Overflow()) continue;if(legal()){x--;y--;map[x][y][0]=-95;if(flag==1){map[x][y][1]=-16;Step++;Showboard();Deuce();if(Judgewin()){cout << "\n黑方胜利了!\n" ;cout << "游戏结束!再来一局?(y/n)";cin >> Switch;if(Switch!='n'){Initboard();Play();}else return 1;}cout << "白方下:";flag=0;}else{map[x][y][1]=-15;Step++;Showboard();Deuce();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;}void main(){CopyB();Interface();}数据记录与结果:课程设计结论:程序运行的很成功。

相关主题