java五子棋网络版源代码加分析.docx
《java五子棋网络版源代码加分析.docx》由会员分享,可在线阅读,更多相关《java五子棋网络版源代码加分析.docx(18页珍藏版)》请在冰豆网上搜索。
java五子棋网络版源代码加分析
本人学会的一个五子棋网络版和单机版游戏,有老师指导完成,详细的解释和代码都在下面,希望可以帮助到大家!
1.客户端连接
新建个gobangClient类
packagegobang;
publicclassgobangClient{
publicstaticvoidmain(String[]args)throwsException{
newChessBoard("localhost",8866);
}
}
2.服务端连接
新建个gobangServer类
packagegobang;
publicclassgobangServer{
publicstaticvoidmain(String[]args)throwsException{
newChessBoard(8866);
}
}
3.源代码,有详细解释
packagegobang;
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.image.BufferedImage;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
import.ServerSocket;
import.Socket;
importjava.util.ArrayList;
importjava.awt.Graphics;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
publicclassChessBoardextendsJFrame
{
privatebooleanmyPlay=false;//(默认是false)是否轮到我落子
privateJButtonbutton1=
newJButton("开始");
privateJButtonbutton2=
newJButton("悔棋");
privateJPanelp1=newJPanel();
privatePicPanepp=null;
privateintside=1;//1表示黑
privateintn=0;//记录点击开始的次数
publicstaticfinalintSIZE=15;
publicstaticint[][]board=newint[SIZE][SIZE];//用来保存棋盘上棋子状态
//1为黑,2为白,0为无
publicstaticfinalintBLACK=1;
publicstaticfinalintWHITE=-1;
privateNetServiceservice;
privatestaticArrayListlist=
newArrayList();//保存了下棋的步骤
privatebooleanend=true;//true表示游戏已经结束
privateintcurrentColor=1;
//服务端的构造器
publicChessBoard(intport)throwsException{
//调用本类的无参构造树
this();//构造器调用规则:
1,只能调用本类或父类的构造器2.调用构造器的代码写在新构造器的第一行3.构造器的调用只能使用this(本类)或者(父类)
//将按钮设置能无效的
button1.setEnabled(false);
button2.setEnabled(false);
//3.创建ServerSocket对象
ServerSocketss=newServerSocket(port);
//4.等待连接
Sockets=ss.accept();
//5.连接成功后创建一个线程来处理请求
service=newNetService(s);
service.start();
//6.将按钮设置成有效的
button1.setEnabled(true);
button2.setEnabled(true);
}
//客户端的构造器
publicChessBoard(Stringip,intport)throwsException{
//调用本类无参构造器
this();
//连接服务器
Sockets=newSocket(ip,port);
//初始化服务类
service=newNetService(s);
service.start();
}
publicChessBoard()throwsIOException
{
Containerc=this.getContentPane();
c.setLayout(newBorderLayout());
pp=newPicPane();
BorderLayoutbl=newBorderLayout();
c.setLayout(bl);
FlowLayoutfl=newFlowLayout();
p1.setLayout(fl);
p1.add(button1);
p1.add(button2);
c.add(p1,BorderLayout.NORTH);
c.add(pp,BorderLayout.CENTER);
setSize(pp.getWidth()+6,pp.getHeight()+65);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
button1.addActionListener(newSomeListener());
button2.addActionListener(newbutton2Listener());
}
classSomeListenerimplementsActionListener
{
publicvoidactionPerformed(ActionEvente)
{
//button2.setVisible(true);
//if(n==0)
//{
//JOptionPane.showMessageDialog(
//ChessBoard.this,"开始游戏啦!
",
//"提示",JOptionPane.INFORMATION_MESSAGE);
//}
//else
//{
//JOptionPane.showMessageDialog(
//ChessBoard.this,"确定要重新开始?
",
//"提示",JOptionPane.INFORMATION_MESSAGE);
//}
//board=newint[15][15];
////调用重绘
//pp.repaint();
//end=false;
//n++;
start();
}
}
privatevoidstart()
{
netStart();
localStart();
myPlay=true;//谁先点了开始按钮,谁先开始
}
privatevoidlocalStart()
{
//重新初始化棋盘
board=newint[SIZE][SIZE];
//重新设置当前颜色
currentColor=BLACK;
//清空下棋步骤
list=newArrayList();
//重绘棋盘
pp.repaint();
//重新设置使下期没有结束
end=false;
}
privatevoidnetStart()
{
service.send("start");
}
classbutton2ListenerimplementsActionListener
{
publicvoidactionPerformed(ActionEvente)
{
back();
//if(end==true)
//{
//JOptionPane.showMessageDialog(
//ChessBoard.this,"人家都赢了你不能悔棋啦!
",
//"提示",JOptionPane.INFORMATION_MESSAGE);
//return;
//}
//intlength=list.size();
//if(list.size()<0)
//{return;}
//Stringstr1=list.remove(list.size()-1);
//String[]str3s=str1.split(",");
//Stringstr31=str3s[0];
//Stringstr32=str3s[1];
//introw=Integer.parseInt(str31);
//intcol=Integer.parseInt(str32);
//board[row][col]=0;
//pp.repaint();
//side=(side==1?
2:
1);//同下
//if(side==1)
//side=2;
//else
//side=1;
}
}
publicvoidback()
{
netBack();
localBack();
myPlay=!
myPlay;//经典啊 boolean的好处啊,悔棋了还是我下棋
}
privatevoidlocalBack()
{
//如果结束不能悔棋
if(end)
{
return;
}
//
if(list.isEmpty())
{
return;
}
//
Stringstr1=list.remove(list.size()-1);
String[]str3s=str1.split(",");
Stringstr31=str3s[0];
Stringstr32=str3s[1];
introw=Integer.parseInt(str31);
intcol=Integer.parseInt(str32);
board[row][col]=0;
currentColor=-currentColor;
//
pp.repaint();
//
}
privatevoidnetBack()
{if(end){// 节省时间
return;
}
service.send("back");
}
publicstaticvoidtoCenter(JFrameframe){
intwidth=frame.getWidth();
intheight=frame.getHeight();
Toolkittookie=Toolkit.getDefaultToolkit();
Dimensiondim=tookie.getScreenSize();
intscreenWidth=dim.width;
intscreenHeight=dim.height;
frame.setLocation((screenWidth-width)/2,(screenHeight-height)/2);
}
classPicPaneextendsJPanel
{
privateintwidth;
privateintheight;
privateBufferedImagebImg;//棋盘
privateBufferedImagechessImg;//黑子
privateBufferedImagewImg;//白子
publicPicPane()throwsIOException
{
InputStream
ips=
PicPane.class.getResourceAsStream("images/chessboard.jpg");
bImg=javax.imageio.ImageIO.read(ips);
ips=
PicPane.class.getResourceAsStream("images/b.gif");
chessImg=javax.imageio.ImageIO.read(ips);
ips=
PicPane.class.getResourceAsStream("images/w.gif");
wImg=javax.imageio.ImageIO.read(ips);
width=bImg.getWidth();
height=bImg.getHeight();
addMouseListener(newboardListener());
}
@Override
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.drawImage(bImg,0,0,null);
//加上画棋子的代码
//依据board数组中的数据
for(inti=0;ifor(intj=0;jif(board[i][j]==1)
{
g.drawImage(chessImg,j*35+3,i*35+3,null);
}
elseif(board[i][j]==-1)
{
g.drawImage(wImg,j*35+3,i*35+3,null);
}
}
}
}
publicintgetHeight()
{
returnheight;
}
publicintgetWidth()
{
returnwidth;
}
classboardListenerextendsMouseAdapter//只对感兴趣的方法实现,不用借口全部实现
{
@Override
publicvoidmouseClicked(MouseEventae)
{
intx=ae.getX();
inty=ae.getY();
introw=(y-4)/35;
intcol=(x-4)/35;
play(row,col);
////判断游戏的状态,游戏是否结束的逻辑
//if(end)
//{
//return;
//}
////获得点击的坐标
//intx=e.getX();
//inty=e.getY();
//introw=y/35;
//intcol=x/35;
//if(row>=15||col>=15)
//{
//return;
//}
//if(board[row][col]==0)
//switch(side)
//{
//case1:
//{
//e.getComponent().getGraphics().drawImage(chessImg,col*35+3,row*35+3,null);
//board[row][col]=1;//记录棋子
//side=2;//改为白方
//list.add(row+","+col);
//break;
//}
//case2:
//{
//e.getComponent().getGraphics().drawImage(wImg,col*35+3,row*35+3,null);
//board[row][col]=2;
//side=1;//改为黑方
//list.add(row+","+col);
//break;//改为黑方
//}
//}
////判断输赢
//if(isWin(row,col))
//{
////如果赢了:
//end=true;
////提示用户,谁赢了
//JOptionPane.showMessageDialog(ChessBoard.this,
//(side==1?
"白方胜":
"黑方胜"),"提示",JOptionPane.INFORMATION_MESSAGE);
//}
//
}
}
}
privatevoidplay(introw,intcol)
{
if(!
myPlay){//如果不是我落子,直接返回,什么也不做
return;
}
netPlay(row,col);//使另一方的棋盘上落子
localPlay(row,col);//在本地的棋盘上落子
myPlay=false;
}
privatevoidnetPlay(introw,intcol)
{if(end){//只打开服务器,会报错的问题
return;
}
service.send(row+","+col);
}
privatevoidlocalPlay(introw,intcol)
{
if(end)
{
return;
}
if(row>=SIZE||col>=SIZE||row<0||col<0)
{
return;
}
if(board[row][col]!
=0)
{
return;
}
//保存棋子
board[row][col]=currentColor;
//保存下棋步骤
list.add(row+","+col);
//重绘棋盘
pp.repaint();
if(isWin(row,col))
{
end=true;
JOptionPane.showMessageDialog(ChessBoard.this,
((currentColor==BLACK?
"黑方":
"白方")+"胜!
"),"提示",JOptionPane.INFORMATION_MESSAGE);
}
currentColor=-currentColor;
}
privatebooleanisWin(intcurrRow,intcurrCol)
{//需要比较四个方向的棋子(是否同色)
intcolor=board[currRow][currCol];
int[][]directions={
{1,0},
{1,1},
{0,1},
{-1,1}
};
//从四个方向比较
for(inti=0;i{
//计数器
intnum=1;
//控制正反的
for(intj=-1;j<2;j+=2)
{
//比较次数
for(intk=1;k<5;k++)
{
//获得相邻的棋子的坐标
introw=currRow+j*k*directions[i][0];
intcol=currCol+j*k*directions[i][1];
//有可能行和列越界
if(row<0||row>=15||col<0||row>=15)
{
break;
}
if(board[row][col]==color)
{
num++;
if(num==5)
{
returntrue;
}
}
else
break;
}
}
}
returnfalse;
}
classNetService