这只是一个非常简易的五子棋,仅仅作为初学JAVA者的编程锻炼
来张效果图:
下面的完整的源码:
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class hcj extends JFrame implements ChessmanListener
- {
- Container contentPane;
- Block[] blocks = new Block[15 * 15];
- JPanel chessBoard = new JPanel();
- JLabel labHeiQi = new JLabel();
- JLabel labBaiQi = new JLabel();
- JLabel pinJu = new JLabel();
- JButton cmdNewGame = new JButton();
- JButton exit = new JButton();
- JButton about = new JButton();
- int cout=0;
- public static void main(String[] args)
- {
- hcj game = new hcj();
- }
- public hcj()
- {
- setTitle("五子棋");
- contentPane = getContentPane();
- contentPane.setLayout(null);
- contentPane.setBackground(new Color(200, 160, 90));
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- chessBoard.setBackground(new Color(131, 200, 0));
- // 设置容器大小
- chessBoard.setPreferredSize(new Dimension(450, 450));
- // 填充在以(150,60)这个点为左上角,宽度为450,高度为450的矩形中
- chessBoard.setBounds(new Rectangle(150, 60, 450, 450));
- chessBoard.setLayout(new GridLayout(15, 15));
- int n;
- for (int i = 0; i < 15 * 15; i++)
- {
- n = Block.ALL;
- // 判断的先后顺序,确定了最后的n是属于哪条边
- if (i % 15 == 0)
- n = n & ~Block.LINELEFT; // 14
- if (i % 15 == 14)
- n = n & ~Block.LINERIGHT; // 13
- if (i / 15 == 0)
- n = n & ~Block.LINETOP; // 11
- if (i / 15 == 14) // 7
- 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.setIconTextGap(50);
- labHeiQi.setText("黑棋");
- labHeiQi.setBounds(new Rectangle(200, 15, 100, 40));
- pinJu.setFont(new java.awt.Font("Dialog", 1, 16));
- pinJu.setIconTextGap(50);
- pinJu.setText("平局");
- pinJu.setBounds(new Rectangle(270, 15, 100, 40));
- labBaiQi.setFont(new java.awt.Font("Dialog", 1, 16));
- labBaiQi.setIconTextGap(50);
- labBaiQi.setText("白棋");
- labBaiQi.setBounds(new Rectangle(330, 15, 100, 40));
- cmdNewGame.setBounds(new Rectangle(20, 20, 100, 30));
- cmdNewGame.setFont(new java.awt.Font("黑体", 0, 16));
- cmdNewGame.setText("新游戏");
- exit.setBounds(new Rectangle(20, 70, 100, 30));
- exit.setFont(new java.awt.Font("黑体", 0, 16));
- exit.setText("退出");
- about.setBounds(new Rectangle(20, 120, 100, 30));
- about.setFont(new java.awt.Font("黑体", 0, 16));
- about.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("黑棋");
- pinJu.setVisible(false);
- Block.StartGame();
- }
- });
- about.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- AboutFrm af1 = new AboutFrm();
- }
- });
- exit.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- Block.setPersonLabel(labHeiQi, labBaiQi);
- contentPane.add(cmdNewGame);
- contentPane.add(exit);
- contentPane.add(about);
- contentPane.add(chessBoard);
- contentPane.add(labBaiQi);
- contentPane.add(labHeiQi);
- contentPane.add(pinJu);
- pinJu.setVisible(false);
- setSize(800, 600);
- setResizable(false);
- setLocation(280, 70);
- 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();
- }
- }
- }
- for (int j = 0; j < 15 * 15; j++)
- {
- if (blocks[j].getHas() != 0)
- {
- cout++;
- }
- }
- if(cout==225)
- {
- pinJu.setVisible(true);
- labHeiQi.setVisible(false);
- labBaiQi.setVisible(false);
- }
- cout = 0;
- }
- if (Block.isEnd())
- {
- if (e.getHas() == 1)
- {
- labHeiQi.setText("黑棋胜");
- labBaiQi.setVisible(false);
- labHeiQi.setVisible(true);
- }
- else if(e.getHas() == 2)
- {
- labBaiQi.setText("白棋胜");
- labHeiQi.setVisible(false);
- labBaiQi.setVisible(true);
- }
- }
- }
- }
- class Block extends JPanel
- {
- static Block last, last2;
- public static final int LINELEFT = 1 << 0; // 左边线 1
- public static final int LINERIGHT = 1 << 1; // 右边线 2
- public static final int LINETOP = 1 << 2; // 上边线 4
- public static final int LINEBOTTOM = 1 << 3; // 下边线 8
- public static final int ALL = LINELEFT + LINERIGHT + LINETOP + LINEBOTTOM; // 15
- 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; // 是否开始新游戏
- 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)); // 设置鼠标性状为手型
- // 处理当鼠标进入游戏区域后点击方格的处理方法
- 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();
- }
- });
- }
- // 是否为星点
- 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);
- // 把传入的Graphics容器转换为一个可用的 Graphics2D 对象
- Graphics2D g2d = (Graphics2D) g;
- int w;
- w = bWidth;
- if ((type & LINELEFT) == 0 || (type & LINERIGHT) == 0) // 如果是左右边线
- g2d.setStroke(new BasicStroke(3)); // 创建一个笔划来描述形状的外形
- 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(3));
- else
- g2d.setStroke(new BasicStroke(1));
- // 左边的线
- if ((type & LINELEFT) == LINELEFT)
- g2d.drawLine(0, w / 2, w / 2, w / 2);
- // 右边的线
- if ((type & LINERIGHT) == LINERIGHT)
- g2d.drawLine(w / 2, w / 2, w, w / 2);
- if (star)
- g.fillRect(w / 2 - 2, w / 2 - 2, 5, 5);
- if (has == 1)
- {
- g.setColor(Color.BLACK); // 设置颜色 黑色
- g.fillOval(3, 3, bWidth - 6, bWidth - 6); // 填充
- } else if (has == 2)
- {
- g.setColor(Color.LIGHT_GRAY);
- g.fillOval(3, 3, bWidth - 6, bWidth - 6);
- } else if (start && mouseIn)
- {
- if (person == 2 || person == 0)
- {
- g.setColor(Color.BLACK);
- g.fillOval(3, 3, bWidth - 6, bWidth - 6);
- } else if (person == 1)
- {
- g.setColor(Color.LIGHT_GRAY);
- g.fillOval(3, 3, bWidth - 6, bWidth - 6);
- }
- }
- g2d.setStroke(new BasicStroke(2)); // 设置线宽
- g.setColor(Color.RED); // 设置颜色
- // 判断是否结束比赛
- if (winChessman)
- {
- g.drawOval(9, 9, bWidth - 17, bWidth - 17);
- } else if (last == Block.this)
- {
- g.drawLine(bWidth / 2 - 5, bWidth / 2, bWidth / 2 + 5, bWidth / 2);
- g.drawLine(bWidth / 2, bWidth / 2 - 5, bWidth / 2, bWidth / 2 + 5);
- }
- }
- // 返回该格的棋子,黑为1,白为2
- public int getHas()
- {
- return has;
- }
- // 返回当前所下的棋子,黑为1,白为2
- static public int getPerson()
- {
- return person;
- }
- // 标记当前格子为胜棋,重画
- public void setWinChessman()
- {
- winChessman = true;
- this.repaint();
- }
- // 重设当前格为空,重画
- public void reSet()
- {
- has = 0;
- winChessman = false;
- repaint();
- }
- // 开始游戏
- static public void StartGame()
- {
- last = null;
- p1Count = p2Count = 0;
- person = 0;
- start = true;
- labP1.setVisible(true);
- labP2.setVisible(false);
- }
- // 结束游戏
- static public void end()
- {
- start = false;
- }
- // 是否结束
- static public boolean isEnd()
- {
- return !start;
- }
- static public void setPersonLabel(JLabel p1, JLabel p2)
- {
- labP1 = p1;
- labP2 = p2;
- // 此刻如果没有放置棋子或者放下的为白色,则显示labp1,否则为labp2
- if (person == 0 || person == 2)
- {
- labP1.setVisible(true);
- labP2.setVisible(false);
- } else
- {
- labP2.setVisible(true);
- labP1.setVisible(false);
- }
- }
- }
- // 实现关于窗体类
- class AboutFrm implements ActionListener
- {
- static JFrame aboutfrm = new JFrame("关于");
- public AboutFrm()
- {
- JLabel j1 = new JLabel(" 简单的五子棋游戏");
- JTextArea ta1 = new JTextArea("设计者:胡成健");
- ta1.setEnabled(false);
- JTextArea ta2 = new JTextArea("院系:集美大学机械工程学院");
- ta2.setEnabled(false);
- JTextArea ta3 = new JTextArea("版权所有 Copyright(C) 2012");
- ta3.setEnabled(false);
- JButton mbtn = new JButton("谢谢");
- JPanel mp = new JPanel();
- JPanel mp2 = new JPanel();
- aboutfrm.getContentPane().setLayout(new BorderLayout());
- mp.setLayout(new GridLayout(4, 1));
- mp.add(j1);
- mp.add(ta1);
- mp.add(ta2);
- mp.add(ta3);
- mp2.add(mbtn);
- Container c = aboutfrm.getContentPane();
- c.add(mp, "North");
- c.add(mp2, "South");
- mbtn.addActionListener(this);
- aboutfrm.setSize(220, 150);
- aboutfrm.setLocation(500, 250);
- aboutfrm.setResizable(false);
- aboutfrm.show();
- }
- public void actionPerformed(ActionEvent e)
- {
- AboutFrm.aboutfrm.dispose();
- }
- }
- // 棋子事件监听接口
- interface ChessmanListener
- {
- public void chessmanPress(ChessmanEvent e);
- }
- // 棋子事件
- class ChessmanEvent
- {
- int has;
- int row, collumn;
- // 构造函数
- public ChessmanEvent(int has, int row, int collumn)
- {
- this.has = has;
- this.row = row;
- this.collumn = collumn;
- }
- // 返回该格棋子,黑为1,白为2
- public int getHas()
- {
- return has;
- }
- // 返回该格所在的行
- public int getRow()
- {
- return row;
- }
- // 返回该格所在的列
- public int getCollumn()
- {
- return collumn;
- }
- }