单机五子棋游戏实现Word文档下载推荐.docx
《单机五子棋游戏实现Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《单机五子棋游戏实现Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。
当落子时程序进行判断五子连珠的情况,判断要在8个方向进行,8个方向分成4组,每组包含两个完全相反的方向。
当向右方向试探时,对应棋子位置的数组元素的行下标不增加(加0),列增下标加1,这样对应一个2元组(0,1);
向下方向试探时,对应棋子位置的数组元素的的行下标加1,列下标加0,对应的二元组为(1,0),。
。
依次类推。
行
列
右
1
右下
下
左下
-1
左
左上
上
右上
为了方便算法设计,将8个方向拆开成两个对应相反方向的二维数组,存储4个方向及对应相反方向值。
一个数组为:
cc[4][2]={0,1,
1,1,
1,0,
1,-1};
另一个数组为:
dd[4][2]={0,-1,
-1,-1,
-1,0,
-1,1};
当落子时,同时向4个方向及其相反方向在连续的位置上搜索同色棋子的对应的数组元素值,并求和,两个相反方向求和的结果相加,如果是>
=6并且<
=10,判断红棋胜出,如果相加结果为>
=60,判断黑方胜出。
参考算法:
intCWin:
:
win(intx,inty)
{
constintcc[4][2]={0,1,
1,1,
1,0,
1,-1};
constintdd[4][2]={0,-1,
-1,-1,
-1,0,
-1,1};
ints,s1,s2,x1,y1;
for(inti=0;
i<
4;
i++)
{
s=chess[x][y];
s1=chess[x][y];
s2=chess[x][y];
x1=x;
y1=y;
x1+=cc[i][0];
y1+=cc[i][1];
while(chess[x1][y1]==s)
{
if(((x1>
=0)&
&
(x1<
15))&
((y1>
(y1<
15)))
{
s1+=chess[x1][y1];
x1+=cc[i][0];
y1+=cc[i][1];
}
else
break;
}
x1+=dd[i][0];
y1+=dd[i][1];
s2+=chess[x1][y1];
x1+=dd[i][0];
y1+=dd[i][1];
if((s1+s2)>
=6&
(s1+s2)<
=10)return1;
=60)return2;
}
return0;
}
5、程序结构(UML类图)(供参考)
6、使用VisualC++6.0实现参考步骤:
1)StartVisualC++6.0IDE
2)CreateaSingleDocumentInterfaceProject(MFCAppWizard(exe));
SelectCFormViewasBaseClassoftheClassViewintheproject;
3)InterfaceDesignasabove;
4)AddvariablesfortheEdit-BoxandRadio-ButtoncontrolstoClassView;
5)Addastructwhichincludes2integermembers
6)Addastructasastackinwhichincludes2members,oneisainteger,anotherisaarraywhichtypeisdefinedatStep5);
7)Addaclasstotheproject,inwhichhasa15x15matrixandsomefunctionsoperatingonthematrix;
8)Addmouseclickeventprocessingfunctionsforthe2buttonsputontheCFromViewandwritecode;
9)ModifytheconstructoroftheClassView;
10)AddafunctiontotheClassViewtodrawchessnuts
11)Debugandruntheproject;
7、Referencecode:
//Win.h
structElement
{
intx;
inty;
};
structData
inttop;
Elementelement[225];
voidinitData()
top=0;
for(inti=0;
225;
element[i].x=0;
element[i].y=0;
Elementpush(Elemente)
element[top]=e;
top++;
returne;
Elementpop()
Elemente1={0,0};
if(top>
0)
top--;
e1=element[top];
returne1;
classCWin
public:
voidinit();
intwin(intx,inty);
intchess[15][15];
CWin();
virtual~CWin();
//Win.cpp
CWin:
CWin()
this->
init();
~CWin()
constintdd[4][2]={0,-1,
-1,-1,
voidCWin:
init()
for(inti1=0;
i1<
15;
i1++)
for(intj1=0;
j1<
j1++)
chess[i1][j1]=0;
///FiveView.h
classCFiveView:
publicCFormView
…
voiddraw(CDC*p,intx,inty);
Elemente;
Datadata;
CWinwin;
//FiveView.cpp
CFiveView:
CFiveView():
CFormView(CFiveView:
IDD)
…
m_r=0;
data.initData();
}voidCFiveView:
OnDraw(CDC*pDC)
{for(inti=30;
=450;
i+=30)
pDC->
MoveTo(30,i);
LineTo(450,i);
MoveTo(i,30);
LineTo(i,450);
doublex;
doubley;
for(intn1=0;
n1<
n1++)
for(intm1=0;
m1<
m1++)
if((win.chess[m1][n1]==1)||win.chess[m1][n1]==10)
draw(pDC,n1,m1);
voidCFiveView:
O