欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    五子棋游戏JAVA源代码.doc

    • 资源ID:2387317       资源大小:33.50KB        全文页数:11页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    五子棋游戏JAVA源代码.doc

    本源代码实现的是一单机双人对战五子棋游戏本源代码由五个java源程序文件组成,Game.java、GameFrame.java、GamePanel.java、 ChessMan.java、Player.java 源代码下载源代码:Game.javaimport javax.swing.*;public class Game public static void main(String args) GameFrame GFrame=new GameFrame(); GFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GFrame.setVisible(true); GameFrame.javaimport java.awt.*;import javax.swing.*;class GameFrame extends JFrame private static final int Width=570; private static final int Height=470; GameFrame() setTitle("五子棋游戏"); CenteredFrame(); setSize(Width,Height); GamePanel Gpanel=new GamePanel(); add(Gpanel); void CenteredFrame() Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screenSize=kit.getScreenSize(); int screenHeight=screenSize.height; int screenWidth=screenSize.width; int Xposition=(screenWidth-Width)/2; int Yposition=(screenHeight-Height)/2; setLocation(Xposition,Yposition); GamePanel.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;class GamePanel extends JPanel private Point cursor=new Point(40,60); /棋盘坐标 private int ChessState=new int1818;/棋盘状态 private int i=0;/横坐标 private int j=0;/纵坐标 private final static int testnum=5;/五子棋的规定棋子数 private Player Black=new Player(1,Color.BLACK,"黑方");/黑方棋子 private Player White=new Player(2,Color.WHITE,"白方");/白方棋子 private Player Cplayer=null;/当前用户的引用 private JTextField textBlack=new JTextField("",5);/黑方文本提示框对象和文本长度设置 private JTextField textWhite=new JTextField("",5);/白方文本提示框对象和文本长度设置 private String Nothing="" private String textblack="请黑方下子"/黑方提示文本 private String textwhite="请白方下子"/白方提示文本 GamePanel() setLayout(null); Initialization(); setFocusable(true); JButton Rutton=new JButton("重新开局"); Rutton.setBounds(20,14,100,26); RestartListener restart=new RestartListener(); Rutton.addActionListener(restart); add(Rutton); textBlack.setHorizontalAlignment(JTextField.CENTER); textBlack.setBounds(150,14,110,26); textBlack.setEditable(false); add(textBlack); textWhite.setHorizontalAlignment(JTextField.CENTER); textWhite.setBounds(290,14,110,26); textWhite.setEditable(false); add(textWhite); JTextArea gInstruction=new JTextArea(); gInstruction.setSelectedTextColor(new Color(238,238,238); String gSInstruction= "Play1(黑方) Key nup-W ndown-S nleft-A nright-F nn"+ "Play2(白方) Key nup-up ndown-down nleft-left nright-right nn"+ "Exit game: nPress Esc" gInstruction.setText(gSInstruction); gInstruction.setEditable(false); gInstruction.setBounds(440,60,100,340); add(gInstruction); ChessManPerformListener Perform=new ChessManPerformListener(); addKeyListener(Perform); / 设置第一个JTextField输出"请黑方下棋",坐标为(40,60) void Initialization() for(int i=0,j;i<18;i+) for(j=0;j<18;j+) ChessStateij=0; cursor.setLocation(40,60); Cplayer=Black; textBlack.setText(textblack); textWhite.setText(Nothing); /画棋盘和初始化棋局状态 protected void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2=(Graphics2D) g; for(int i=60;i<=400;i+=20) g2.drawLine(40,i,380,i); for(int j=40;j<=380;j+=20) g2.drawLine(j,60,j,400); g2.drawString("_",cursor.x-6,cursor.y); g2.drawString("_",cursor.x,cursor.y); for(i=0;i<18;i+) for(j=0;j<18;j+) if(ChessStateij!=0) if(ChessStateij=1) g2.setPaint(Black.getplayerChessManColor(); if(ChessStateij=2) g2.setPaint(White.getplayerChessManColor(); g2.fillOval(j*20+40-10, i*20+60-10, ChessMan.getChessManSize(), ChessMan.getChessManSize(); /判断棋盘的当前位置是否已有棋子 boolean isChessState() this.j=(cursor.x-40)/20; this.i=(cursor.y-60)/20; if(ChessStatethis.ithis.j!=0) return true; else return false; /记录落子后棋盘的当前位置的状态 void RecordChessState() this.j=(cursor.x-40)/20; this.i=(cursor.y-60)/20; ChessStatethis.ithis.j=Cplayer.getCurrentIdentify(); /判断当前玩家落子后是否赢了 void JudgeWin() for(int i=0;i<4;i+) if(JudgeWinLine(i) /提示当前玩家已经获得胜利 try String Ginformation="GameInformation" String Message="恭喜玩家"+Cplayer.getsIdentify()+"获胜!"+"n"+"继续游戏还是退出游戏?" String options = "继续","退出" int selection=JOptionPane.showOptionDialog(null,Message,Ginformation, JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE ,null,options,options0);/throws HeadlessException if(selection=JOptionPane.OK_OPTION) Initialization(); repaint(); return; if(selection=JOptionPane.NO_OPTION) System.exit(0);/退出程序 catch(HeadlessException e) e.printStackTrace(); /如果当前方没有赢棋则双方轮换 ChangeCurrentPlayer(); /在当前方向上是否有连续的五只棋子 boolean JudgeWinLine(int direction) int i,j,di,dj,count; i=j=di=dj=count=0; switch(direction) case 0: j=this.j-(testnum-1); i=this.i; dj=1; di=0; break; case 1: j=this.j; i=this.i-(testnum-1); dj=0; di=1; break; case 2: j=this.j-(testnum-1); i=this.i+(testnum-1); dj=1; di=-1; break; case 3: j=this.j-(testnum-1); i=this.i-(testnum-1); dj=1; di=1; break; for(int k=0;k<testnum*2+1;k+) if(j>=0&&j<18&&i>=0&&i<18) if(ChessStateij=Cplayer.getCurrentIdentify() count+; if(count>=testnum) return true; else count=0; j+=dj; i+=di; return false; /更换当前玩家 void ChangeCurrentPlayer() if(Cplayer=Black) Cplayer=White; textBlack.setText(Nothing); textWhite.setText(textwhite); else Cplayer=Black; textBlack.setText(textblack); textWhite.setText(Nothing); /重新开局监听器private class RestartListener implements ActionListener public void actionPerformed(ActionEvent arg0) Initialization(); repaint(); requestFocus(); /棋盘、棋局状态监听器private class ChessManPerformListener implements KeyListener /玩家2的按键 public void keyPressed(KeyEvent event) int keyCode=event.getKeyCode(); if(keyCode=KeyEvent.VK_ESCAPE) System.exit(0); if(Cplayer.getCurrentIdentify()=2)/判别当前玩家 if(keyCode=KeyEvent.VK_LEFT) if(cursor.x>40) cursor.x-=20; else if(keyCode=KeyEvent.VK_RIGHT) if(cursor.x<380) cursor.x+=20; else if(keyCode=KeyEvent.VK_UP) if(cursor.y>60) cursor.y-=20; else if(keyCode=KeyEvent.VK_DOWN) if(cursor.y<400) cursor.y+=20; else if(keyCode=KeyEvent.VK_ENTER) if(!isChessState() Cplayer.PerformChessMan(); RecordChessState(); repaint(); JudgeWin();/判定当前落子后是否赢棋 repaint(); public void keyReleased(KeyEvent event) /玩家1的按键 public void keyTyped(KeyEvent event) char keyChar=event.getKeyChar(); if(Cplayer.getCurrentIdentify()=1)/判别当前玩家 if(keyChar='a') if(cursor.x>40)/对移动光标超界现象做判别 cursor.x-=20; else if(keyChar='d') if(cursor.x<380) cursor.x+=20; else if(keyChar='w') if(cursor.y>60) cursor.y-=20; else if(keyChar='s') if(cursor.y<400) cursor.y+=20; else if(keyChar=' ') if(!isChessState() /落子前先判断当前位置上是否已有棋子 Cplayer.PerformChessMan();/落子 RecordChessState();/记录当前落子后棋盘状态 repaint(); JudgeWin();/判定当前落子后是否赢棋 repaint(); ChessMan.javaimport java.awt.Color;class ChessMan private static final int ChessManSize=20; private Color ChessManColor; ChessMan(Color c) ChessManColor=c; static int getChessManSize() return ChessManSize; Color getChessManColor() return ChessManColor; Player.java import java.awt.Color;class Player private int identify; private ChessMan pChessMan; private String PlayerStringIdentify; Player(int identify,Color c,String sIdentify) this.identify=identify; pChessMan=new ChessMan(c); this.PlayerStringIdentify=sIdentify; int getCurrentIdentify() return identify; String getsIdentify() return PlayerStringIdentify; void PerformChessMan() Color getplayerChessManColor() return pChessMan.getChessManColor();

    注意事项

    本文(五子棋游戏JAVA源代码.doc)为本站会员(laozhun)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开