五子棋

来源:互联网 发布:滑板淘宝店推荐 编辑:程序博客网 时间:2024/04/29 20:37
  1. 接口:在Java中,接口是实现可插入特性的保证。定义接口的关键字是interface,实现接口的关键字是implements,一个类可以实现多个接口,接口之间的继承支持多重继承。
  2. 接口和抽象类的异同

  3. 类/类和类/接口之间的关系

    • IS-A关系:继承/实现
    • HAS-A关系:关联/聚合(聚集)/合成
    • USE-A关系:依赖
  4. UML:统一建模语言(标准的图形化符号)
    • 类图:描述类以及类和类之间关系的图形符号。
    • 用例图:捕获需求。
    • 时序图:描述对象交互关系。
  5. 面向对象的设计原则
    • 单一职责原则(SRP):类的设计要做到高内聚,一个类只承担单一的职责(不做不该它做的事情)。
    • 开闭原则(OCP):软件系统应该接受扩展(对扩展开放),不接受修改(对修改关闭)。要符合开闭原则:抽象是关键,封装可变性。
    • 依赖倒转原则(DIP):面向接口编程。声明变量的引用类型,声明方法的参数类型,声明方法的返回类型时,尽可能使用抽象类型而不是具体类型。
    • 里氏替换原则(LSP):用子类型替换父类型没有任何问题,用父类型替换子类型通常都是不行的。
    • 接口隔离原则(ISP):接口要小而专,不能大而全。
    • 合成聚合复用原则(CARP):优先考虑用强关联关系复用代码,而不是用继承关系复用代码。
    • 迪米特法则(LoD):对象之间尽可能少的发生联系。

练习:五子棋

package com.lovoinfo;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import javax.swing.ImageIcon;import javax.swing.JDialog;@SuppressWarnings("serial")public class RenjuDialog extends JDialog {    private RenjuPanel panel = new RenjuPanel();    public RenjuDialog() {        this.setTitle("五子棋");        this.setSize(625, 645);        this.setResizable(false);        this.setLocationRelativeTo(null);        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);        this.setIconImage(new ImageIcon("renju.png").getImage());        this.add(panel);        this.addKeyListener(new KeyAdapter() {            @Override            public void keyPressed(KeyEvent e) {                if(e.getKeyCode() == KeyEvent.VK_F2) {                    panel.reset();                    repaint();                }            }        });    }    public static void main(String[] args) {        new RenjuDialog().setVisible(true);    }}
package com.lovoinfo;import java.awt.Color;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JPanel;import javax.swing.Timer;/** * 五子棋的面板 * @author jackfrued * */@SuppressWarnings("serial")public class RenjuPanel extends JPanel {    private Timer timer = null;    private Board b = new Board();      // 创建棋盘对象    private boolean isBlack = true;     // 是否黑方走棋    private boolean gameBegin = false;  // 游戏是否开始    public RenjuPanel() {        this.setSize(620, 620);        this.addMouseListener(new MouseAdapter() {  // 添加鼠标事件监听器监听鼠标按下事件            @Override            public void mousePressed(MouseEvent e) {                if(gameBegin) {                    int x = e.getX();                    int y = e.getY();                    if(x >= 20 && x <= 600 && y >= 20 && y <= 600) {                        int row = Math.round((y - 30) / 40.0f);                        int col = Math.round((x - 30) / 40.0f);                        if(b.move(row, col, isBlack)) { // 在鼠标点击位置走棋                            isBlack = !isBlack;                            repaint();                        }                    }                }            }        });        timer = new Timer(1500, new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                int row = (int) (Math.random() * 15);                int col = (int) (Math.random() * 15);                if(b.move(row, col, isBlack)) {                    isBlack = !isBlack;                    repaint();                }            }        });        timer.start();    }    @Override    public void paint(Graphics g) {        super.paint(g);        // 绘制背景        g.setColor(Color.ORANGE);        g.fillRect(0, 0, 620, 620);             // 绘制棋盘        g.setColor(Color.BLACK);        b.draw(g);    }    /**     * 重置五子棋面板     */    public void reset() {        timer.stop();        b.reset();        isBlack = true;        gameBegin = true;    }}
package com.lovoinfo;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;/** * 棋盘 * @author jackfrued * */public class Board {    private static final int CELL_SIZE = 40;    private CellState[][] board = new CellState[15][15];    private int lastRow = -1, lastCol = -1;    public Board() {        reset();    }    /**     * 走棋     * @param row 落子的行     * @param col 落子的列     * @param isBlack 是不是黑棋     * @return 如果落子成功返回true否则返回false     */    public boolean move(int row, int col, boolean isBlack) {        if(board[row][col] == CellState.Empty) {            board[row][col] = isBlack? CellState.Black : CellState.White;            lastRow = row;            lastCol = col;            return true;        }        return false;    }    /**     * 绘制棋盘和棋子     * @param g 画笔     */    public void draw(Graphics g) {        // 纵横15道        for(int i = 0; i < board.length; i++) {            g.drawLine(30, 30 + CELL_SIZE * i, 590, 30 + CELL_SIZE * i);            g.drawLine(30 + CELL_SIZE * i, 30, 30 + CELL_SIZE * i, 590);        }        g.fillOval(305, 305, 10, 10);       // 天元        Graphics2D g2d = (Graphics2D) g;        g2d.setStroke(new BasicStroke(3));  // 将画笔变粗        g.drawRect(25, 25, 570, 570);       // 外边框        // 对棋盘的行和列进行循环绘制棋子        for(int i = 0; i < board.length; i++) {            for(int j = 0; j < board[i].length; j++) {                if(board[i][j] != CellState.Empty) {    // 不是空白                    Color color = board[i][j] == CellState.Black? Color.BLACK : Color.WHITE;                    g.setColor(color);                    g.fillOval(10 + CELL_SIZE * j, 10 + CELL_SIZE * i,                             CELL_SIZE, CELL_SIZE);                }            }        }        if(lastRow != -1 && lastCol != -1) {            g.setColor(Color.RED);            g.drawLine(25 + CELL_SIZE * lastCol, 30 + CELL_SIZE * lastRow,                     35 + CELL_SIZE * lastCol, 30 + CELL_SIZE * lastRow);            g.drawLine(30 + CELL_SIZE * lastCol, 25 + CELL_SIZE * lastRow,                     30 + CELL_SIZE * lastCol, 35 + CELL_SIZE * lastRow);        }    }    /**     * 重置棋盘     */    public void reset() {        lastRow = -1;        lastCol = -1;        for(int i = 0; i < board.length; i++) {            for(int j = 0; j < board[i].length; j++) {                board[i][j] = CellState.Empty;            }        }    }}
package com.lovoinfo;public enum CellState {    Empty, Black, White}
1 0
原创粉丝点击