• PUDN用户
    了解作者
  • Java
    开发工具
  • 3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 74
    下载次数
  • 2005-09-17 01:13
    上传日期
单机版五子棋,可以双人对奕,里面的算法很详尽,特别适合初学编程者看,希望大家多提意见
GobangGame.zip
  • www.pudn.com.txt
    218B
  • GobangGame.java
    10.8KB
内容介绍
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GobangGame extends JFrame implements ChessmanListener { Container contentPane; Block[] blocks = new Block[15 * 15]; JPanel chessBoard = new JPanel(); JButton cmdNewGame = new JButton(); JLabel labHeiQi = new JLabel(); JLabel labBaiQi = new JLabel(); JButton cmdReDo = new JButton(); public static void main(String [] args) { GobangGame game=new GobangGame(); } public GobangGame() { setTitle("五子棋"); contentPane = getContentPane(); contentPane.setLayout(null); contentPane.setBackground(new Color(187,159,90)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Icon bq = new ImageIcon("baiqi.gif"); Icon hq = new ImageIcon("heiqi.gif"); chessBoard.setBackground(new Color(131, 152, 0)); chessBoard.setPreferredSize(new Dimension(450, 450)); chessBoard.setBounds(new Rectangle(20, 60, 450, 450)); chessBoard.setLayout(new GridLayout(15,15)); int n; for (int i = 0; i < 15 * 15; i++) { n = Block.ALL; if (i % 15 == 0) n = n & ~Block.LINELEFT; if (i % 15 == 14) n = n & ~Block.LINERIGHT; if (i / 15 == 0) n = n & ~Block.LINETOP; if (i / 15 == 14) n = n & ~Block.LINEBOTTOM; blocks[i] = new Block(n, i / 15, i % 15); blocks[i].addChessmanListener(this); if ( (i - 1) % 4 == 3 && i / 15 % 4 == 3) blocks[i].setStar(true); chessBoard.add(blocks[i]); } labHeiQi.setFont(new java.awt.Font("Dialog", 1, 16)); labHeiQi.setIcon(hq); labHeiQi.setIconTextGap(10); labHeiQi.setText("黑棋"); labHeiQi.setBounds(new Rectangle(200, 15, 100, 40)); labBaiQi.setFont(new java.awt.Font("Dialog", 1, 16)); labBaiQi.setIcon(bq); labBaiQi.setIconTextGap(10); labBaiQi.setText("白棋"); labBaiQi.setBounds(new Rectangle(330, 15, 100, 40)); cmdNewGame.setBounds(new Rectangle(20, 20, 100, 30)); cmdNewGame.setFont(new java.awt.Font("Dialog", 0, 16)); cmdNewGame.setText("新游戏"); cmdNewGame.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (Block.getPerson() != 0) { for (int i = 0; i < 15 * 15; i++) { if(blocks[i].getHas()!=0) blocks[i].reSet(); } } labBaiQi.setText("白棋"); labHeiQi.setText("黑棋"); Block.StartGame(); } } ); Block.setPersonLabel(labHeiQi, labBaiQi); contentPane.add(cmdNewGame); contentPane.add(chessBoard); contentPane.add(labBaiQi); contentPane.add(labHeiQi); setSize(800,600); setVisible(true); } public void chessmanPress(ChessmanEvent e) { int up, down, left, right; up = down = e.getRow(); left = right = e.getCollumn(); do { up++; } while (up < 15 && blocks[up * 15 + e.getCollumn()].getHas() == e.getHas()); do { down--; } while (down >= 0 && blocks[down * 15 + e.getCollumn()].getHas() == e.getHas()); if ( (up - down) > 5) { for (int i = down + 1; i < up; i++) blocks[i * 15 + e.getCollumn()].setWinChessman(); Block.end(); } else { do { right++; } while (right < 15 && blocks[right + e.getRow() * 15].getHas() == e.getHas()); do { left--; } while (left >= 0 && blocks[left + e.getRow() * 15].getHas() == e.getHas()); if ( (right - left) > 5) { for (int i = left + 1; i < right; i++) blocks[i + e.getRow() * 15].setWinChessman(); Block.end(); } else { up = down = e.getRow(); left = right = e.getCollumn(); do { right++; down++; } while (right < 15 && down < 15 && blocks[right + down * 15].getHas() == e.getHas()); do { left--; up--; } while (left >= 0 && up >= 0 && blocks[left + up * 15].getHas() == e.getHas()); if ( (right - left) > 5) { for (int i = left + 1; i < right; i++) { up++; blocks[i + up * 15].setWinChessman(); } Block.end(); } else { up = down = e.getRow(); left = right = e.getCollumn(); do { right++; down--; } while (right < 15 && down >= 0 && blocks[right + down * 15].getHas() == e.getHas()); do { left--; up++; } while (left >= 0 && up < 15 && blocks[left + up * 15].getHas() == e.getHas()); if ( (right - left) > 5) { for (int i = left + 1; i < right; i++) { up--; blocks[i + up * 15].setWinChessman(); } Block.end(); } } } } if (Block.isEnd()) { cmdReDo.setEnabled(false); if (e.getHas() == 1) { labHeiQi.setText("黑棋胜"); labBaiQi.setVisible(false); labHeiQi.setVisible(true); } else { labBaiQi.setText("白棋胜"); labHeiQi.setVisible(false); labBaiQi.setVisible(true); } } } } class Block extends JPanel { static Block last,last2; public static final int LINELEFT = 1 << 0; //左边线 public static final int LINERIGHT = 1 << 1; //右边线 public static final int LINETOP = 1 << 2; //上边线 public static final int LINEBOTTOM = 1 << 3; //下边线 public static final int ALL = LINELEFT + LINERIGHT + LINETOP + LINEBOTTOM; public static int bWidth = 30; private static int p1Count = 0, p2Count = 0; private static JLabel labP1, labP2; private static int person = 0; private int type; //处于那个位置上 private boolean mouseIn; private boolean winChessman = false; private boolean star = false; //该点是否是星点 private static boolean start = true; //是否开始新游戏 static ImageIcon imgNormal, heiqi, baiqi, over, win, lastImg; private ChessmanEvent chessmanEvent; private ChessmanListener chessmanListener; int has = 0; int x = 0, y = 0; Block(int bType, int row, int collumn) { type = bType; this.x = row; this.y = collumn; this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); imgNormal = new ImageIcon("back.gif"); heiqi = new ImageIcon("heiqi.gif"); baiqi = new ImageIcon("baiqi.gif"); over = new ImageIcon("over.gif"); win = new ImageIcon("win.gif"); lastImg = new ImageIcon("last.gif"); this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (start && has == 0) { person= 2 - ( person + 1 ) % 2 ; has = person; if (has == 2) { p2Count++; labP1.setVisible(true); labP2.setVisible(false); } else if (has == 1) { p1Count++; labP2.setVisible(true); labP1.setVisible(false); } chessmanEvent = new ChessmanEvent(has, x, y); chessmanListener.chessmanPress(chessmanEvent); last2=last; last=Block.this; if( last2!=null ) last2.repaint(); } } } ); this.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { if(!start) Block.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); else if(has==0) Block.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); mouseIn = true; repaint(); } public void mouseExited(MouseEvent e) { mouseIn = false; if (has != 0) Block.this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); repaint(); } } ); this.setDoubleBuffered(false); this.setPreferredSize(new Dimension(bWidth, bWidth)); } //是否为星点 public boolean isStar() { return star; } //设为星点 public void setStar(boolean star) { this.star = star; } //添加监听器 public void addChessmanListener(ChessmanListener c) { chessmanListener = c; } //绘制图面 public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; int w; w = bWidth; g.drawImage(imgNormal.getImage(), 0, 0, bWidth, bWidth, this); if ( (type & LINELEFT) == 0 || (type & LINERIGHT) == 0) g2d.setStroke(new BasicStroke(2)); else g2d.setStroke(new BasicStroke(1)); //上面的线 if ( (type & LINETOP) == LINETOP) g2d.drawLine(w / 2, 0, w / 2, w / 2); //下面的线 if ( (type & LINEBOTTOM) == LINEBOTTOM) g2d.drawLine(w / 2, w / 2, w / 2, w); if ( (type & LINETOP) == 0 || (type & LINEBOTTOM) == 0) g2d.setStroke(new BasicStroke(2)); else g2d.setSt
评论
    相关推荐
    • java五子棋
      java五子棋,直接可以用,主要是利用绘图技术,布局什么都可以直接用
    • java五子棋
      开发简单的五子棋游戏,使用纯java,里面有五子棋的基本功能,其中五子棋的难度设置有点复杂弄得不好,请多包涵
    • java五子棋
      java五子棋 html+class 用网页加载applet的方式运行 如果感兴趣可以用反编一下 看源代码
    • java 五子棋
      自己写的java 五子棋代码 里面都解释的很清楚了
    • java五子棋
      初学java只是有限,用swing做的一个五子棋游戏,总共代码量有600行吧,花了两天时间才做好,应为学java才一个月,做错来的时候还有点点兴奋,下次再接再厉
    • java五子棋
      java五子棋程序,能实现人机对战,局域网联机对战功能,简单易懂。
    • java五子棋
      java五子棋源码 ------------------------------------------------------------ 五子棋java源码
    • java五子棋
      一个完整的java五子棋,具有完整的功能包括人机对战,人人对战
    • java实现五子棋
      本文件主要是运用java实现五子棋,包括界面UI设计,五元组算法等。
    • java 五子棋java 五子棋java 五子棋
      java 五子棋java 五子棋java 五子棋