当前位置:编程学习 > JAVA >>

各位大神帮帮小弟啊

--------------------编程问答-------------------- //右侧按钮,初始化右侧的button
       private void initRight()
       {
          final Group group_bw=new Group(shell,SWT.NONE);
          group_bw.addPaintListener(new PaintListener()
           {  public void paintControl(final PaintEvent pe)
              {
                  GC gc=pe.gc;
                  gc.drawImage(image,-620,-155);
               }
           });
         
        group_bw.setBounds(619,155,133,80);
        final Button button_b=new Button(group_bw,SWT.NONE);
        button_b.setText("黑棋先手");
        button_b.setBounds(35,25,70,20);
        button_b.setSelection(true);
        final Button button_w=new Button(shell,SWT.NONE);
        start_Button.addSelectionListener(new SelectionAdapter()
        {    public void WidgetSelecter(final SelectionEvent arg0)
             {
                if(button_b.getSelection())
                   startbw=0;
                else
                {
                  startbw=1;
                  putOne(1-startbw);
                }
                start_Button.setEnabled(false);
                String str=(bw==0?"黑棋":"白棋");
                label_wait_bw.setText("等待"+str+"落子");
            }
        });
        start_Button.setText("开始");
        start_Button.setBounds(653,267,76,34);
        final Button restart_button=new Button(shell,SWT.NONE);
        restart_button.addSelectionListener(new SelectionAdapter()
          {   public void WidgetSelected(final SelectionEvent se)
               {      startbw=-1;
                      win=false;
                      for(int i=0;i<15;i++)
                          for(int j=0;j<15;j++)
                       {      chess[i][j]=2;
                              computer[i][j][0]=computer[i][j][1]
                                               =computer[i][j][2]
                                               =computer[i][j][3]=0;
                              player[i][j][0]=player[i][j][1]
                                             =player[i][j][2]
                                             =player[i][j][3]=0;
                        }
               canvas.setVisible(false);
               canvas.setVisible(true);
               aaButton.setEnabled(false);
               bw=0;
               chess_num=0;
               total_time=white_time=black_time=0;
               if(button_b.getSelection())
                  startbw=0;
               else
               {  startbw=1;
                  int y,x;
                  x=y=3;
                  for(int i=3;i<12;i++)
                    for(int j=3;j<12;j++)
                         if(randomTest(20*(Math.abs(7-i)+Math.abs(7-i))+2))
                         {
                             x=i; 
                             y=j;
                          }
                     GC gc=new GC(canvas);
                     draw(gc,x*size+10,y*size+10,bw,8);
                     gc.dispose();
                     chess[x][y]=bw;
                     bw=1-bw;
                   }
                 }
        });
        restart_button.setText("重新开始");
        restart_button.setBounds(653,307,76,34); 
        final Button helpButton=new Button(shell,SWT.NONE);
        helpButton.addSelectionListener(new SelectionAdapter()
        {   public void WidgetSelecter(final SelectionEvent arg0)
            {
                new Help().open();
             }
        });
        helpButton.setText("帮助");
        helpButton.setBounds(653,347,76,34);
        final Button exit_button=new Button(shell,SWT.NONE);
        exit_button.addSelectionListener(new SelectionAdapter()
        {   public void WidgetSelected(final SelectionEvent arg0)
            {
                dispose();
            }
         });
        exit_button.setText("exit");
        exit_button.setBounds(653,387,76,34);
       }
              //极大极小值搜索------------------------------------------------------------------
        /**
                *@param alpha祖先节点得到的最大最小值
                *@param beta祖先节点得到的当前最大最小值
                *@param step还要搜索的步数
                */
        protected int findMax(int alpha,int beta,int step)
        {     int mx=alpha;
              if (step==0)
              {
                 return evaluate();
              }
              for (int i=3;i<11;i++) 
                 for(int j=3;j<11;j++)
                   if(chess[i][j]==2)
                    {
                       if(getType(i,j,1-startbw)==1)
                             return 100*getMark(1);
                       chess[i][j]=1-startbw;
                       int f=findMin(mx,beta,step-1);
                       chess[i][j]=2;
                       if(t>mx)
                         mx=t;
                       if(mx>=beta)
                       {
                           return mx;
                       }
                    }
              return mx;
       }
               /**
                *@param x,y落子位置
                *@param bwf黑棋还是白棋
              */
       protected int getType(int x,int y,int bwf)
      {
            if(chess[x][y]!=2)
                 return -1;
            int[][] types=new int[4][2];
            types[0]=count(x,y,0,1,bwf);  
            types[1]=count(x,y,1,0,bwf);
            types[2]=count(x,y,-1,1,bwf);
            types[3]=count(x,y,1,1,bwf);
            int c5,s4,h4,s3,h3,s2,h2;
            c5=s4=h4=s3=h3=s2=h2=0;
            for(int k=0;k<4;k++)
            {    if(type[k][0]==5)
                    c5++;
                 else if(type[k][0]==4&&type[k][1]==2)
                 h4++;  
                 else if(type[k][0]==4&&type[k][1]!=2)
                 s4++;  
                 else if(type[k][0]==3&&type[k][1]==2)
                 h3++;  
                 else if(type[k][0]==3&&type[k][1]!=2)
                 s3++;  
                 else if(type[k][0]==2&&type[k][1]==2)
                 h2++;  
                 else if(type[k][0]==2&&type[k][1]!=2)
                 s2++;
              }
            if(c5!=0)
                return 1;
            if(h4!=0||s4>=2||s4!=0&&h3!=0) 
                return 2;
            if(h3>=2)
                return 3;
            if(s3!=0&&h3!=0)
                return 4;
            if(s4!=0)
                return 5;
            if(h3!=0)
                return 6;
            if(h2>=2)
                return 7;
            if(s3!=0)
                return 8;
            if(h2!=0&&s2!=0)
                return 9;
            if(h2!=0)
                return 10;
            if(s2!=0)
                return 11;
            return 0;
      }
             /*
             *@param k棋型代号
             */
      private int getMark(int k)
      {    switch (k)
           {
             case 1:
                 return 100000;
             case 2:
                 return 10000;
             case 3:
                 return 5000;
             case 4:
                 return 1000;
             case 5:
                 return 500;
             case 6:
                 return 200;
             case 7:
                 return 100;
             case 8:
                 return 50;
             case 9:
                 return 10;
             case 10:
                 return 5;
             case11:
                 return 3;
             default:
                 return 0;
         }
    } --------------------编程问答--------------------  /**
                *@param alpha祖先节点得到的最大最小值
                *@param beta祖先节点得到的当前最大最小值
                *@param step还要搜索的步数
                */
    protected int findMin(int alpha,int beta,int step)
    {
       int mn=beta;
       if(step==0)
       {
          return evaluate();
       }
       int[][] rt=getBests(startbw);
       for(int i=0;i<rt.length;i++)
      {  int ii=rt[i][0];
         int jj=rt[i][1];
         if(getType(ii,jj,startbw)==1)
           return -100*getMark(1);
         chess[ii][jj]=2;
         if(t<mn)
            mn=t;
         if(mn<alpha)
         {  return mn;
         }
       }
       return mn;
     }
 
     protected int evaluate()
     {    int rt=0;
          for(int i=0;i<15;i++)
              for(int j=0;j<15;j++)
                   if(chess[i][j]==2)
                   {
                      int type=getType(i,j,1-startbw);
                      rt+=getMark(type);
                      type=getType(i,j,startbw);
                      rt-=getMark(type);
                    }
           return rt;
      }
      /**
              *@param bwf 棋色
              */
      private int[][] getBests(int bwf)
      {     int[][] rt=new int[255][3];
            int n=0;
            for(int i=0;i<15;i++)
               for(int j=0;j<15;j++)
                  if(chess[i][j]==2)
                  {
                      rt[n][0]=i;
                      rt[n][1]=i;
                      rt[n][2]=getMark(getType(i,j,bwf))
                          +getMark(getType(i,j,1-bwf));
                      n++;
                   }
       Arrays.sort(rt,new ArrComparator());
       int size=8>n?n:8;
       int[][] bests=new int[size][3];
       System.arraycopy(rt,0,bests,0,size);
       return bests;
      }
    
                /**
              *@param bwf 棋色
              */
            //计算机落子的实现
     private void putOne(int bwf)
     {     int it,jt,mx=-100000000;
           it=jt=-1;
           int[][] bests=getBests(bwf);
           for (int k=0;k<bests.length;k++)
           {    int i=bests[k][0];
                int j=bests[k][1];
                if(getType(i,j,bwf)==1)
                {
                  it=i;
                  jt=j;
                  break;
                 }
                 chess[i][j]=bwf;
                 int t=findMin(-100000000,100000000,3);
                 chess[i][j]=2;
                 if(t>mx||t==mx&&randomTest(3*(Math.abs(7-i)+Math.abs(7-j))+2))
                 {    it=i;
                      jt=j;
                      mx=t;
                 }
           }
           GC gc=new GC(canvas);
           this.draw(gc.it*size+10,jt*size+10,bwf,8);
           if(haveWin(it,jt,bwf))
           { wined(gc,bwf);
           }
           chess[it][jt]=bwf;
           bw=1-bw;
           updateChess();
           if(chess_num==15*15)
           {   win=true;
               Font font=new Font(Display.getDefault(),"Arial",27,SWT.BOLD|SWT.ITALIC);
               gc.setFont(font);
               Color color=new Color(Display.getDefault(),120,242,105);
               gc.setForeground(color);
               String str="wuziluodian heqi!!";
               font.dispose();
               color.dispose();
               return;
            }
           gc.dispose();
         }
                /**
               *@param kt随即因子
                */
                 private boolean randomTest(int kt)
           {
            Random rm=new Random();
            return rm.nextInt()%kt==0;
           }
       
      
      private void updateChess()
      {  chess_num++;
         label_luozi.setText("总落子:"+chess_num);
         String str=(bw==0?"黑棋":"白棋");
         label_wait_bw.setText("等待"+str+"落子");
       }



      private void startTime()
      {   time=new Timer(false);
          time.schedule(new TimerTask()
          {  public void run()
             {     if(!win&&startbw!=-1)
                   { total_time++;
                     if(bw==0)
                       black_time++;
                     else
                       white_time++;
                   Display.getDefault().syncExec(new Runnable()
                   {    public void run()
                        {
                           label_time.setText("总耗时:"+total_time/60+"分"+total_time%60+"秒");
                           label_black_time.setText("黑棋时"+black_time/60+"分"+black_time%60+"秒");
                           label_white_time.setText("白棋耗时:"+white_time/60+"分"+white_time%60+"秒");
               }
             });
            }
          }
       },0,1000);
      }



