实验一五子棋游戏解答.docx
《实验一五子棋游戏解答.docx》由会员分享,可在线阅读,更多相关《实验一五子棋游戏解答.docx(16页珍藏版)》请在冰豆网上搜索。
![实验一五子棋游戏解答.docx](https://file1.bdocx.com/fileroot1/2023-2/25/242d03dc-d460-4462-89a7-88a800893149/242d03dc-d460-4462-89a7-88a8008931491.gif)
实验一五子棋游戏解答
院系:
计算机学院
实验课程:
计算机基础实验
实验项目:
实验七简单生态系统仿真实验
指导老师:
开课时间:
2015~2016年度第2学期
专业:
计算机类
班级:
学生:
学号:
实验一五子棋游戏(人机对战)
1.实验简介
五子棋的规则:
双方各执一种颜色的棋子,轮流下子(将子放在棋盘的任一未下子的点上),直到有一方的棋子有5个排成一线(无论是横、竖还是斜均可),则棋局结束,该方胜利。
2.实验要求:
(1)独立思考完成;
(2)代码结构清晰,编程风格良好;
3.完成时间
课堂1次课
4.参考运行界面
图1程序启动界面
图2游戏运行界面
图3游戏结束界面
#include
#include
#include
//chargui[11]={"┌","┬","┐","├","┼","┤","└","┴","┘","○","●"};
classchess{
private:
intsize,depth,board[100][100],score[100][100];
charcolor;
voidprintBoard(intx,inty);
voidprintSplit();
intMiniMax(intturn);
voidchessScore();
intevaluate(intx,inty);
public:
intwin;
voidstartmenu();
voidstartchess();
voidifwin(intx,inty,intplayer);
voidplayer_1();
voidplayer_2();
voidgame_over();
voidcomputer();
chess();
};
chess:
:
chess(){
depth=3;
win=0;
}
voidchess:
:
printBoard(intx,inty){
if(board[x][y]==1)
printf("●");
elseif(board[x][y]==-1)
printf("○");
elseif(board[x][y]==0){
if(x==0&&y==0)
printf("┌");
elseif(x==size-1&&y==size-1)
printf("┘");
elseif(x==0&&y==size-1)
printf("┐");
elseif(x==size-1&&y==0)
printf("└");
elseif(x==0)
printf("┬");
elseif(y==0)
printf("├");
elseif(x==size-1)
printf("┴");
elseif(y==size-1)
printf("┤");
else
printf("┼");
}
return;
}
voidchess:
:
printSplit(){
printf("<================================================>\n");
return;
}
voidchess:
:
startmenu(){
printSplit();
printf("Pleaseinputthesizeofthechessboard\n");
printSplit();
scanf("%d",&size);
/*printf("Doyoulike");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN|FOREGROUND_RED);
printf("Red");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
printf("or");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE);
printf("Black");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
printf("?
(Typethefirstcharacter.)\n");
scanf("%c",&color);
scanf("%c",&color);*/
system("cls");
return;
}
voidchess:
:
startchess(){
system("color3E");
printf("");
for(inti=0;iprintf("%2d",i);
printf("\n");
for(inti=0;iprintf("%3d",i);
for(intj=0;jboard[i][j]=0;
printBoard(i,j);
}
printf("\n");
}
printf("(player_1)Inputthepositionyouwant:
");
return;
}
voidchess:
:
ifwin(intx,inty,intplayer){
intcolumn,line,ldiagonal,rdiagonal;
for(inti=0;i<5&&x+i<=size;i++){
column=line=ldiagonal=rdiagonal=0;
for(intj=0;j<5&&x-i>=0;j++){
column+=board[x-i+j][y];
ldiagonal+=board[x-i+j][y-i+j];
}
for(intj=0;j<5&&x+irdiagonal+=board[x+i-j][y-i+j];
for(intj=0;j<5&&y-i>=0;j++)
line+=board[x][y-i+j];
if(line==5||line==-5||column==5||column==-5||ldiagonal==5||ldiagonal==-5||rdiagonal==5||rdiagonal==-5){
win=player;
return;
}
}
return;
}
voidchess:
:
player_1(){
intx,y;
scanf("%d%d",&x,&y);
while(board[x][y]!
=0||x>size||y>size){
printf("Wrongposition!
Pleaseinputanotherposition:
");
scanf("%d%d",&x,&y);
}
system("cls");
board[x][y]=1;
printf("");
for(inti=0;iprintf("%2d",i);
printf("\n");
for(inti=0;iprintf("%3d",i);
for(intj=0;jprintBoard(i,j);
printf("\n");
}
ifwin(x,y,1);
if(!
win)
printf("(player_2)Inputthepositionyouwant:
");
else{
system("cls");
game_over();
}
return;
}
voidchess:
:
player_2(){
intx,y;
scanf("%d%d",&x,&y);
while(board[x][y]!
=0||x>size||y>size){
printf("Wrongposition!
Pleaseinputanotherposition:
");
scanf("%d%d",&x,&y);
}
system("cls");
board[x][y]=-1;
printf("");
for(inti=0;iprintf("%2d",i);
printf("\n");
for(inti=0;iprintf("%3d",i);
for(intj=0;jprintBoard(i,j);
printf("\n");
}
ifwin(x,y,-1);
if(!
win)
printf("(player_1)Inputthepositionyouwant:
");
else{
system("cls");
game_over();
}
return;
}
voidchess:
:
game_over(){
printSplit();
if(win==1)
printf("Player_1wins!
\n");
else
printf("Player_2wins!
\n");
printSplit();
return;
}
/*intchess:
:
evaluate(intx,inty){
intvalue;
if(ifwin(x,y,-1))
value=100;
elseif(
returnvalue;
}*/
//clear!
!
!
/*intchess:
:
MiniMax(intturn){
intminMax,temp;
if(turn&1)
minMax=INT_MIN;
else
minMax=INT_MAX;
for(inti=0;ifor(intj=0;jif(!
board[i][j]){
if(turn==depth)
temp=evaluate(i,j);
else{
if(turn&1)
board[i][j]=-1;
else
board[i][j]=1;
temp=MiniMax(turn+1);
board[i][j]=0;
if(turn&1){
if(temp>minMax)
minMax=temp;
}
else{
if(tempminMax=temp;
}
}
}
returnminMax;
}*/
/*voidchess:
:
chessScore(){
for(inti=0;ifor(intj=0;jif(!
board[i][j]){
board[i][j]=-1;
score[i][j]=MiniMax
(2);
board[i][j]=0;
}
return;
}
voidchess:
:
computer(){
intx,y,maxScore=INT_MIN;
chessScore();
for(inti=0;ifor(intj=0;jif(maxScoremaxScore=score[i][j];
x=i;
y=j;
}
//computerplayer[x,y]
system("cls");
board[x][y]=-1;
printf("");
for(inti=0;iprintf("%2d",i);
printf("\n");
for(inti=0;iprintf("%3d",i);
for(intj=0;jprintBoard(i,j);
printf("\n");
}
ifwin(x,y,-1);
if(!
win)
printf("(player_1)Inputthepositionyouwant:
");
else{
system("cls");
game_over();
}
return;
}*/
intmain()
{
SetConsoleTitle("Chess");
system("color3E");
chessone;
one.startmenu();
one.startchess();
while
(1){
one.player_1();
if(one.win)
break;
one.player_2();
//puter();
if(one.win)
break;
}
system("pause");
return0;
}
运行结果:
实验总结:
优点:
通过使用windowsapi使得界面更好看,并且通过特殊字符,可以显示出棋盘的网格。
缺点:
电脑下棋的能力太低,需要在进一步改善代码。