五子棋学习总结

一.首先就是五子棋界面的创建

首先建立一个包含所需要所有内容的窗体类。

public class firststep extends JFrame {
    public void intui(){
        this.setTitle("五子棋");
        this.setSize(1000,800);
        this.setDefaultCloseOperation(3);
        Mpanel mp=new Mpanel();
        this.add(mp);
        String[]name={"悔棋","重开"};
        drawlist dl = new drawlist();
        for(int i=0;i<name.length;i++){
            JButton jbu =new JButton(name[i]);
            mp.add(jbu);
            jbu.addActionListener(dl);
        }
        this.setVisible(true);
        Graphics g =getGraphics();
        mp.gameArr= dl.gameArr;
    }

    public static void main(String[] args) {
        firststep ft=new firststep();
         ft.intui();
    }
}

二棋盘的绘制:

利用画布类mpanel绘制画布使界面发生改变时画布上的元素能随时重新绘制,再绘制出棋盘格式。

public class Mpanel extends JPanel {
    public game[] gameArr;
    int x0 = 50, y0 = 50, height = 750, width = 50;

    public void paint(Graphics g) {
        super.paint(g);
        for (int i = 0; i < 15; i++) {
            g.drawLine(x0 + i * width, y0, x0 + i * width, height);
        }
        for (int i = 0; i < 15; i++) {
            g.drawLine(height, y0 + i * width, x0, y0 + i * width);
        }
        if (gameArr == null) {
            return;
        }
        for (int i = 0; i < gameArr.length; i++) {
            game game = gameArr[i];
            if (game != null) {
                game.drawgame(g);
            }

        }
    }
}

绘画方法类

先将两个监听器所继承的方法在类里全重写,再根据需要进行添加绘画方法。

public class drawlist implements MouseListener, ActionListener {
    public String name = "";
    public Color color;
    public Graphics g;
    public int x, y, count , x1, y1, chessx, chessy;
    public game[] gameArr = new game[10000];
    public void actionPerformed(ActionEvent e){
        name = e.getActionCommand();
    }
    public void mouseClicked(MouseEvent e){}


    public void mousePressed(MouseEvent e){}


    public void mouseReleased(MouseEvent e){}


    public void mouseEntered(MouseEvent e){}


    public void mouseExited(MouseEvent e){}

}

各类方法的实现:

public class DrawLis implements MouseListener, ActionListener {
    public Graphics g;
    public int x, y, count , x1, y1, chessx, chessy;
    int x0 = 100, y0 = 100, line = 15, width = 50;
    public game[] gameArr = new game[10000];
    private int chessArr[][] = new int[line][line];
    public String name = "";
    public Color color;



    public void actionPerformed(ActionEvent e) {
        name = e.getActionCommand();
        if (name.equals("开始")) {
            count=1;
        }
        if (name.equals("重开")) {
            replay();
            System.out.println("game replay");
        }
        if (name.equals("悔棋")) {
            back();
            System.out.println("huiqichenggong");
        }

    }

    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();

            if ((x - x0) % width >= width / 2) {
                x1 = ((x - x0) / width + 1) * width + x0;
                chessx = x1 / width;
                if ((y - y0) % width >= width / 2) {
                    y1 = ((y - y0) / width + 1) * width + y0;
                    chessy = y1 / width;
                } else if ((y - y0) % width < width / 2) {
                    y1 = ((y - y0) / width) * width + y0;
                    chessy = y1 / width;
                }

            } else if ((x - x0) % width < width / 2) {
                x1 = ((x - x0) / width) * width + x0;
                chessx = x1 / width;
                if ((y - y0) % width >= width / 2) {
                    y1 = ((y - y0) / width + 1) * width + y0;
                    chessy = y1 / width;
                } else if ((y - y0) % width < width) {
                    y1 = ((y - y0) / width) * width + y0;
                    chessy = y1 / width;
                }
            }

