做个简单的黑白棋(2)
发表于 2006-02-04 06:54 AM 作者: cat
上次主要的都有了,这次弄个简单的GUI就是了。
这里用一个简单的办法来做棋盘:Button数组。为了简单起见,我要在每个Button上增加一些功能,所以自定义了一个Button类,实现如下:
然后弄个窗口来响应用户输入并且显示棋盘就是了(这个驱动就是上次写的那个循环)。
首先是这个驱动,作为一个线程执行:
然后,当用户按了某个格子时:
这个所谓的shootMoveEvent最终归结到HumanPlayer的onMove函数。记得上次的Player接口吗?上次我定义了一个VirtualPlayer, 这次定义个HumanPlayer. 这个类主关系到2个线程:
最后弄张效果图(从代码中可以看到,这些棋子可以美化一下的,具体见JMyButton.java,我么,偷个懒,弄个熊猫黑白棋凑合了),以及贴上最后的3个文件。
左边那个,就是这个Java的,右边那个C#的,基本一样,美化了一下界面(左边的完全可以做到,不过要作出区别来嘛~),增强了一下AI而已,下次有空把AI增强写一下 :b
这里用一个简单的办法来做棋盘: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);
}
}
} 代码:
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;
} 左边那个,就是这个Java的,右边那个C#的,基本一样,美化了一下界面(左边的完全可以做到,不过要作出区别来嘛~),增强了一下AI而已,下次有空把AI增强写一下 :b
评论总数 0
评论
发表评论 |
作者为 cat 的最新文章
- C# events VS delegates (2006-11-17)
- .net 匿名delegate 可以修改的局部变量 (2006-11-17)
- 做个简单的黑白棋(2) (2006-02-04)
- 做个简单的黑白棋(1) (2006-02-02)
- Stored Procedure (2006-01-28)




