五子棋源玛

来源:互联网 发布:时时彩胆码预测软件 编辑:程序博客网 时间:2024/06/08 04:05
五子棋源玛
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import javax.swing.ButtonGroup;
  12. import javax.swing.JFrame;
  13. import javax.swing.JMenu;
  14. import javax.swing.JMenuBar;
  15. import javax.swing.JMenuItem;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JRadioButtonMenuItem;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.UIManager;
  21. /*
  22.  *main方法创建了ChessFrame类的一个实例对象(cf),
  23.  *并启动屏幕显示显示该实例对象。
  24.  **/
  25. public class FiveChessAppletDemo {
  26.     public static void main(String args[]){
  27.         ChessFrame cf = new ChessFrame();
  28.         cf.setVisible(true);
  29.     }
  30. }
  31. /*
  32.  *类ChessFrame主要功能是创建五子棋游戏主窗体和菜单
  33.  **/
  34. class ChessFrame extends JFrame implements ActionListener {
  35.     /**
  36.      * 
  37.      */
  38.     private static final long serialVersionUID = 2183726320279905885L;
  39.     private String[] strsize={"20x15","30x20","40x30"};
  40.     private String[] strmode={"人机对弈","人人对弈"};
  41.     public static boolean iscomputer=true;
  42.     public static boolean checkcomputer=true;
  43.     private int width,height;
  44.     private ChessModel cm;
  45.     private MainPanel mp;
  46.     
  47.     //构造五子棋游戏的主窗体
  48.     public ChessFrame() {
  49.         this.setTitle("五子棋游戏");
  50.         cm=new ChessModel(1);
  51.         mp=new MainPanel(cm);
  52.         Container con=this.getContentPane();
  53.         con.add(mp,"Center");
  54.         this.setResizable(false);
  55.         this.addWindowListener(new ChessWindowEvent());
  56.         MapSize(20,15);
  57.         JMenuBar mbar = new JMenuBar();
  58.         this.setJMenuBar(mbar);
  59.         JMenu gameMenu = new JMenu("游戏");
  60.         mbar.add(makeMenu(gameMenu, new Object[] {
  61.             "开局""棋盘","模式"null"退出"
  62.             }, this));
  63.         JMenu lookMenu =new JMenu("视图");
  64.         mbar.add(makeMenu(lookMenu,new Object[] {
  65.             "Metal","Motif","Windows"
  66.             },this));
  67.         JMenu helpMenu = new JMenu("帮助");
  68.         mbar.add(makeMenu(helpMenu, new Object[] {
  69.             "关于"
  70.         }, this));
  71.     }
  72.     //构造五子棋游戏的主菜单
  73.     public  JMenu makeMenu(Object parent, Object items[], Object target){
  74.         JMenu m = null;
  75.         if(parent instanceof JMenu)
  76.             m = (JMenu)parent;
  77.         else if(parent instanceof String)
  78.             m = new JMenu((String)parent);
  79.         else
  80.             return null;
  81.         for(int i = 0; i < items.length; i++)
  82.             if(items[i] == null)
  83.                 m.addSeparator();
  84.             else if(items[i] == "棋盘"){
  85.                 JMenu jm = new JMenu("棋盘");
  86.                 ButtonGroup group=new ButtonGroup();
  87.                 JRadioButtonMenuItem rmenu;
  88.                 for (int j=0;j<strsize.length;j++){
  89.                     rmenu=makeRadioButtonMenuItem(strsize[j],target);
  90.                     if (j==0)
  91.                         rmenu.setSelected(true);
  92.                     jm.add(rmenu);
  93.                     group.add(rmenu);
  94.                 }
  95.                 m.add(jm);
  96.             }else if(items[i] == "模式"){
  97.                 JMenu jm = new JMenu("模式");
  98.                 ButtonGroup group=new ButtonGroup();
  99.                 JRadioButtonMenuItem rmenu;
  100.                 for (int h=0;h<strmode.length;h++){
  101.                     rmenu=makeRadioButtonMenuItem(strmode[h],target);
  102.                     if(h==0)
  103.                         rmenu.setSelected(true);
  104.                     jm.add(rmenu);
  105.                     group.add(rmenu);
  106.                 }
  107.                 m.add(jm);
  108.             }else
  109.                 m.add(makeMenuItem(items[i], target));
  110.         return m;
  111.     }
  112.     
  113.     //构造五子棋游戏的菜单项
  114.     public  JMenuItem makeMenuItem(Object item, Object target){
  115.         JMenuItem r = null;
  116.         if(item instanceof String)
  117.             r = new JMenuItem((String)item);
  118.         else if(item instanceof JMenuItem)
  119.             r = (JMenuItem)item;
  120.         else
  121.             return null;
  122.         if(target instanceof ActionListener)
  123.             r.addActionListener((ActionListener)target);
  124.         return r;
  125.     }
  126.     
  127.     //构造五子棋游戏的单选按钮式菜单项
  128.     public  JRadioButtonMenuItem makeRadioButtonMenuItem(
  129.         Object item, Object target){
  130.         JRadioButtonMenuItem r = null;
  131.         if(item instanceof String)
  132.             r = new JRadioButtonMenuItem((String)item);
  133.         else if(item instanceof JRadioButtonMenuItem)
  134.             r = (JRadioButtonMenuItem)item;
  135.         else
  136.             return null;
  137.         if(target instanceof ActionListener)
  138.             r.addActionListener((ActionListener)target);
  139.         return r;
  140.     }
  141.     
  142.     public void MapSize(int w,int h){
  143.         setSize(w * 20+50 , h * 20+100 );
  144.         if(!ChessFrame.checkcomputer) {
  145.             ChessFrame.iscomputer=false;
  146.         } else {
  147.             ChessFrame.iscomputer=true;
  148.         }
  149.         mp.setModel(cm);
  150.         mp.repaint();
  151.     }
  152.     
  153.     public boolean getiscomputer(){
  154.         return ChessFrame.iscomputer;
  155.     }
  156.     
  157.     public void restart(){
  158.         int modeChess = cm.getModeChess();
  159.         if(modeChess <= 3 && modeChess >= 1){
  160.             cm = new ChessModel(modeChess);
  161.             MapSize(cm.getWidth(),cm.getHeight());
  162.         }else{
  163.             System.out.println("/u81EA/u5B9A/u4E49");
  164.         }
  165.     }
  166.     
  167.     public void actionPerformed(ActionEvent e){
  168.         String arg=e.getActionCommand();
  169.         try{
  170.             if (arg.equals("Windows"))
  171.                 UIManager.setLookAndFeel(
  172.                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  173.             else if(arg.equals("Motif"))
  174.                 UIManager.setLookAndFeel(
  175.                     "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  176.             else
  177.                 UIManager.setLookAndFeel(
  178.                     "javax.swing.plaf.metal.MetalLookAndFeel" );
  179.             SwingUtilities.updateComponentTreeUI(this);
  180.         }catch(Exception ee){}
  181.         if(arg.equals("20x15")){
  182.             this.width=20;
  183.             this.height=15;
  184.             cm=new ChessModel(1);
  185.             MapSize(this.width,this.height);
  186.             SwingUtilities.updateComponentTreeUI(this);
  187.         }
  188.         if(arg.equals("30x20")){
  189.             this.width=30;
  190.             this.height=20;
  191.             cm=new ChessModel(2);
  192.             MapSize(this.width,this.height);
  193.             SwingUtilities.updateComponentTreeUI(this);
  194.         }
  195.         if(arg.equals("40x30")){
  196.             this.width=40;
  197.             this.height=30;
  198.             cm=new ChessModel(3);
  199.             MapSize(this.width,this.height);
  200.             SwingUtilities.updateComponentTreeUI(this);
  201.         }
  202.         if(arg.equals("人机对弈")){
  203.             this.checkcomputer=true;
  204.             this.iscomputer=true;
  205.             cm=new ChessModel(cm.getModeChess());
  206.             MapSize(cm.getWidth(),cm.getHeight());
  207.             SwingUtilities.updateComponentTreeUI(this);
  208.         }
  209.         if(arg.equals("人人对弈")){
  210.             this.checkcomputer=false;
  211.             this.iscomputer=false;
  212.             cm=new ChessModel(cm.getModeChess());
  213.             MapSize(cm.getWidth(),cm.getHeight());
  214.             SwingUtilities.updateComponentTreeUI(this);
  215.         }
  216.         if(arg.equals("开局")){
  217.             restart();
  218.         }
  219.         if(arg.equals("关于"))
  220.             JOptionPane.showMessageDialog(this"五子棋游戏测试版本""关于"0);
  221.         if(arg.equals("退出"))
  222.             System.exit(0);
  223.     }
  224. }
  225. /*
  226.  *类ChessModel实现了整个五子棋程序算法的核心
  227.  */
  228. class ChessModel {
  229.     //棋盘的宽度、高度、棋盘的模式(如20×15)
  230.     private int width,height,modeChess;
  231.     //棋盘方格的横向、纵向坐标
  232.     private int x=0,y=0;
  233.     //棋盘方格的横向、纵向坐标所对应的棋子颜色,
  234.     //数组arrMapShow只有3个值:1,2,3,-5,
  235.     //其中1代表该棋盘方格上下的棋子为黑子,
  236.     //2代表该棋盘方格上下的棋子为白子,
  237.     //3代表为该棋盘方格上没有棋子,
  238.     //-5代表该棋盘方格不能够下棋子
  239.     private int[][] arrMapShow;
  240.     //交换棋手的标识,棋盘方格上是否有棋子的标识符
  241.     private boolean isOdd,isExist;
  242.     public ChessModel() {}
  243.     
  244.     //该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘
  245.     public ChessModel(int modeChess){
  246.         this.isOdd=true;
  247.         if(modeChess == 1){
  248.             PanelInit(2015, modeChess);
  249.         }
  250.         if(modeChess == 2){
  251.             PanelInit(3020, modeChess);
  252.         }
  253.         if(modeChess == 3){
  254.             PanelInit(4030, modeChess);
  255.         }
  256.     }
  257.     
  258.     //按照棋盘模式构建棋盘大小
  259.     private void PanelInit(int width, int height, int modeChess){
  260.         this.width = width;
  261.         this.height = height;
  262.         this.modeChess = modeChess;
  263.         arrMapShow = new int[width+1][height+1];
  264.         for(int i = 0; i <= width; i++){
  265.             for(int j = 0; j <= height; j++){
  266.                 arrMapShow[i][j] = -5;
  267.             }
  268.         }
  269.     }
  270.     
  271.     //获取是否交换棋手的标识符
  272.     public boolean getisOdd(){
  273.         return this.isOdd;
  274.     }
  275.     //设置交换棋手的标识符
  276.     public void setisOdd(boolean isodd){
  277.         if(isodd)
  278.             this.isOdd=true;
  279.         else
  280.             this.isOdd=false;
  281.     }
  282.     
  283.     //获取某棋盘方格是否有棋子的标识值
  284.     public boolean getisExist(){
  285.         return this.isExist;
  286.     }
  287.     
  288.     //获取棋盘宽度
  289.     public int getWidth(){
  290.         return this.width;
  291.     }
  292.     
  293.     //获取棋盘高度
  294.     public int getHeight(){
  295.         return this.height;
  296.     }
  297.     
  298.     //获取棋盘模式
  299.     public int getModeChess(){
  300.         return this.modeChess;
  301.     }
  302.     //获取棋盘方格上棋子的信息
  303.     public int[][] getarrMapShow(){
  304.         return arrMapShow;
  305.     }
  306.     //判断下子的横向、纵向坐标是否越界
  307.     private boolean badxy(int x, int y){
  308.         if(x >= width+20 || x < 0)
  309.             return true;
  310.         return y >= height+20 || y < 0;
  311.     }
  312.     //计算棋盘上某一方格上八个方向棋子的最大值,
  313.     //这八个方向分别是:左、右、上、下、左上、左下、右上、右下
  314.     public boolean chessExist(int i,int j){
  315.         if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)
  316.             return true;
  317.         return false;
  318.     }
  319.     //判断该坐标位置是否可下棋子
  320.     public void readyplay(int x,int y){
  321.         if(badxy(x,y))
  322.             return;
  323.         if (chessExist(x,y))
  324.             return;
  325.         this.arrMapShow[x][y]=3;
  326.     }
  327.     //在该坐标位置下棋子
  328.     public void play(int x,int y){
  329.         if(badxy(x,y))
  330.             return;
  331.         if(chessExist(x,y)){
  332.             this.isExist=true;
  333.             return;
  334.         }else
  335.             this.isExist=false;
  336.         if(getisOdd()){
  337.             setisOdd(false);
  338.         this.arrMapShow[x][y]=1;
  339.         }else{
  340.             setisOdd(true);
  341.             this.arrMapShow[x][y]=2;
  342.         }
  343.     }
  344.     //计算机走棋
  345.     /*
  346.      *说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,
  347.      *最后得出棋子数最大值的坐标,下子
  348.      **/
  349.     public void computerDo(int width,int height){
  350.         int max_black,max_white,max_temp,max=0;
  351.         setisOdd(true);
  352.         System.out.println("计算机走棋 ...");
  353.         for(int i = 0; i <= width; i++){
  354.             for(int j = 0; j <= height; j++){
  355.                 if(!chessExist(i,j)){//算法判断是否下子
  356.                     max_white=checkMax(i,j,2);//判断白子的最大值
  357.                     max_black=checkMax(i,j,1);//判断黑子的最大值
  358.                     max_temp=Math.max(max_white,max_black);
  359.                     if(max_temp>max){
  360.                         max=max_temp;
  361.                         this.x=i;
  362.                         this.y=j;
  363.                     }
  364.                 }
  365.             }
  366.         }
  367.         setX(this.x);
  368.         setY(this.y);
  369.         this.arrMapShow[this.x][this.y]=2;
  370.     }
  371.     
  372.     //记录电脑下子后的横向坐标
  373.     public void setX(int x){
  374.         this.x=x;
  375.     }
  376.     
  377.     //记录电脑下子后的纵向坐标
  378.     public void setY(int y){
  379.         this.y=y;
  380.     }
  381.     
  382.     //获取电脑下子的横向坐标
  383.     public int getX(){
  384.         return this.x;
  385.     }
  386.     
  387.     //获取电脑下子的纵向坐标
  388.     public int getY(){
  389.         return this.y;
  390.     }
  391.     //计算棋盘上某一方格上八个方向棋子的最大值,
  392.     //这八个方向分别是:左、右、上、下、左上、左下、右上、右下
  393.     public int checkMax(int x, int y,int black_or_white){
  394.         int num=0,max_num,max_temp=0;
  395.         int x_temp=x,y_temp=y;
  396.         int x_temp1=x_temp,y_temp1=y_temp;
  397.         //judge right
  398.         for(int i=1;i<5;i++){
  399.             x_temp1+=1;
  400.             if(x_temp1>this.width)
  401.                 break;
  402.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  403.                 num++;
  404.             else
  405.                 break;
  406.         }
  407.         //judge left
  408.         x_temp1=x_temp;
  409.         for(int i=1;i<5;i++){
  410.             x_temp1-=1;
  411.             if(x_temp1<0)
  412.                 break;
  413.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  414.                 num++;
  415.             else
  416.                 break;
  417.         }
  418.         if(num<5)
  419.             max_temp=num;
  420.         //judge up
  421.         x_temp1=x_temp;
  422.         y_temp1=y_temp;
  423.         num=0;
  424.         for(int i=1;i<5;i++){
  425.             y_temp1-=1;
  426.             if(y_temp1<0)
  427.                 break;
  428.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  429.                 num++;
  430.             else
  431.                 break;
  432.         }
  433.         //judge down
  434.         y_temp1=y_temp;
  435.         for(int i=1;i<5;i++){
  436.             y_temp1+=1;
  437.             if(y_temp1>this.height)
  438.                 break;
  439.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  440.                 num++;
  441.             else
  442.                 break;
  443.         }
  444.         if(num>max_temp&&num<5)
  445.             max_temp=num;
  446.         //judge left_up
  447.         x_temp1=x_temp;
  448.         y_temp1=y_temp;
  449.         num=0;
  450.         for(int i=1;i<5;i++){
  451.             x_temp1-=1;
  452.             y_temp1-=1;
  453.             if(y_temp1<0 || x_temp1<0)
  454.                 break;
  455.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  456.                 num++;
  457.             else
  458.                 break;
  459.         }
  460.         //judge right_down
  461.         x_temp1=x_temp;
  462.         y_temp1=y_temp;
  463.         for(int i=1;i<5;i++){
  464.             x_temp1+=1;
  465.             y_temp1+=1;
  466.             if(y_temp1>this.height || x_temp1>this.width)
  467.                 break;
  468.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  469.                 num++;
  470.             else
  471.                 break;
  472.         }
  473.         if(num>max_temp&&num<5)
  474.             max_temp=num;
  475.         //judge right_up
  476.         x_temp1=x_temp;
  477.         y_temp1=y_temp;
  478.         num=0;
  479.         for(int i=1;i<5;i++){
  480.             x_temp1+=1;
  481.             y_temp1-=1;
  482.             if(y_temp1<0 || x_temp1>this.width)
  483.                 break;
  484.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  485.                 num++;
  486.             else
  487.                 break;
  488.         }
  489.         //judge left_down
  490.         x_temp1=x_temp;
  491.         y_temp1=y_temp;
  492.         for(int i=1;i<5;i++){
  493.             x_temp1-=1;
  494.             y_temp1+=1;
  495.             if(y_temp1>this.height || x_temp1<0)
  496.                 break;
  497.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  498.                 num++;
  499.             else
  500.                 break;
  501.         }
  502.         if(num>max_temp&&num<5)
  503.             max_temp=num;
  504.         max_num=max_temp;
  505.         return max_num;
  506.     }
  507.     //判断胜负
  508.     public boolean judgeSuccess(int x,int y,boolean isodd){
  509.         int num=1;
  510.         int arrvalue;
  511.         int x_temp=x,y_temp=y;
  512.         if(isodd)
  513.             arrvalue=2;
  514.         else
  515.             arrvalue=1;
  516.         int x_temp1=x_temp,y_temp1=y_temp;
  517.         //判断右边
  518.         for(int i=1;i<6;i++){
  519.             x_temp1+=1;
  520.             if(x_temp1>this.width)
  521.                 break;
  522.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  523.                 num++;
  524.             else
  525.                 break;
  526.         }
  527.         //判断左边
  528.         x_temp1=x_temp;
  529.         for(int i=1;i<6;i++){
  530.             x_temp1-=1;
  531.             if(x_temp1<0)
  532.                 break;
  533.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  534.                 num++;
  535.             else
  536.                 break;
  537.         }
  538.         if(num==5)
  539.             return true;
  540.         //判断上方
  541.         x_temp1=x_temp;
  542.         y_temp1=y_temp;
  543.         num=1;
  544.         for(int i=1;i<6;i++){
  545.             y_temp1-=1;
  546.             if(y_temp1<0)
  547.                 break;
  548.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  549.                 num++;
  550.             else
  551.                 break;
  552.         }
  553.         //判断下方
  554.         y_temp1=y_temp;
  555.         for(int i=1;i<6;i++){
  556.             y_temp1+=1;
  557.             if(y_temp1>this.height)
  558.                 break;
  559.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  560.                 num++;
  561.             else
  562.                 break;
  563.         }
  564.         if(num==5)
  565.             return true;
  566.         //判断左上
  567.         x_temp1=x_temp;
  568.         y_temp1=y_temp;
  569.         num=1;
  570.         for(int i=1;i<6;i++){
  571.             x_temp1-=1;
  572.             y_temp1-=1;
  573.             if(y_temp1<0 || x_temp1<0)
  574.                 break;
  575.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  576.                 num++;
  577.             else
  578.                 break;
  579.         }
  580.         //判断右下
  581.         x_temp1=x_temp;
  582.         y_temp1=y_temp;
  583.         for(int i=1;i<6;i++){
  584.         x_temp1+=1;
  585.         y_temp1+=1;
  586.         if(y_temp1>this.height || x_temp1>this.width)
  587.             break;
  588.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  589.                 num++;
  590.             else
  591.                 break;
  592.         }
  593.         if(num==5)
  594.             return true;
  595.         //判断右上
  596.         x_temp1=x_temp;
  597.         y_temp1=y_temp;
  598.         num=1;
  599.         for(int i=1;i<6;i++){
  600.             x_temp1+=1;
  601.             y_temp1-=1;
  602.             if(y_temp1<0 || x_temp1>this.width)
  603.                 break;
  604.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  605.                 num++;
  606.             else
  607.                 break;
  608.         }
  609.         //判断左下
  610.         x_temp1=x_temp;
  611.         y_temp1=y_temp;
  612.         for(int i=1;i<6;i++){
  613.             x_temp1-=1;
  614.             y_temp1+=1;
  615.             if(y_temp1>this.height || x_temp1<0)
  616.                 break;
  617.             if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
  618.                 num++;
  619.             else
  620.                 break;
  621.         }
  622.         if(num==5)
  623.             return true;
  624.         return false;
  625.     }
  626.     //赢棋后的提示
  627.     public void showSuccess(JPanel jp){
  628.         JOptionPane.showMessageDialog(jp,"你赢了,好厉害!","win",
  629.             JOptionPane.INFORMATION_MESSAGE);
  630.     }
  631.     
  632.     //输棋后的提示
  633.     public void showDefeat(JPanel jp){
  634.         JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost",
  635.             JOptionPane.INFORMATION_MESSAGE);
  636.     }
  637. }
  638. /*
  639.  *类MainPanel主要完成如下功能:
  640.  *1、构建一个面板,在该面板上画上棋盘;
  641.  *2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)
  642.  **/
  643. class MainPanel extends JPanel 
  644.     implements MouseListener,MouseMotionListener{
  645.     private int width,height;//棋盘的宽度和高度
  646.     private ChessModel cm;
  647.     
  648.     //根据棋盘模式设定面板的大小
  649.     MainPanel(ChessModel mm){
  650.         cm=mm;
  651.         width=cm.getWidth();
  652.         height=cm.getHeight();
  653.         addMouseListener(this);
  654.     }
  655.     
  656.     //根据棋盘模式设定棋盘的宽度和高度
  657.     public void setModel(ChessModel mm){
  658.         cm = mm;
  659.         width = cm.getWidth();
  660.         height = cm.getHeight();
  661.     }
  662.     //根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),
  663.     //然后调用draw方法在棋盘上画出相应的棋子
  664.     public void paintComponent(Graphics g){
  665.         super.paintComponent(g);
  666.         for(int j = 0; j <= height; j++){
  667.             for(int i = 0; i <= width; i++){
  668.                 int v = cm.getarrMapShow()[i][j];
  669.                 draw(g, i, j, v);
  670.             }
  671.         }
  672.     }
  673.     //根据提供的棋子信息(颜色、坐标)画棋子
  674.     public void draw(Graphics g, int i, int j, int v){
  675.         int x = 20 * i+20;
  676.         int y = 20 * j+20;
  677.         //画棋盘
  678.         if(i!=width && j!=height){
  679.             g.setColor(Color.white);
  680.             g.drawRect(x,y,20,20);
  681.         }
  682.         //画黑色棋子
  683.         if(v == 1 ){
  684.             g.setColor(Color.gray);
  685.             g.drawOval(x-8,y-8,16,16);
  686.             g.setColor(Color.black);
  687.             g.fillOval(x-8,y-8,16,16);
  688.         }
  689.         //画白色棋子
  690.         if(v == 2 ){
  691.             g.setColor(Color.gray);
  692.             g.drawOval(x-8,y-8,16,16);
  693.             g.setColor(Color.white);
  694.             g.fillOval(x-8,y-8,16,16);
  695.         }
  696.         if(v ==3){
  697.             g.setColor(Color.cyan);
  698.             g.drawOval(x-8,y-8,16,16);
  699.         }
  700.     }
  701.     //响应鼠标的点击事件,根据鼠标的点击来下棋,
  702.     //根据下棋判断胜负等
  703.     public void mousePressed(MouseEvent evt){
  704.         int x = (evt.getX()-10) / 20;
  705.         int y = (evt.getY()-10) / 20;
  706.         System.out.println(x+" "+y);
  707.         if (evt.getModifiers()==MouseEvent.BUTTON1_MASK){
  708.             cm.play(x,y);
  709.             System.out.println(cm.getisOdd()+" "+cm.getarrMapShow()[x][y]);
  710.             repaint();
  711.             if(cm.judgeSuccess(x,y,cm.getisOdd())){
  712.                 cm.showSuccess(this);
  713.                 evt.consume();
  714.                 ChessFrame.iscomputer=false;
  715.             }
  716.             //判断是否为人机对弈
  717.             if(ChessFrame.iscomputer&&!cm.getisExist()){
  718.                 cm.computerDo(cm.getWidth(),cm.getHeight());
  719.                 repaint();
  720.                 if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){
  721.                     cm.showDefeat(this);
  722.                     evt.consume();
  723.                 }
  724.             }
  725.         }
  726.     }
  727.     public void mouseClicked(MouseEvent evt){}
  728.     public void mouseReleased(MouseEvent evt){}
  729.     public void mouseEntered(MouseEvent mouseevt){}
  730.     public void mouseExited(MouseEvent mouseevent){}
  731.     public void mouseDragged(MouseEvent evt){}
  732.     
  733.     //响应鼠标的拖动事件
  734.     public void mouseMoved(MouseEvent moveevt){
  735.         int x = (moveevt.getX()-10) / 20;
  736.         int y = (moveevt.getY()-10) / 20;
  737.         cm.readyplay(x,y);
  738.         repaint();
  739.     } 
  740. }
  741. class ChessWindowEvent extends WindowAdapter{
  742.     public void windowClosing(WindowEvent e){
  743.         System.exit(0);
  744.     }
  745.     ChessWindowEvent(){}
  746. }