            if (count % 2 == 0) {
                color = Color.WHITE;
                g.setColor(color);
                chessArr[x1 / width][y1 / width] = 2;


            } else {
                color = Color.BLACK;
                g.setColor(color);
                chessArr[x1 / width][y1 / width] = 1;

            }
            g.fillOval(x1 - 25, y1 - 25, 50, 50);
            game game = new game(color, x1, y1);
            gameArr[count] = game;
            count++;
            if (xwin() >= 5 || ywin() >= 5 || leftuptorighetdown() >= 5 || leftdowntorighetup() >= 5) {
                if (chessArr[chessx][chessy] == 1) {
                    System.out.println("黑棋赢");
                } else if (chessArr[chessx][chessy] == 2) {
                    System.out.println("白棋赢");
                }
            }


    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }  public void drawChess(int r, int C, int count) {
        if (count % 2 == 0) {
            g.setColor(Color.black);
        } else {
            g.setColor(Color.white);
        }
        int x = x0 + r * width - width / 2;
        int y = y0 + C * width - width / 2;
        g.fillOval(x, y, width, width);
    }

    //    棋子重绘
    public void paintchsee() {
        if (chessArr == null) {
            return;
        }
        for (int i = 0; i < chessArr.length; i++) {
            for (int j = 0; j < chessArr[i].length; j++) {
                if (chessArr[i][j] != 0) {
                    drawChess(i, j, chessArr[i][j]);
                }
            }
        }

    }

    public void back() {
        if (count < 0) {
            return;
        } else {
            chessArr[count] = null;
            count--;
            paintchsee();
        }

    }
    public void replay(){
        for(int i=0;i<chessArr.length;i++){
            for(int j=0;j<chessArr[i].length;j++){
                chessArr[i][j]=0;
            }
        }
        count=0;
        paintchsee();
        count=0;
    }


    public int xwin() {
        int nun = 0;
        for (int i = chessx; i < chessArr.length; i++) {
            if (chessArr[chessx][chessy] == chessArr[i][chessy]) {
                nun++;
            } else break;
        }
        for (int i = chessx - 1; i >= 0; i--) {
            if (chessArr[chessx][chessy] == chessArr[i][chessy]) {
                nun++;
            } else break;
        }
        return nun;
    }

    public int ywin() {
        int nun = 0;
        for (int i = chessy; i < chessArr.length; i++) {
            if (chessArr[chessx][chessy] == chessArr[chessx][i]) {
                nun++;
            } else break;
        }
        for (int i = chessy - 1; i >= 0; i--) {
            if (chessArr[chessx][chessy] == chessArr[chessx][i]) {
                nun++;
            } else break;

        }
        return nun;

    }

    public int leftuptorighetdown() {
        int nun = 0;
        for (int i = chessx, j = chessy; i >= 0 && j < chessArr.length; i--, j++) {
            if (chessArr[chessx][chessy] == chessArr[i][j]) {
                nun++;
            } else break;
        }
        for (int i = chessx + 1, j = chessy - 1; i < chessArr.length && j >= 0; i++, j--) {
            if (chessArr[chessx][chessy] == chessArr[i][j]) {
                nun++;
            } else break;

        }
        return nun;
    }

    public int leftdowntorighetup() {
        int num = 0;
        for (int i = chessx, j = chessy; i >= 0 && j >= 0; i--, j--) {
            if (chessArr[chessx][chessy] == chessArr[i][j]) {
                num++;
            } else break;
        }
        for (int i = chessx + 1, j = chessy + 1; i < chessArr.length && j < chessArr.length; i++, j++) {
            if (chessArr[chessx][chessy] == chessArr[i][j]) {
                num++;
            } else break;

        }
        return num;
    }


}

悔棋

先对一维数组的下标进行判断,减一后小于0代表没有棋子,那么直接返回即可。有则从二维数组中找出最后一个下的棋子,坐标由一维数组进行查找。找到后将二维数组最后一个下的棋子初始化,删去一位数组中记录它的坐标,下标减一后进行重绘。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值