java含类图五子棋小游戏.docx
《java含类图五子棋小游戏.docx》由会员分享,可在线阅读,更多相关《java含类图五子棋小游戏.docx(29页珍藏版)》请在冰豆网上搜索。
java含类图五子棋小游戏
Java设计报告书
课程名称:
JAVA语言程序设计
设计题目:
五子棋小游戏
院系:
计算机科学与信息工程系
学生姓名:
学号:
专业班级:
指导教师:
2010年12月31日
一、题目描述:
五子棋是一种两人对弈的纯策略型棋类游戏,是起源于中国,传统五子棋的棋具与围棋相同,棋子分为黑白两色,棋盘为17×10,棋子放置于棋盘线交叉点上。
两人对局,各执一色,轮流下一子,先将横、竖或斜线的5个或5个以上同色棋子连成不间断的一排者为胜。
本课题的功能就是能按照五子棋的规则实现人机对战,并能顺利结束棋局。
二、设计思路:
1类图:
2说明:
表1.1java源文件及其功能
序号
文件名
主要功能
备注
1
Chess
本程序的主函数
初始化棋盘
2
Autoplay
实现电脑可以自动下棋
分为8种情况
3
Scan
扫描整个棋盘记录连在一起的黑白棋子数
为判断胜负服务
4
Sort
实现人与电脑有次序的下棋
黑棋白棋有序的下
5
Judge
判断人与电脑的胜负
可以在文本框中显示胜负结果
6
Evaluate
电脑估计
为电脑下棋提供帮助
表2.1Chess
成员变量描述
变量类型
名称
控制棋子颜色
int
i
重新开局
Button
Button
请黑棋下子
TextField
text_1
请白棋下子
TextField
text_2
这是第"+i+"步
TextField
text_3
表2.2Chess
方法名
功能
备注
ChessPad()
初始化棋盘
设置棋盘颜色,
Voidpaint(Graphicsg)
对棋子等图像进行初始化
设置棋子大小,颜色等
publicvoidmousePressed(MouseEvente)
鼠标的实践监听
单击实践
publicvoidpaint(Graphicsg)
画出图像
控制图像的大小范围
publicvoidmousePressed(MouseEvente)
处理按下鼠标的事件
选中下棋的位置
publicvoidmouseReleased(MouseEvente)
处理鼠标离开的事件
本步下棋结束
publicvoidmouseExited(MouseEvente)
处理鼠标离开棋盘时
鼠标离开组件不实现任何事件
publicvoidmouseClicked(MouseEvente)
处理发生单击的事件
实现下棋以及重新开始游戏
表2.3Autoplay
成员变量描述
变量类型
名称
统计玩家的连起来的棋子数
int
X
电脑玩家的连起来的棋子数
Int
Y
表2.4Autoplay
方法名
功能
备注
voidautoPlay(intchesspad[][],inta,intb)
分情况实现电脑自动下棋
使用switch语句分为8种情况来下棋
表2.5Scan
成员变量描述
变量类型
名称
统计棋盘种五个连起来的棋子数
数组
intshape[][][]
控制行数
int
i
控制列数
int
j
表2.6Scan
方法名
功能
备注
voidscan(intchesspad[][],intcolour){
判断棋盘上连在一起的黑白棋子个数。
表2.7Sort
成员变量描述
变量类型
名称
保存临时变量
int
temp
表2.8Sort
方法名
功能
备注
publicvoidsort(intshape[][][])
实现黑白棋子的交替下
数组为参数
表2.9Judge
成员变量描述
变量类型
名称
控制行数
int
i
控制列数
int
j
记录连在一起的黑白棋子数
int
flag
表2.10Judge
方法名
功能
备注
staticbooleanjudge(inta[][],intcolor)
判断玩家与电脑的游戏胜负
使用静态函数,分多钟情况判断
表2.11Evaluate
成员变量描述
变量类型
名称
玩家最可能下的棋子的行数
int
max_x
玩家最可能下的棋子的列数
int
max_y
保存最大可能下棋的位置
int
max
控制行数
int
i
控制列数
int
j
表2.12Evaluate
方法名
功能
备注
publicvoidevaluate(intshape[][][])
电脑用于估计玩家下步棋的走法
三、运行结果:
四、源代码:
importjava.awt.*;
importjava.awt.event.*;
classChessPadextendsPanelimplementsMouseListener,ActionListener{
intarray[][]=newint[19][19];
Scanscanp=newScan();
Scanscanc=newScan();
AutoPlayautoPlay=newAutoPlay();
Evaluateevaluatep=newEvaluate();
Evaluateevaluatec=newEvaluate();
Sortsort=newSort();
inti=0;
intx=-1,y=-1,棋子颜色=1;
Buttonbutton=newButton("重新开局");
TextFieldtext_1=newTextField("请黑棋下子"),
text_2=newTextField(),
text_3=newTextField();
ChessPad(){
setSize(440,440);
setLayout(null);setBackground(Color.pink);
addMouseListener(this);add(button);button.setBounds(10,5,60,26);
button.addActionListener(this);
add(text_1);text_1.setBounds(90,5,90,24);
add(text_2);text_2.setBounds(290,5,90,24);
add(text_3);text_3.setBounds(200,5,80,24);
for(inti=0;i<19;i++)
for(intj=0;j<19;j++)
{array[i][j]=0;}
for(inti=0;i<19;i++)
for(intj=0;j<19;j++)
for(inth=0;h<5;h++)
{scanp.shape[i][j][h]=0;
scanc.shape[i][j][h]=0;}
text_1.setEditable(false);
text_2.setEditable(false);
}
publicvoidpaint(Graphicsg){
for(inti=40;i<=400;i=i+20){
g.drawLine(40,i,400,i);
}
for(intj=40;j<=400;j=j+20){
g.drawLine(j,40,j,400);
}
g.fillOval(97,97,6,6);
g.fillOval(337,97,6,6);
g.fillOval(97,337,6,6);
g.fillOval(337,337,6,6);
g.fillOval(217,217,6,6);
}
publicvoidmousePressed(MouseEvente){
inta=0,b=0;
if(e.getModifiers()==InputEvent.BUTTON1_MASK){
x=(int)e.getX();y=(int)e.getY();
ChessPoint_blackchesspoint_black=newChessPoint_black(this);
ChessPoint_whitechesspoint_white=newChessPoint_white(this);
i++;
text_3.setText("这是第"+i+"步");
if((x+5)/20<2||(y+5)/20<2||(x-5)/20>19||(y-5)/20>19)
{}
else{a=(x+10)/20;b=(y+10)/20;
if(array[b-2][a-2]==0&&棋子颜色==1){
this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-9,b*20-9,18,18);
棋子颜色=棋子颜色*(-1);
array[b-2][a-2]=1;
if(Judge.judge(array,1)){
text_1.setText("黑棋赢!
");棋子颜色=2;removeMouseListener(this);}
else{
text_1.setText("");}
}
}
if(i>2&&棋子颜色==-1){
scanp.scan(array,1);
scanc.scan(array,-1);
sort.sort(scanp.shape);
sort.sort(scanc.shape);
evaluatep.evaluate(scanp.shape);
evaluatec.evaluate(scanc.shape);
棋子颜色=棋子颜色*(-1);
this.add(chesspoint_white);
if(evaluatep.max>evaluatec.max){
text_2.setText(evaluatep.max_x+""+evaluatep.max_y+""+evaluatep.max);
chesspoint_white.setBounds((evaluatep.max_y+2)*20-9,(evaluatep.max_x+2)*20-9,18,18);
array[evaluatep.max_x][evaluatep.max_y]=-1;
text_1.setText("请黑棋下子");
for(inti=0;i<19;i++)
for(intj=0;j<19;j++)
for(inth=0;h<5;h++)
{scanp.shape[i][j][h]=0;
scanc.shape[i][j][h]=0;}
}
else
{
text_2.setText(evaluatec.max_x+""+evaluatec.max_y+""+evaluatec.max);
chesspoint_white.setBounds((evaluatec.max_y+2)*20-9,(evaluatec.max_x+2)*20-9,18,18);
array[evaluatec.max_x][evaluatec.max_y]=-1;
if(Judge.judge(array,-1))
{
text_2.setText("白棋赢!
");棋子颜色=2;removeMouseListener(this);}
else{
text_1.setText("请黑棋下子");
for(inti=0;i<19;i++)
for(intj=0;j<19;j++)
for(inth=0;h<5;h++)
{scanp.shape[i][j][h]=0;
scanc.shape[i][j][h]=0;}
}
}
}
if(i<=2&&棋子颜色==-1)
{
autoPlay.autoPlay(array,b-2,a-2);
this.add(chesspoint_white);
棋子颜色=棋子颜色*(-1);
chesspoint_white.setBounds((autoPlay.y+2)*20-9,(autoPlay.x+2)*20-9,18,18);
array[autoPlay.x][autoPlay.y]=-1;
if(Judge.judge(array,-1))
{
text_2.setText("白棋赢!
");棋子颜色=2;removeMouseListener(this);}
else{
text_1.setText("请黑棋下子");
text_2.setText(autoPlay.x+""+autoPlay.y);}
}
}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){}
publicvoidactionPerformed(ActionEvente)
{this.removeAll();
棋子颜色=1;
add(button);button.setBounds(10,5,60,26);
add(text_1);text_1.setBounds(90,5,90,24);
text_2.setText("");
text_1.setText("请黑棋下子");
add(text_2);
text_2.setBounds(290,5,90,24);
add(text_3);text_3.setBounds(200,5,80,24);
i=0;
text_3.setText("这是第"+i+"步");
for(inti=0;i<19;i++)
for(intj=0;j<19;j++)
{array[i][j]=0;}
for(inti=0;i<19;i++)
for(intj=0;j<19;j++)
for(inth=0;h<5;h++)
{scanp.shape[i][j][h]=0;
scanc.shape[i][j][h]=0;}
addMouseListener(this);
}
}
classChessPoint_blackextendsCanvasimplementsMouseListener{
ChessPadchesspad=null;
ChessPoint_black(ChessPadp){
setSize(20,20);
addMouseListener(this);
chesspad=p;
}
publicvoidpaint(Graphicsg){
g.setColor(Color.black);g.fillOval(0,0,18,18);
}
publicvoidmousePressed(MouseEvente){
/*if(e.getModifiers()==InputEvent.BUTTON3_MASK){
chesspad.remove(this);
chesspad.棋子颜色=1;
chesspad.text_2.setText("");
chesspad.text_1.setText("请黑棋下子");
}*/
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){
}
}
classChessPoint_whiteextendsCanvasimplementsMouseListener{
ChessPadchesspad=null;
ChessPoint_white(ChessPadp){
setSize(20,20);addMouseListener(this);
chesspad=p;
}
publicvoidpaint(Graphicsg){
g.setColor(Color.white);g.fillOval(0,0,18,18);
}
publicvoidmousePressed(MouseEvente){
/*if(e.getModifiers()==InputEvent.BUTTON3_MASK){
chesspad.remove(this);
chesspad.棋子颜色=-1;
chesspad.text_2.setText("请白棋下子");
chesspad.text_1.setText("");
}*/
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){
}
}
publicclassChessextendsFrame{
ChessPadchesspad=newChessPad();
Chess(){
setVisible(true);
setLayout(null);
Labellabel=newLabel("五子棋",Label.CENTER);
add(label);label.setBounds(70,55,440,26);
label.setBackground(Color.orange);
add(chesspad);chesspad.setBounds(70,90,440,440);
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);}
});
pack();setSize(600,550);
}
publicstaticvoidmain(Stringargs[]){
Chesschess=newChess();
}
}
classAutoPlay{
intx,y;
voidautoPlay(intchesspad[][],inta,intb){
intrandomNumber=(int)(Math.random()*8)+1;
switch(randomNumber){
case
(1):
if(chesspad[a-1][b-1]==0)
{x=a-1;y=b-1;}
elseif(chesspad[a-2][b-2]==0)
{x=a-2;y=b-2;}
else{x=a-3;y=b-3;}
break;
case
(2):
if(chesspad[a-1][b]==0)
{x=a-1;y=b;}
elseif(chesspad[a-2][b]==0)
{x=a-2;y=b;}
else{x=a-3;y=b;}
break;
case(3):
if(chesspad[a-1][b+1]==0)
{x=a-1;y=b+1;}
elseif(chesspad[a-2][b+2]==0)
{x=a-2;y=b+2;}
else{x=a-3;y=b+3;}
break;
case(4):
if(chesspad[a][b+1]==0)
{x=a;y=b+1;}
elseif(chesspad[a][b+2]==0)
{x=a;y=b+2;}
else{x=a;y=b+3;}
break;
case(5):
if(chesspad[a+1][b+1]==0)
{x=a+1;y=b+1;}
elseif(chesspad[a+2][b+2]==0)
{x=a+2;y=b+2;}
else{x=a+3;y=b+3;}
break;
case(6):
if(chesspad[a+1][b]==0)
{x=a+1;y=b;}
elseif(chesspad[a+2][b]==0)
{x=a+2;y=b;}
else{x=a+3;y=b;}
break;
case(7):
if(chesspad[a+1][b-1]==0)
{x=a+1;y=b-1;}
elseif(chesspad[a+2][b-2]==0)
{x=a+2;y=b-2;}
else{x=a+3;y=b-3;}
break;
case(8):
if(chesspad[a][b-1]==0)
{x=a;y=b-1;}
elseif(chesspad[a][b-2]==0)
{x=a;y=b-2;}
else{x=a;y=b+3;}
break;
}
}
}
classEvaluate{
intmax_x,max_y,max;
publicvoidevaluate(intshape[][][]){
inti=0,j=0;
for(i=0;i<19;i++)
for(j=0;j<19;j++){
switch(shape[i][j][0]){
case5:
shape[i][j][4]=200;
break;
case4:
switch(shape[i][j][1]){
case4:
shape[i][j][4]=150+shape[i][