ChessMap.java
上传用户:sjjz88
上传日期:2013-04-10
资源大小:452k
文件大小:6k
- //杨建国:ChessMap.java
- // Play fivelink game
- // <applet code=ChessMap width=700 height=400></applet>
- import javax.swing.*;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import java.applet.*;
- public class ChessMap extends JApplet {
- private ChessBot bot; //电脑AI
- private ImageIcon map; //棋盘背景位图
- private ImageIcon blackchess; //黑子位图
- private ImageIcon whitechess; //白子位图
- private ChessPanel cp; //棋盘
- private JMenuBar menubar; //以下为下拉菜单
- private JMenu[] menu={new JMenu("Game"),new JMenu("Configure"),new JMenu("Help")};
- private JMenuItem[] menuitem1={new JMenuItem("Restart",KeyEvent.VK_R),new JMenuItem("Rollback",KeyEvent.VK_O),new JMenuItem("Exit",KeyEvent.VK_X)};
- private JMenuItem[] menuitem2={new JMenuItem("Configure...",KeyEvent.VK_C),new JMenuItem("About...",KeyEvent.VK_A),new JMenuItem("Human",KeyEvent.VK_U)};
- private int player; //人执黑子还是白子
- private boolean haveai=true; //人与人下还是人与电脑下,true与电脑下
-
- class Mouseclicked extends MouseAdapter //判断鼠标左击并通知棋盘和电脑
- {
- public void mouseClicked(MouseEvent e)
- {
- Point p1=new Point();
- Point p2=new Point();
-
- p1=cp.addChess(e.getX(),e.getY(),player);
- //play(sound);
- if(cp.isSuccess(player)==true)
- {
- cp.gameOver(player);
- if(player==cp.BLACK_ONE)
- {
- JOptionPane.showMessageDialog(null,"Game Over!Black Win!");
- }
- else if(player==cp.WHITE_ONE)
- {
- JOptionPane.showMessageDialog(null,"Game Over!White Win!");
- }
- }
- else if(haveai)
- {
- if(cp.isSuccess((-1)*player)==false)
- {
- p2=bot.play(p1.x,p1.y);
- cp.addChess(p2.x,p2.y,(-1)*player,true);
- if(cp.isSuccess((-1)*player)==true)
- {
- cp.gameOver((-1)*player);
- if(player==cp.BLACK_ONE)
- {
- JOptionPane.showMessageDialog(null,"Game Over!White Win!");
- }
- else if(player==cp.WHITE_ONE)
- {
- JOptionPane.showMessageDialog(null,"Game Over!Black Win!");
- }
- }
- }
- }
- else player*=(-1);
-
- }
-
- }
- class MouseMoved implements MouseMotionListener //调试用,获得鼠标位置
- {
- public void mouseMoved(MouseEvent e)
- {
- cp.showMousePos(e.getPoint());
- }
- public void mouseDragged(MouseEvent e)
- {}
- }
- class Menuitemclicked implements ActionListener //菜单消息处理
- {
- public void actionPerformed(ActionEvent e)
- {
- JMenuItem target = (JMenuItem)e.getSource();
- String actionCommand = target.getActionCommand();
- if(actionCommand.equals("Restart")) //重开一局
- {
- cp.reset();
- bot.restart();
- // player=cp.BLACK_ONE;
- }
- if(actionCommand.equals("Rollback")) //悔棋
- {
- cp.rollback();
- bot.rollback();
-
- }
- else if(actionCommand.equals("Exit")) //退出
- {
- System.exit(1);
- }
- else if(actionCommand.equals("Configure")) //执黑子还是执白子
- {
- Object[] options = { "Black", "White" };
- int sel = JOptionPane.showOptionDialog(
- null, "Your side:", "choose:",
- JOptionPane.DEFAULT_OPTION,
- JOptionPane.QUESTION_MESSAGE, null,
- options, options[0]);
- if(sel==1)
- {
- cp.reset();
- player=cp.WHITE_ONE;
- Point pt=new Point(bot.playfirst());
- cp.addChess(pt.x,pt.y,cp.BLACK_ONE,true);
-
- }
- else
- {
- cp.reset();
- player=cp.BLACK_ONE;
- }
-
- }
- else if(actionCommand.equals("Human")) //与人下
- {
- haveai=false;
- }
- else if(actionCommand.equals("About")) //版权与帮助
- {
- JOptionPane.showConfirmDialog(null,"fivelink v1.0nCopyright(c)2004 Yang Jianguo","About",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
- Mouseclicked mouseclicked=new Mouseclicked();
- MouseMoved mousemoved=new MouseMoved();
- Menuitemclicked menuclicked=new Menuitemclicked();
-
- public void init() //初始化与默认值
- {
- int i;
-
- map=new ImageIcon(getClass().getResource("map.gif"));
- blackchess=new ImageIcon(getClass().getResource("blackchess.gif"));
- whitechess=new ImageIcon(getClass().getResource("whitechess.gif"));
- cp=new ChessPanel(map,blackchess,whitechess);
- bot=new ChessBot(cp.N);
- menubar=new JMenuBar();
- menuitem1[0].setActionCommand("Restart");
- menuitem1[1].setActionCommand("Rollback");
- menuitem1[2].setActionCommand("Exit");
- menuitem2[0].setActionCommand("Configure");
- menuitem2[1].setActionCommand("About");
- menuitem2[2].setActionCommand("Human");
- for(i=0;i<3;i++)
- menu[0].add(menuitem1[i]);
- menu[1].add(menuitem2[0]);
- menu[1].add(menuitem2[2]);
- menu[2].add(menuitem2[1]);
- menu[0].setMnemonic(KeyEvent.VK_G);
- menu[1].setMnemonic(KeyEvent.VK_C);
- menu[2].setMnemonic(KeyEvent.VK_H);
- for(i=0;i<3;i++)
- menubar.add(menu[i]);
- Container p = getContentPane();
- setJMenuBar(menubar);
- p.add(cp);
- cp.addMouseListener(mouseclicked);
- cp.addMouseMotionListener(mousemoved);
- menuitem1[0].addActionListener(menuclicked);
- menuitem1[1].addActionListener(menuclicked);
- menuitem1[2].addActionListener(menuclicked);
- menuitem2[0].addActionListener(menuclicked);
- menuitem2[1].addActionListener(menuclicked);
- menuitem2[2].addActionListener(menuclicked);
- player=cp.BLACK_ONE;
-
- }
-
- public static void main(String[] args) {
- Console.run(new ChessMap(), 460, 500); //Console 可以使该程序既可以在控制台下运行,也可在网页中运行
- }
- } ///:~