ソフトウェア設計及び演習用の班Wiki

AI.c

#include <stdio.h>
#include <stdlib.h> /* randを使いたいので */

int AI(void) 
{
  int count;
  int xai;
  int yai;
  int ran;
  extern int x; //繰り返し文、gameに返すAIのx座標など
  extern int y; //繰り返し文、gameに返すAIのy座標など
  int x1;
  int y1;
  int i;
  int p;
  int count3 = 0; /*右下がり斜めカウント*/
  int count4 = 0; /*右上がり斜めカウント*/
  int ev2[63];
  int ev3[63];
  int ev4[63];
  int countev2 = 0;
  extern int masu[8][8]; 
  int ev = 0;
  int eval[8][8] =
{
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
      { 0, 0, 0, 0, 0, 0, 0, 0 },
 
};



      
/*******************************AIの部分***************************************/
     
      /*----------------------横の評価----------------------*/
      for(x=0; x<8; x++)    
	{
	  count = 0;
	for(y=0; y<8; y++)
	  {
	    if(masu[x][y] == 1)
	      {
		count++;
		if(y+1 <= 7)
		  {
		  if(eval[x][y+1] < count)
		    {
		      eval[x][y+1] = count;
		    }
		  }
		if(y - count >= 0)
		  {
		    if(eval[x][y-count] < count)
		      {
			eval[x][y-count] =  count;
		      }
		  }
	      }
	    if(masu[x][y] == 0 || masu[x][y] == 2)
	      {
		count = 0;
	      }
	  }
	}
      
      /*---------------------縦の評価------------------------*/
      for(y=0; y<8; y++)
	{
	  count = 0;
	  for(x=0; x<8; x++)
	    {
	      if(masu[x][y] == 1)
		{
		  count++;
		  if(x+1 <= 7)
		    {
		      if(eval[x+1][y] < count) /*評価が高い方を使う*/
			{
			  eval[x+1][y] = count;
			}
		    }
		  if(x - count >= 0)
		    {
		      if(eval[x-count][y] < count)
			{
			  eval[x-count][y] = count;
			}
		    }	
		}
	      if(masu[x][y] == 0 || masu[x][y] == 2)
		{
		  count = 0;
		}
	    }
	}
      
      /*--------------------右下がり斜めの評価-----------------------*/ 
        
      for(p=0; p<=3; p++)
	{
	  y=p;
	  count3 = 0;
	  for(x=0; x<=7-p; x++)
	    {
	      if(masu[x][y] == 1)
		{
		  count3++;
		  if(x+1 <= 7)
		    {
		      if(eval[x+1][y+1] < count3)
			{
			  eval[x+1][y+1] = count3;
			}
		    }
		  if(x - count3 >= 0)
		    {
		      if(eval[x-count3][y-count3] < count3)
			{
			  eval[x-count3][y-count3] = count3;
			}
		    }
		}
	      if(masu[x][y] == 0 || masu[x][y] == 2)
		{
		  count3 = 0;
		}
	      y++;
	    }
	}	  

      for(p=0; p<=2; p++)
	{
	  x=p+1;
	  count3 = 0;
	  for(y=0; y<=7-x; y++)
	    {
	      if(masu[x][y] == 1)
		{
		  count3++;
		   if(x+1 <= 7)
		    {
		      if(eval[x+1][y+1] < count3)
			{
			  eval[x+1][y+1] = count3;
			}
		    }
		   if(x - count3 >= 0)
		     {
		       if(eval[x-count3][y-count3] < count3)
			 {
			   eval[x-count3][y-count3] = count3;
			 }
		     }
		}
	      if(masu[x][y] == 0 || masu[x][y] == 2)
		{
		  count3 = 0;
		}
	      x++;
	    }
	}
		  
	      
       /*--------------------右上がり斜めの評価-----------------------*/

      for(p=4; p<=7; p++)
	{
	  x = p;
	  count4 = 0;
	  for(y=0; y<=p; y++)
	    {
	       if(masu[x][y] == 1)
		{
		  count4++;
		  if(x >= 1)
		    {
		      if(eval[x-1][y+1] < count4)
			{
			  eval[x-1][y+1] = count4;
			}
		    }
		  if(y - count4 >= 0)
		    {
		      if(eval[x+count4][y-count4] < count4)
			{
			  eval[x+count4][y-count4] = count4;
			}
		    }
		}
	       if(masu[x][y] == 0 || masu[x][y] == 2)
		 {
		   count4 = 0;
		 }
	       x--;
	    }
	}

      for(p=1; p<=3; p++)
	{
	  x = p;
	  count4 = 0;
	  for(y=7; y>=p; y--)
	    {
	      if(masu[x][y] == 1)
		{
		  count4++;
		  if(x+1 <= 7)
		    {
		      if(eval[x+1][y-1] < count4)
			{
			  eval[x+1][y-1] = count4;
			}
		    }
		  if(y + count4 <= 7)
		    {
		      if(eval[x-count4][y+count4] < count4)
			{
		      eval[x-count4][y+count4] = count4;
			}
		    }
		}
	      if(masu[x][y] == 0 || masu[x][y] == 2)
		{
		  count4 = 0;
		}
	      x++;
	    }
	}



      for(x=0; x<8; x++)      /*石の置いてある部分の評価の初期化*/
	{
	  for(y=0; y<8; y++)
	    {
	      if(masu[x][y] == 1 || masu[x][y] == 2)
		{
		  eval[x][y] = 0;
		}
	    }
	}
      
      for(x=0; x<8; x++)     /*評価の最大値を調べる*/
	{
	  for(y=0; y<8; y++)
	    {
	      if(eval[x][y] > ev)
		{
		  ev = eval[x][y];
		}
	    }
	}



      for(x=0; x<8; x++)        /*評価が最大値の座標の格納*/
	{
	  for(y=0; y<8; y++)
	    {
	      if(eval[x][y] == ev)
		{
		  ev3[countev2] = x;
		  ev4[countev2] = y;
		  countev2++;
		}
	    }
	}
      ev = 0;                   /*evの初期化*/
      ran = rand()%countev2;    /*乱数を使って手を決める*/
      xai = ev3[ran];
      yai = ev4[ran];
      countev2 = 0;             /*countev2の初期化*/
      
/******************************************************************************/
      
      x = ev3[ran];
      y = ev4[ran];

  return 0;

}


最終更新日:2014/01/30 14:48:28