ChessMap.java
上传用户:sjjz88
上传日期:2013-04-10
资源大小:452k
文件大小:6k
源码类别:

游戏

开发平台:

Java

  1. //杨建国:ChessMap.java
  2. // Play fivelink game
  3. // <applet code=ChessMap width=700 height=400></applet>
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.net.*;
  9. import java.applet.*;
  10. public class ChessMap extends JApplet {
  11. private ChessBot bot; //电脑AI
  12. private ImageIcon map; //棋盘背景位图
  13. private ImageIcon blackchess; //黑子位图
  14. private ImageIcon whitechess; //白子位图
  15. private ChessPanel cp; //棋盘
  16. private JMenuBar menubar; //以下为下拉菜单
  17. private JMenu[] menu={new JMenu("Game"),new JMenu("Configure"),new JMenu("Help")};
  18. private JMenuItem[] menuitem1={new JMenuItem("Restart",KeyEvent.VK_R),new JMenuItem("Rollback",KeyEvent.VK_O),new JMenuItem("Exit",KeyEvent.VK_X)};
  19. private JMenuItem[] menuitem2={new JMenuItem("Configure...",KeyEvent.VK_C),new JMenuItem("About...",KeyEvent.VK_A),new JMenuItem("Human",KeyEvent.VK_U)};
  20. private int player; //人执黑子还是白子
  21. private boolean haveai=true; //人与人下还是人与电脑下,true与电脑下
  22. class Mouseclicked extends MouseAdapter //判断鼠标左击并通知棋盘和电脑
  23. {
  24. public void mouseClicked(MouseEvent e)
  25. {
  26.      Point p1=new Point();
  27.      Point p2=new Point();
  28.     
  29.      p1=cp.addChess(e.getX(),e.getY(),player);
  30.      //play(sound);
  31.      if(cp.isSuccess(player)==true)
  32.      {
  33.      cp.gameOver(player);
  34.      if(player==cp.BLACK_ONE)
  35.      {
  36.      JOptionPane.showMessageDialog(null,"Game Over!Black Win!");
  37.      }
  38.      else if(player==cp.WHITE_ONE)
  39.      {
  40.      JOptionPane.showMessageDialog(null,"Game Over!White Win!");
  41.      }
  42.      }
  43.      else if(haveai)
  44.      { 
  45.      if(cp.isSuccess((-1)*player)==false)
  46.      {
  47.      p2=bot.play(p1.x,p1.y);
  48.      cp.addChess(p2.x,p2.y,(-1)*player,true);
  49.      if(cp.isSuccess((-1)*player)==true)
  50.      {
  51.      cp.gameOver((-1)*player);
  52.      if(player==cp.BLACK_ONE)
  53.      {
  54.      JOptionPane.showMessageDialog(null,"Game Over!White Win!");
  55.      }
  56.      else if(player==cp.WHITE_ONE)
  57.      {
  58.      JOptionPane.showMessageDialog(null,"Game Over!Black Win!");
  59.      }
  60.      }
  61.      }
  62.      }
  63.      else player*=(-1);
  64.     
  65.      } 
  66.     
  67. }
  68. class MouseMoved implements MouseMotionListener //调试用,获得鼠标位置
  69. {
  70. public void mouseMoved(MouseEvent e)
  71.      {
  72.      cp.showMousePos(e.getPoint());
  73.      }
  74.      public void mouseDragged(MouseEvent e)
  75.      {}
  76. }
  77. class Menuitemclicked implements ActionListener //菜单消息处理
  78. {
  79. public void actionPerformed(ActionEvent e) 
  80. {
  81.        JMenuItem target = (JMenuItem)e.getSource();
  82.        String actionCommand = target.getActionCommand();
  83.        if(actionCommand.equals("Restart"))  //重开一局
  84.        {
  85.          cp.reset();
  86.          bot.restart();
  87.          // player=cp.BLACK_ONE;
  88.        }
  89.        if(actionCommand.equals("Rollback"))  //悔棋
  90.        {
  91.          cp.rollback();
  92.          bot.rollback();
  93.         
  94.        }
  95.        else if(actionCommand.equals("Exit"))  //退出
  96.        {
  97.          System.exit(1);
  98.        }
  99.        else if(actionCommand.equals("Configure")) //执黑子还是执白子
  100.        {
  101.          Object[] options = { "Black", "White" };
  102.          int sel = JOptionPane.showOptionDialog(
  103.            null, "Your side:", "choose:",
  104.            JOptionPane.DEFAULT_OPTION,
  105.            JOptionPane.QUESTION_MESSAGE, null,
  106.            options, options[0]);
  107.            if(sel==1)
  108.            {
  109.            cp.reset();
  110.            player=cp.WHITE_ONE;
  111.            Point pt=new Point(bot.playfirst());
  112.            cp.addChess(pt.x,pt.y,cp.BLACK_ONE,true);
  113.             
  114.            }
  115.            else
  116.            {
  117.            cp.reset();
  118.            player=cp.BLACK_ONE;
  119.            }
  120.           
  121.            }
  122.            else if(actionCommand.equals("Human"))  //与人下
  123.        {
  124.          haveai=false;
  125.        }
  126.        else if(actionCommand.equals("About"))  //版权与帮助
  127.        {
  128.          JOptionPane.showConfirmDialog(null,"fivelink v1.0nCopyright(c)2004 Yang Jianguo","About",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);
  129.        }
  130.      }
  131. }
  132. Mouseclicked mouseclicked=new Mouseclicked();
  133. MouseMoved mousemoved=new MouseMoved();
  134. Menuitemclicked menuclicked=new Menuitemclicked();
  135.   public void init() //初始化与默认值
  136.   {
  137. int i;
  138. map=new ImageIcon(getClass().getResource("map.gif"));
  139. blackchess=new ImageIcon(getClass().getResource("blackchess.gif"));
  140. whitechess=new ImageIcon(getClass().getResource("whitechess.gif"));
  141. cp=new ChessPanel(map,blackchess,whitechess);
  142. bot=new ChessBot(cp.N);
  143. menubar=new JMenuBar();
  144. menuitem1[0].setActionCommand("Restart");
  145. menuitem1[1].setActionCommand("Rollback");
  146. menuitem1[2].setActionCommand("Exit");
  147. menuitem2[0].setActionCommand("Configure");
  148. menuitem2[1].setActionCommand("About");
  149. menuitem2[2].setActionCommand("Human");
  150. for(i=0;i<3;i++)
  151. menu[0].add(menuitem1[i]);
  152. menu[1].add(menuitem2[0]);
  153. menu[1].add(menuitem2[2]);
  154. menu[2].add(menuitem2[1]);
  155. menu[0].setMnemonic(KeyEvent.VK_G);
  156. menu[1].setMnemonic(KeyEvent.VK_C);
  157. menu[2].setMnemonic(KeyEvent.VK_H);
  158. for(i=0;i<3;i++)
  159. menubar.add(menu[i]);
  160. Container p = getContentPane();
  161. setJMenuBar(menubar);
  162. p.add(cp);
  163. cp.addMouseListener(mouseclicked);
  164. cp.addMouseMotionListener(mousemoved);
  165. menuitem1[0].addActionListener(menuclicked);
  166. menuitem1[1].addActionListener(menuclicked);
  167. menuitem1[2].addActionListener(menuclicked);
  168. menuitem2[0].addActionListener(menuclicked);
  169. menuitem2[1].addActionListener(menuclicked);
  170. menuitem2[2].addActionListener(menuclicked);
  171. player=cp.BLACK_ONE;
  172. }
  173.   
  174.   public static void main(String[] args) {
  175.     Console.run(new ChessMap(), 460, 500); //Console 可以使该程序既可以在控制台下运行,也可在网页中运行
  176.   }
  177. } ///:~