返回   cpper编程论坛 > Blog > cat
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

为这篇文章评分

做个简单的黑白棋(2)

发表于 2006-02-04 06:54 AM 作者: cat
上次主要的都有了,这次弄个简单的GUI就是了。

这里用一个简单的办法来做棋盘:Button数组。为了简单起见,我要在每个Button上增加一些功能,所以自定义了一个Button类,实现如下:

代码:
public class JMyButton extends JButton {
    private static final long serialVersionUID = -5948631467254568191L;

    public JMyButton(int x, int y) {
        this.x = x;
        this.y = y;

        if (!inited) {
             try {
                 black = new ImageIcon(ImageIO.read(new File("black.jpg")));
             } catch (IOException e) {}
             try {
                 white = new ImageIcon(ImageIO.read(new File("white.jpg")));
             } catch (IOException e) {}
             inited = true;
        }
    }

    public void setContent(int state) {
        switch (state) {
            case 0:
                this.setBackground(new Color(0, 128, 0));
                break;
            case 10:                               // 这个格子是个valid move, 高亮显示
                this.setBackground(new Color(0, 230, 0));
                break;
            case 1:
                if (black != null)
                    this.setIcon(black);
                this.setBackground(Color.BLACK);
                break;
            case -1:
                if (white != null)
                    this.setIcon(white);
                this.setBackground(Color.WHITE);
                break;
        }
        this.state = state;
    }
    public int getContent() {
        return state;
    }

    private int state;
    public int x, y;

    private static ImageIcon black;
    private static ImageIcon white;
    private static boolean inited;
}
然后弄个窗口来响应用户输入并且显示棋盘就是了(这个驱动就是上次写的那个循环)。
首先是这个驱动,作为一个线程执行:

代码:
    public void run() {
        jUndoButton.setEnabled(true);
        while (!board.isEndOfGame()) {
            ShowBoard(board);
            getPlayer(currentColor).play(board);
        }
        int val = board.getBlackScore() - board.getWhiteScore();
        String msg;
        if (val > 0)
            msg = "Black won by " + val + " stones";
        else if (val < 0)
            msg = "White won by " + -val + " stones";
        else
            msg = "Draw";
        jUndoButton.setEnabled(false);
        jStartButton.setEnabled(true);
        JOptionPane.showMessageDialog(this, msg);
    }
然后,当用户按了某个格子时:
代码:
    public void actionPerformed(ActionEvent arg0) {
        synchronized (boardGUI) {
            JMyButton cell = (JMyButton) arg0.getSource();
            if (cell.getContent() == 10) {                   // 这个个子是一个valid move
                shootMoveEvent(cell.x, cell.y);
            }
        }
    }
这个所谓的shootMoveEvent最终归结到HumanPlayer的onMove函数。记得上次的Player接口吗?上次我定义了一个VirtualPlayer, 这次定义个HumanPlayer. 这个类主关系到2个线程:
代码:
package myothello;
public class HumanPlayer implements Player {
    public HumanPlayer(int color, Othello othello) {
        this.color = color;
        othello.addMoveListener(new Othello.MoveListener() {
            public void onMove(int x, int y, int color) {
                synchronized (HumanPlayer.this) {
                    if (color != HumanPlayer.this.color) return;
                    HumanPlayer.this.x = x;
                    HumanPlayer.this.y = y;
                    HumanPlayer.this.played = true;
                    HumanPlayer.this.notifyAll();
                }
            }
        });
    }

    public void play(Board board) {
        synchronized (this) {
            while (!played) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (x != -1) board.putStone(x, y);
            else board.pass();
            played = false;
        }
    }
    public int getColor() {
        return color;
    }
    private volatile int x, y;
    private boolean played;
    private int color;
}
最后弄张效果图(从代码中可以看到,这些棋子可以美化一下的,具体见JMyButton.java,我么,偷个懒,弄个熊猫黑白棋凑合了),以及贴上最后的3个文件。

左边那个,就是这个Java的,右边那个C#的,基本一样,美化了一下界面(左边的完全可以做到,不过要作出区别来嘛~),增强了一下AI而已,下次有空把AI增强写一下 :b
上传的缩略图
点击图片以查看大图

名称:	PandaOthello.jpg
查看次数:	36
文件大小:	30.0 KB
ID:	5  点击图片以查看大图

名称:	TinyOthello.jpg
查看次数:	45
文件大小:	56.7 KB
ID:	6  
上传的附件
文件类型: java HumanPlayer.java (991 字节, 78 次查看)
文件类型: java JMyButton.java (1.5 KB, 77 次查看)
文件类型: java Othello.java (8.4 KB, 81 次查看)
评论 0 Email文章
评论总数 0

评论

发表评论 发表评论
作者为 cat 的最新文章

所有时间均为格林尼治时间 +9。现在的时间是 08:54 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0