import java.awt.*;
import java.awt.event.*;
public class OXMain extends Frame implements ActionListener {
private OX oxBoard;
private OXMain() {
super("井字游戏");
Menu m;
MenuBar mb;
oxBoard = new OX(this);
this.add(oxBoard);
CloseWindow close = new CloseWindow(this, true);
this.setMenuBar(mb = new MenuBar());
mb.add(m = new Menu("游戏")).add(new MenuItem("新游戏")).addActionListener(this);
m.add(new MenuItem("结束")).addActionListener(close);
mb.add(new Menu("说明")).add(new MenuItem("关于本游戏")).addActionListener(this);
this.addWindowListener(close);
this.pack();
this.show();
}
public static void main(String argv[]) {
new OXMain();
}
// implements the ActionListener interface
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("关于本游戏")) {
new ErrorDialog(this,"俞旭升写好玩的");
} else if (command.equals("新游戏")) {
oxBoard.newGame();
}
}
}
class OX extends Component implements MouseListener {
public static final byte EMPTY = 0;
public static final byte CIRCLE = 1;
public static final byte CROSS = 2;
private byte[] board = new byte;
private byte playing = CIRCLE;
private Dimension mySize = new Dimension(300,300);
private Frame parent;
private byte[][] directions = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
public OX(Frame p) {
this.addMouseListener(this);
parent = p;
}
// The following 5 functions implement the MouseListener interface
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
int row = e.getY()/100;
int col = e.getX()/100;
if (row >= 3 || col >= 3) return; // 超过边界
if (board == EMPTY) { // 此位置可以下
board = playing;
repaint(); // notify Window Manager
// Anyone Win?
for (int i=0; i<directions.length; i++) {
int j;
for (j=0; j<3 && board] ..
访客只能看到部份内容,免费 加入会员 或由脸书 Google 可以看到全部内容