五子棋人机对战java代码文档格式.docx
《五子棋人机对战java代码文档格式.docx》由会员分享,可在线阅读,更多相关《五子棋人机对战java代码文档格式.docx(25页珍藏版)》请在冰点文库上搜索。
this.jContentPane.add(newBjpanel());
returnjContentPane;
}
文件二:
importjava.awt.Font;
importjava.awt.Graphics;
importjavax.imageio.ImageIO;
importjavax.swing.JOptionPane;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
import.URL;
publicclassBjpanelextendsJPanelimplementsMouseListener
privatestaticfinallongserialVersionUID=1L;
//素材
BufferedImagebackgroundImage=null;
BufferedImagewhiteImage1=null;
BufferedImageblackImage2=null;
BufferedImagechessboardImage=null;
//棋子坐标
intx=0;
inty=0;
//保存下过棋子的全部坐标
//数据内容含义:
0:
这个点没有棋子1:
这个点是黑子2:
这个点是白子
int[][]allqz=newint[15][15];
//标志下一步该黑走还是白走
booleanflag=true;
//能否进行游戏的标志
booleancanplay=true;
//记录每个可放白棋的点的优先大小,数字越大,越优先在这个点放白棋
int[][]youxian=newint[15][15];
//最优先
int[][]zyouxian=newint[15][15];
//保存游戏信息
Stringmessage="
黑方下子"
;
publicBjpanel()
super();
initialize();
privatevoidinitialize()
this.setSize(500,500);
try{
URLimgURL=Bjpanel.class.getResource("
background.jpg"
URLimgURL1=Bjpanel.class.getResource("
white.png"
URLimgURL2=Bjpanel.class.getResource("
black.png"
URLimgURL3=Bjpanel.class.getResource("
chessboard.jpg"
backgroundImage=ImageIO.read(imgURL);
whiteImage1=ImageIO.read(imgURL1);
blackImage2=ImageIO.read(imgURL2);
chessboardImage=ImageIO.read(imgURL3);
}
catch(IOExceptione)
e.printStackTrace();
this.addMouseListener(this);
this.setLayout(null);
//启动线程
publicvoidpaint(Graphicsg)
g.drawImage(backgroundImage,0,0,this);
g.drawImage(chessboardImage,10,50,363,364,this);
g.setFont(newFont("
黑体"
Font.BOLD,20));
g.drawString("
游戏信息:
"
+message,150,40);
宋体"
0,12));
黑方时间:
无限制"
30,450);
白方时间:
250,450);
//绘制棋子
for(inti=0;
i<
15;
i++)
for(intj=0;
j<
j++)
{
if(allqz[i][j]==1)
{
inttempx=i*24+18;
inttempy=j*24+58;
g.drawImage(blackImage2,tempx,tempy,15,15,this);
}
if(allqz[i][j]==2)
g.drawImage(whiteImage1,tempx,tempy,15,15,this);
publicvoidmouseClicked(MouseEvente)
publicvoidmouseEntered(MouseEvente)
publicvoidmouseExited(MouseEvente)
publicvoidmousePressed(MouseEvente)
if(canplay)
x=e.getX();
y=e.getY();
if(x>
=18&
&
x<
=379&
y>
=58&
y<
=580)
x=(x-18)/24;
y=(y-58)/24;
//判断该坐标是否有棋子,没有则放棋子,有则显示已经有棋子对话框
if(allqz[x][y]==0)
//玩家鼠标点击的坐标设置为放黑子标志
allqz[x][y]=1;
message="
白方走棋"
this.repaint();
booleanWinflag=this.Win();
if(Winflag)
JOptionPane.showMessageDialog(this,"
游戏结束:
+(allqz[x][y]==1?
黑方胜"
:
白方胜"
));
canplay=false;
if(canplay)
//电脑通过最优算法获得最能够阻止玩家的黑棋成为5连得坐标x,y并把这个坐标设置为放白子标记
//allqz[x][y]=2;
this.zuiyouBaiqi();
message="
黑方走棋"
//判断落棋后是否连成5个,即判断谁赢并游戏结束
Winflag=this.Win();
}
else
JOptionPane.showMessageDialog(this,"
已经有棋子,请重新落子!
//点击开始游戏按钮
if(e.getX()>
=401&
e.getX()<
=469&
e.getY()>
=51&
e.getY()<
=79)
intresult=JOptionPane.showConfirmDialog(this,"
是否重新开始游戏?
canplay=true;
if(result==0)
//把棋盘清空---将allqz[][]全部数据清零
allqz=newint[15][15];
//将游戏信息提示变回出示位置
黑方先行"
this.repaint();
=151&
=179)
JOptionPane.showMessageDialog(this,"
这是一个简易五子棋游戏,玩家持黑棋,电脑持白棋,"
任一方先在棋盘上形成横向、竖向、斜向的连续的相同颜色的五个(含五个以上)棋子的一方为胜!
=252&
=280)
intb=JOptionPane.showConfirmDialog(this,"
确定认输"
if(b==0)
JOptionPane.showMessageDialog(this,"
玩家认输!
电脑赢!
canplay=false;
=302&
=330)
简易五子棋1.0单机版"
=351&
=380)
inta=JOptionPane.showConfirmDialog(this,"
确定退出游戏"
if(a==0)
System.exit(0);
publicvoidmouseReleased(MouseEvente)
privatebooleanWin()
booleanflag=false;
//判断横向是否连成5个
//保存相同颜色棋子个数
intcount1=1;
intcolor=allqz[x][y];
inti1=1;
while(allqz[x+i1][y]==color)
count1++;
i1++;
i1=1;
while(allqz[x-i1][y]==color)
if(count1>
=5)
flag=true;
intcount2=1;
inti2=1;
while(allqz[x][y+i2]==color)
count2++;
i2++;
i2=1;
while(allqz[x][y-i2]==color)
if(count2>
intcount3=1;
inti3=1;
while(allqz[x+i3][y-i3]==color)
count3++;
i3++;
i3=1;
while(allqz[x-i3][y+i3]==color)
if(count3>
intcount4=1;
inti4=1;
while(allqz[x+i4][y+i4]==color)
count4++;
i4++;
i4=1;
while(allqz[x-i4][y-i4]==color)
if(count4>
returnflag;
publicvoidzuiyouBaiqi()
intjishu=0;
intpdflage=0;
inti3=0;
booleanm=false;
for(inti=1;
14;
for(intj=1;
if(allqz[i][j]==0)
intcount1=0,count2=0,count3=0,count4=0,count5=0,count6=0,count7=0,count8=0;
intcolor=1;
inti1=1;
while(allqz[i+i1][j]==color)
{
count1++;
i1++;
}
i1=1;
while(allqz[i-i1][j]==color)
count2++;
while(allqz[i][j+i1]==color)
count3++;
while(allqz[i][j-i1]==color)
count4++;
while(allqz[i+i1][j-i1]==color)
count5++;
while(allqz[i-i1][j+i1]==color)
count6++;
while(allqz[i+i1][j+i1]==color)
count7++;
while(allqz[i-i1][j-i1]==color)
count8++;
if(count1>
=1)
jishu++;
if(count2>
if(count3>
if(count4>
if(count5>
if(count6>
if(count7>
if(count8>
if(count1>
=2||count2>
=2||count3>
=2||count4>
=2||count5>
=2||count6>
=2||count7>
=2||count8>
=2)
pdflage++;
}else{zyouxian[i][j]=0;
if(count1==2||count2==2||count3==2||count4==2||count5==2||count6==2||count7==2||count8==2)
zyouxian[i][j]=2;
if(count1==3||count2==3||count3==3||count4==3||count5==3||count6==3||count7==3||count8==3)
zyouxian[i][j]=3;
if(count1==4||count2==4||count3==4||count4==4||count5==4||count6==4||count7==4||count8==4)
zyouxian[i][j]=4;
youxian[i][j]=jishu;
jishu=0;
else{youxian[i][j]=-1;
if(pdflage>
0)
if(m==false)
a2:
for(i3=0;
i3<
i3++)
for(intj4=0;
j4<
j4++)
if(zyouxian[i3][j4]==4&
allqz[i3][j4]==0)
allqz[i3][j4]=2;
m=true;
x=i3;
y=j4;
breaka2;
a3:
if(zyouxian[i3][j4]==3&
breaka3;
a1:
if(zyouxian[i3][j4]==2&
breaka1;
else
if(m==false)
b1:
for(intj3=0;
j3<
j3++)
if(youxian[i3][j3]==8)
allqz[i3][j3]=2;
m=true;
x=i3;
y=j3;
breakb1;
if(m==false)
b2:
for(intj3=0;
if(youxian[i3][j3]==7)
{
allqz[i3][j3]=2;
m=true;
x=i3;
y=j3;
breakb2;
}
b3:
15