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

~AI1.c(AIと戦えます※勝たないと終わりません)~

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

int main(void)
{
  int syu;
  int count;
  int xai;
  int yai;
  int ran;
  int x;
  int 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;
  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 },
 
};






  for(x=0; x<8; x++) /* masuの初期化*/
    {
      for(y=0; y<8; y++)
	{
	  masu[x][y] = 0;
	}
    }

  printf("   0 1 2 3 4 5 6 7\n");
  
   for(x=0; x<8; x++) 
    {
      printf("\n");
      printf("%d: ",x);
      for(y=0; y<8; y++)
	{
	  printf("%d ", masu[x][y]);
	}
    } 
   printf("\n");
   
  for(i=0; i<32; i++)
    {
      while(1){
      printf("縦のマス指定してください");
      scanf("%d",&x1);
      printf("横のマスを指定してください");
      scanf("%d",&y1);
      if(masu[x1][y1] == 1 || masu[x1][y1] == 2)
	{
	printf("そこには置けないよ\n");
	}
      else{
	break;
      }
      }

      masu[x1][y1] = 1;
      
/*******************************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];
		  if(ev >= 5)  /* 評価が5を越えたら勝利判定 */
		    {
		      printf("勝ちました~");
		      return 0;
		    }
		}
	    }
	}



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

      printf("   0 1 2 3 4 5 6 7\n");
	
  for(x=0; x<8; x++) /*結果の表示*/
    {
      printf("\n");
      printf("%d: ",x);
      for(y=0; y<8; y++)
	{
	  printf("%d ", masu[x][y]);
	}
    } 
  
  printf("\n\n↓評価↓");
  
  for(x=0; x<8; x++) /*評価の表示*/
    {
      printf("\n");
      printf("%d: ",x);
      for(y=0; y<8; y++)
	{
	  printf("%d ", eval[x][y]);
	}
    } 

  

  printf("\n");

    }
  return 0;

}


最終更新日:2014/02/11 16:55:21