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

关于遗传算法设计的五子棋相关问题

问题,每一次黑子下了以后,白子(电脑)就只会下在黑子下方,请教一下怎么改,万分感谢
核心代码如下:
public class GameView extends View{
private int startX=0;
private int startY=0;
private int tile_Width=GlobalData.getWidth()/15;
private int tile_num=16;  
private int power;
private int need_X,need_Y;
private boolean active_four=false;
private boolean active_three=false;
private boolean active_two=false;
private boolean active_one=false;
private int index_x;
private int power_up;
private int[] num=new int[4];
private int index_y;
private Paint paint =null;
private int chess[][] =new int [tile_num][tile_num];
private int black=1;
private int white=2;
private int type;
private int flag=0;   
public boolean onTouchEvent(MotionEvent event)
    {
     float x=event.getX();
     float y=event.getY();
     index_x=Math.round((x-startX)/tile_Width);
     index_y=Math.round((y-startY)/tile_Width);
     flag=black;
     if(flag==black && chess[index_x][index_y]==0)
     {
     type=white;
     single_power(index_x,index_y);
     chess[need_X][need_Y]=white;
     flag=white;
     }
     if(flag==white&&chess[index_x][index_y]==0)
     {
     type=black;
     chess[index_x][index_y]=black;
     flag=black;
     }
        invalidate();
        return super.onTouchEvent(event);
    }
  public void single_power(int x,int y)
  {   
 int i,j;
 int needed_X=0,needed_Y=0;
 need_X=x;
 power_up=17;
 need_Y=y+1;
 for(i=x-3;i<x+3;i++)
 {
  for(j=y-3;j<y+3;j++)
      {
if(type==white&&i!=x&&j!=y)
{
judge(i,j);
}

                if(power_up<power)
{
power_up=power;
need_X=needed_X;
need_Y=needed_Y;
}
}
  }
}
public void judge(int x, int y)
      {    
         int i=x;
         int j=y;
         int h=0,k=0;
         int search_deep[]=new int[8];
         for(h=0;h<search_deep.length;h++)
            {
            search_deep[h]=search(x,y,white,h+1);
            }
           for(k=0;k<8;k++)
           {
            if(search_deep[k]==5)
          {      
         active_four=active(x,y,k+1,white,0);
         if(active_four)
         {
          power=90;
         }
         if(num[0]>=2)
      {
        power=92;
      }
         }
            else if(search_deep[k]==4)
            {
            active_three=active(x,y,k+1,white,1);
            if(active_three&&num[0]==1)
       {
       power=85;
       }
            if(active_three)
            {
           power=83;
            }
            if(num[1]>=2)
       {
       power=80;
       }
            }
            else if(search_deep[k]==3)
            {
            active_two=active(x,y,k+1,white,2);
            if(active_two&&num[1]==1)
       {
       power=60;
       }
           if(active_two)
           {
           power=57;
           }
           if(num[2]>=2)
       {
       power=55;
       }
            }
            else if(search_deep[k]==2)
            {
            active_one=active(x,y,k+1,white,3);
            if(active_one&&num[2]==1)
       {
       power=20;
       }
          if(active_one)
          {
           power=17;
          }
          if(num[3]>=2)
      {
       power=15;
      }
         }
            
         }
   }
   public boolean  active(int x,int y,int direction,int type,int count)
    {   
   boolean  active_flag=false;
   if(direction==1)
     {   
    if(count==0){
    if(chess[x+1][y]==0&&chess[x-4][y]==0)
         {
          active_flag=true;
         }
         if(chess[x+1][y]==0&&chess[x-4][y]!=type&&chess[x-4][y]!=0)
         {
          num[count]++;
         }}
    else if(count==1)
    {
    if(chess[x+1][y]==0&&chess[x-3][y]==0)
         {
          active_flag=true;
         }
         if(chess[x+1][y]==0&&chess[x-3][y]!=type&&chess[x-3][y]!=0)
         {
          num[count]++;
         }}
    else if(count==2)
    {
    if(chess[x+1][y]==0&&chess[x-2][y]==0)
         {
          active_flag=true;
         }
         if(chess[x+1][y]==0&&chess[x-2][y]!=type&&chess[x-2][y]!=0)
         {
          num[count]++;
         }}
    else if(count==3)
    {
    if(chess[x+1][y]==0&&chess[x-1][y]==0)
         {
          active_flag=true;
         }
         if(chess[x+1][y]==0&&chess[x-1][y]!=type&&chess[x-1][y]!=0)
         {
          num[count]++;
         }}
     }
  //剩下七个方向的代码是对的,可以判断活四,活三,单四,单三...(已经测试过了)但是因为代码量大大,论坛不让贴出来。代码的形式和direction1一样。
     return active_flag;
    }
  public int search(int x,int y,int type1,int direction)
    {
     int i=x;
     int j=y;
     int t=1,h;
     for(h=0;h<5;h++)
     {
     if(x>15)
     {
     break;
     }
     if(direction==1){
            if(chess[i][j]==type1)
            {
             i=i-1;
             t++;
            }
            }
     if(direction==2){
                if(chess[i][j]==type1)
                {
                 i=i+1;
                 t++;
                }
                }
     if(direction==3){
                if(chess[i][j]==type1)
                {
                 j=j-1;
                 t++;
                }
                }
     if(direction==4){
                if(chess[i][j]==type1)
                {
                 j=j+1;
                 t++;
                }
                }
     if(direction==5){
                if(chess[i][j]==type1)
                {
                 i=i-1;
                 j=j-1;
                 t++;
                }
                }
     if(direction==6){
                if(chess[i][j]==type1)
                {
                 i=i-1;
                 j=j+1;
                 t++;
                }
                }
     if(direction==7){
                if(chess[i][j]==type1)
                {
                 i=i+1;
                 j=j-1;
                 t++;
                }
               }
     if(direction==8){
                if(chess[i][j]==type1)
                {
                 i=i+1;
                 j=j+1;
                 t++;
                }
                }
     }
     return t;
    } 
} 热弄 五子棋 人工智能
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,