黑白棋程序设计实验报告C++版Word文档格式.docx
《黑白棋程序设计实验报告C++版Word文档格式.docx》由会员分享,可在线阅读,更多相关《黑白棋程序设计实验报告C++版Word文档格式.docx(27页珍藏版)》请在冰豆网上搜索。
4、计算可落子的位置个数,及该位置落子后翻过的棋子的个数
5、设置棋子的稳定性(计算得分的依据),空白处除外
6、评价棋手得分
游戏开始选择类型和棋子:
打印棋盘棋手开始下棋输入下棋的位置:
游戏结束,统计棋数并分出胜负:
一局结束后选择是否继续:
三、性能分析
人机对战中稳定性算法用了8层循环,所以程序的性能为O(N8)级别
四、实验总结
这个程序主要用来实现黑白棋的人人对战,程序中的人机对战的算法主要参考别的程序,而人人对战则相对简单所以以自己的能力就只实现了其中的人人对战部分,由于没有自学MFC,所以界面不是很友好,直接在doc环境下运行,但程序总的实现了黑白棋的基本功能,能够在doc下显示棋盘,判断棋手可下棋位置及最后判断出胜负。
五、程序代码:
#include<
iostream>
usingnamespacestd;
intconstMAX=10000;
enumColor
{
WHITE=-1,BLANK,BLACK//是否能落子//黑子
};
classChoice
public:
intposX;
intposY;
intscore;
classChessman
enumColorcolor;
unsignedstable;
/*棋子的稳定性(0~8),若棋子为BLANK则表示该位置落子后可翻过的棋子个数.*/
//即下此位置后对方的棋变为自己的棋子的数目
classChessboard//棋盘
Chessmancell[8][8];
//定义棋盘中有8*8个格子
unsignedwhiteNum;
//白棋数目
unsignedblackNum;
//黑棋数
voidinitChessboard(Chessboard*board);
voidclone(Chessboard*boardDest,constChessboard*boardSource);
voidview(Chessboard*board);
intjudge(Chessboard*board,enumColorplayer);
intputChess(Chessboard*board,Choice*choice,enumColorplayer);
voidsetStable(Chessboard*board);
intevaluate(Chessboard*board,enumColorplayer);
voidChessboard:
:
initChessboard(Chessboard*board)//初始化棋盘
{//声明一个Chessboard结构体board
inti,j;
board->
whiteNum=2;
blackNum=2;
for(i=0;
i<
8;
i++)
{
for(j=0;
j<
j++)
{
board->
cell[i][j].color=BLANK;
cell[i][j].stable=0;
}
}
cell[3][3].color=board->
cell[4][4].color=BLACK;
cell[3][4].color=board->
cell[4][3].color=WHITE;
}
//复制棋盘.
clone(Chessboard*boardDest,constChessboard*boardSource)
boardDest->
whiteNum=boardSource->
whiteNum;
blackNum=boardSource->
blackNum;
boardDest->
cell[i][j].color=boardSource->
cell[i][j].color;
cell[i][j].stable=boardSource->
cell[i][j].stable;
//显示棋盘.
view(Chessboard*board)
cout<
<
"
\n---"
;
cout<
---"
i+1;
\n────────────────\n"
i+1<
--│"
switch(board->
cell[i][j].color)
{
caseBLACK:
cout<
○│"
break;
caseWHITE:
●│"
caseBLANK:
if(board->
cell[i][j].stable)
{
cout<
+│"
}
else
│"
default:
/*棋子颜色错误*/
*│"
}
白棋(●)个数为:
board->
whiteNum<
"
黑棋(○)个数为:
blackNum<
endl<
endl;
//计算可落子的位置个数,及该位置落子后翻过的棋子的个数(board->
intChessboard:
judge(Chessboard*board,enumColorplayer)
unsignednum=0;
if(board->
cell[i][j].color==BLANK)
intx,y;
board->
for(x=-1;
x<
=1;
x++)
for(y=-1;
y<
y++)
{
if(x||y)/*8个方向*/
{
inti2,j2;
unsignednum2=0;
for(i2=i+x,j2=j+y;
i2>
=0&
&
i2<
=7&
j2>
j2<
=7;
i2+=x,j2+=y)
{
if(board->
cell[i2][j2].color==(enumColor)-player)
{
num2++;
}
elseif(board->
cell[i2][j2].color==player)
board->
cell[i][j].stable+=player*num2;
break;
cell[i2][j2].color==BLANK)
}
}
}
num++;
returnnum;
//落子,翻子.
putChess(Chessboard*board,Choice*choice,enumColorplayer)
inti=choice->
posX,j=choice->
posY;
intx,y;
if(board->
cell[i][j].color!
=BLANK||board->
cell[i][j].stable==0||player==BLANK)
return-1;
cell[i][j].color=player;
if(player==WHITE)
whiteNum++;
elseif(player==BLACK)
blackNum++;
for(x=-1;
for(y=-1;
if(x||y)/*8个方向*/
inti2,j2;
for(i2=i+x,j2=j+y;
if(board->
num++;
elseif(board->
board->
whiteNum+=(player*WHITE)*num;
blackNum+=(player*BLACK)*num;
for(i2-=x,j2-=y;
num>
0;
num--,i2-=x,j2-=y)
cell[i2][j2].color=player;
cell[i2][j2].stable=0;
break;
return0;
/*
*设置棋子的稳定性(计算得分的依据),空白处除外.
*/
setStable(Chessboard*board)
for(j=0;
=BLANK)
cell[i][j].stable=1;
{
/*4个方向*/
if(x==0&
y==0)
x=2;
y=2;
else
inti2,j2,flag=2;
cell[i2][j2].color!
=board->
flag--;
break;
for(i2=i-x,j2=j-y;
i2-=x,j2-=y)
if(flag)/*在某一条线上稳定*/
cell[i][j].stable++;
*评价棋手得分.
evaluate(Chessboard*board,enumColorplayer)
intvalue=0;
setStable(board);
value+=(board->
cell[i][j].color)*(board->
cell[i][j].stable);
value+=64*board->
cell[0][0].color;
cell[0][7].color;
cell[7][0].color;
cell[7][7].color;
value-=32*board->
cell[1][1].color;
cell[1][6].color;
cell[6][1].color;
cell[6][6].color;
returnvalue*player;
//考虑step步,选择最优方案.采用最大最小博弈和α-β剪裁算法
Choice*maximin(Chessboard*board,enumColorplayer,intstep,intmin,intmax,Choice*choice)
inti,j,k,num;
Choice*allChoices;
choice->
score=-MAX;
posX=-1;
posY=-1;
num=board->
judge(board,player);
if(num==0)/*无处落子*/
judge(board,(enumColor)-player))/*对方可以落子,让对方下.*/
ChessboardtempBoard;
ChoicenextChoice;
Choice*pNextChoice=&
nextChoice;
clone(&
tempBoard,board);
pNextChoice=maximin(&
tempBoard,(enumColor)-player,step-1,-max,-min,pNextChoice);
score=-pNextChoice->
score;
returnchoice;
else/*对方也无处落子,游戏结束.*/
intvalue=WHITE*(board->
whiteNum)+BLACK*(board->
blackNum);
if(player*value>
0)
score=MAX-1;
elseif(player*value<
score=-MAX+1;
score=0;
returnchoice;
if(step<
=0)/*已经考虑到step步,直接返回得分*/
score=board->
evaluate(board,player);
allChoices=(Choice*)malloc(sizeof(Choice)*num);
k=0;
if(i==0||i==7||j==0||j==7)
cell[i][j].color==BLANK&
allChoices[k].score=-MAX;
allChoices[k].posX=i;
allChoices[k].posY=j;
k++;
if((i==2||i==5||j==2||j==5)&
(i>
=2&
i<
=5&
j>
j<
=5))
if((i==1||i==6||j==1||j==6)&
=1&
=6&
=6))
for(k=0;
k<
num;
k++)
ChoicethisChoice,nextChoice;
thisChoice=allChoices[k];
putChess(&
tempBoard,&
thisChoice,player);
thisChoice.score=-pNextChoice->
if(thisChoice.score>
min&
thisChoice.score<
max)/*可以预计的更优值*/
min=thisChoice.score;
score=thisChoice.score;
posX=thisChoice.posX;
posY=thisChoice.posY;
elseif(thisChoice.score>
=max)/*好的超乎预计*/
/*不如已知最优值*/
free(allChoices);
intmain(void)/////////////主函数
Chessboardboard;
Chessboard*pBoard=&
board;
enumColorplayer=BLANK,nowPlayer=BLACK;
//声明两个enum枚举变量player,nowplayer
Choicechoice;
Choice*pChoice=&
choice;
intdif=-1,step=4,success=0;
charrestart='
'
start:
/////////////////////////////////////////////
player=BLANK;
/////////////////////////////////////////////
nowPlayer=BLACK;
dif=-1;
step=4;
restart='
intb;
输入1为人人对战,否则为人机对战:
cin>
>
b;
if(b==1