五子棋对战实验报告.docx_第1页
五子棋对战实验报告.docx_第2页
五子棋对战实验报告.docx_第3页
五子棋对战实验报告.docx_第4页
五子棋对战实验报告.docx_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验项目 五子棋网络对战和聊天 实验日期 20160406 实验报告要求: 一、 实验目的:学习和使用socket编程,熟练软件开发2、 实验原理:使用socket进行网络通信,java作为编程语言3、 实验要求:编写五子棋程序可以实现联机网络对战,并且可以进行聊天4、 实验步骤、结果(程序+注释+截图)及分析:首先拟定编程语言与开发方案,选择java语言,考虑到java可以跨平台运行,然后决定把这个程序拆分为客户端、服务器两个部分,每个部分再分成5个小的部分实现不同功能。1、然后考虑使用java的swing包,创建ClientChessPanel类负责棋盘部分,包括判断输赢,使用数组chessesij记录棋盘上棋子的分布,对数组进行不同的赋值表示网格节点上无棋、黑棋、白棋;使用playChessHandler作为鼠标单击事件,单击事件调用Clientskt中的函数传送棋子坐标以及输赢信息。drawChess函数画棋子,drawGrids画网格,gameOver判断棋盘棋子分布,输赢情况。import javax.swing.*;import java.awt.*;import java.awt.event.*;import ChatOneToOneClient.Clientskt;class ClientChessPanel extends JPanelprivate static final long serialVersionUID = 1L;private int space=20; /网格间的距离private int grids=30; /棋盘的网格数private int radius=space/2; /棋的半径Clientskt skt;/当chessesij=0,表示网格节点(i,j)上无棋/当chessesij=1,表示网格节点(i,j)上放白棋/当chessesij=2,表示网格节点(i,j)上放黑棋private int chesses=new intgrids+1grids+1;private int currColor=1; /当前棋的颜色private MouseListener playChessHandler=new MouseAdapter() public void mouseClicked(MouseEvent e) if(skt.reMouseGo() int x=e.getX(); int y=e.getY(); /放一颗棋子 if(x=0 & y=0) if(chessesround(x)round(y)=0) chessesround(x)round(y)=currColor; repaint(); /刷新图形 skt.dataout(x:+String.valueOf(round(x); skt.dataout(y:+String.valueOf(round(y); skt.setMouseGo(false); if(gameOver(currColor) skt.dataout(g:你输了); skt.chat.clientDialog=new ClientMyDialog(skt.chat,你赢了); skt.chat.clientDialog.setVisible(true); currColor=currColor=1?2:1; /切换棋子的颜色 ; public int round(float a) /获得接近a的网格节点坐标 float f=a/space; return Math.round(f); public ClientChessPanel(int space,int grids,Clientskt skt) this.space=space; this.grids=grids; this.radius=space/2; this.skt=skt; setBackground(Color.BLUE); setSize(space*grids,space*grids); addMouseListener(playChessHandler); startChess(); public void startChess() clearGrids(); /清空棋盘 currColor=1; repaint(); /刷新图形 private void clearGrids() for(int i=0;i=grids;i+) for(int j=0;j=grids;j+) chessesij=0; /画一颗棋子 private void drawChess(Graphics g,int x,int y,int color) g.setColor(color=1?Color.GREEN:Color.BLACK); g.fillOval(x*space-radius,y*space-radius,radius*2,radius*2); /画网格 private void drawGrids(Graphics g) g.setColor(Color.DARK_GRAY); for(int i=0;i=grids;i+) g.drawLine(0,i*space,grids*space,i*space); g.drawLine(i*space,0,i*space,grids*space); /接收对方下的棋坐标 public void paintChess(int x,int y) if(x=0 & y=0) if(chessesxy=0) chessesxy=currColor; currColor=currColor=1?2:1; /切换棋子的颜色 skt.setMouseGo(false); skt.setMouseGo(true); repaint(); /刷新图形 /判断游戏是否结束 public boolean gameOver(int gameOver) int five=0;/用于判断是否有连续5个子 for(int i=0;igrids;i+)/行 for(int j=0;jgrids;j+)/列 if(chessesij=gameOver) five+; for(int k=1;k5&jgrids-4;k+)/行向比较 if(chessesij+k=gameOver) five+; if(five=5) return true; else five=1; k=5; for(int k=1;k5&igrids-4;k+)/列向比较 if(chessesi+kj=gameOver) five+; if(five=5) return true; else five=1; k=5; for(int k=1;k5&igrids-4&jgrids-4;k+)/右斜向比较 if(chessesi+kj+k=gameOver) five+; if(five=5) return true; else five=1; k=5; for(int k=1;k5&i4;k+)/左斜向比较 if(chessesi+kj-k=gameOver) five+; if(five=5) return true; else five=1; k=5; five=0; return false; public void paintComponent(Graphics g) /覆盖paintComponent()方法 super.paintComponent(g); /必须先调用父类的方法 drawGrids(g); /画网格 for(int i=0;i=grids;i+) for(int j=0;j+tftype.getText()+n);ta.setCaretPosition(ta.getText().length(); client.dataout(chat:用户端:+tftype.getText();tftype.setText();); clientBtn.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubport=Integer.parseInt(tfport.getText();client =new Clientskt(tfaddress.getText(),port,frm);client.start();contentPane.add(tftype,BorderLayout.SOUTH);chessPanel=new ClientChessPanel(20,30,client);tfaddress.setEditable(false);clientBtn.setEnabled(false);contentPane.add(chessPanel,BorderLayout.CENTER);contentPane.validate(); ); tftype.addMouseListener(ClientComponentPopupMenu.getSharedInstance(); ta.addMouseListener(ClientComponentPopupMenu.getSharedInstance(); north.add(inputIP); north.add(tfaddress); north.add(tfport); north.add(clientBtn); north.add(myName); contentPane.setLayout(new BorderLayout(); contentPane.add(north,BorderLayout.NORTH); contentPane.add(pane,BorderLayout.EAST); setSize(800, 700); setTitle(客服端); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);public static void main(String args) frm=new ChatOneToOneClient();程序运行结果:显示客户端与服务器端连接成功,在本机调试运行。可以判断输赢同时可以

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论