2.ArrComparator.java
import java.util.Comparator;
class ArrComparator implements Comparator<Object>{
int column=2;
int sortOrder=-1;
public ArrComparator() {}
public ArrComparator(int cl)
{
column=cl;
}
 public int compare(Object a,Object b)
 {    if(a instanceof int[])
      {   return sortOrder*(((int[]) a)[column]-((int[]) b)[column]);
      
      }
        throw new IllegalArgumentException("param a,b must be int[].");
 }

}





3.Help.java


import java.awt.Label;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class Help
{
protected Shell shell;
public static void main(String[] args)
{
 try
 {
 Image image=new Image(Display.getDefault(),"bk,jpg");
 Help window=new Help();
 window.open();
 }catch(Exception e)
 {
 e.printStackTrace();
 }
}
public void open()
{   final Display display=Display.getDefault();
    createContents();
    shell.open();
            //貌似这后面还要写东西
    
}
//左侧按钮的实现 首先创建一个group然后创建相应的标签label对象,并设置相应的显示文字,最后将标签放入group
protected void createContents()
{
shell=new Shell(SWT.APPLICATION_MODAL|SWT.TITLE|SWT.BORDER|SWT.CLOSE);
shell.setSize(340,240);
shell.setText("五子棋无禁手");
Color color=new Color(Display.getDefault(),0,150,150);
shell.setBackground(color);
color.dispose();
final Label label=new Label(shell,SWT.NONE);
label.setText("五子棋无禁手 版本v1.0");
label.setBounds(81,10,140,15);

}

}

   
  
   


--------------------编程问答-------------------- 小弟qq67663507,如果需要沟通可以+qq.实在是不太懂啊,刚学一些,跪谢啊
补充:Java ,  Java相关
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,