Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA中的简单沙堆代码_Java_Cellular Automata - Fatal编程技术网

JAVA中的简单沙堆代码

JAVA中的简单沙堆代码,java,cellular-automata,Java,Cellular Automata,有人知道我在哪里可以找到沙堆(细胞自动机)的源代码吗

有人知道我在哪里可以找到沙堆(细胞自动机)的源代码吗<提前谢谢

以下是一个源代码为的沙堆细胞自动机Java小程序:

@服务(“沙堆添加”)
公共级沙漏修正
{
专用静态最终记录器记录器=LoggerFactory.getLogger(sandpileadition.class);
/**
* 
*@param n
*@param limitSupCase
*@param第1行
*@param第2行
*/
public int[][]沙堆(int n,int limitSupCase,String[]row1,String[]row2)
{
logger.info(“沙堆添加”);
int[][]数组=新的int[n][n];
对于(int i=0;i=0&&i-1=0&&j-1=limitSupCase)返回新的casetrovee(i,j);
}
}
返回null;
}
类案件
{
公开期末考试i;
公共最终国际法院;
公共案件组(内部i、内部j)
{
这个。i=i;
这个。j=j;
}
}
}
@ExtendWith(SpringExtension.class)
@春靴测试
@TestMethodOrder(OrderAnnotation.class)
公共级沙堆试验
{
专用静态最终记录器记录器=LoggerFactory.getLogger(SandpileAdditionTest.class);
@自动连线
私人砂井砂井砂井砂井砂井砂井;
私有静态最终int limitSupCase=4;
私有字符串[]数组1=null;
私有字符串[]array2=null;
私有int[][]应为空;
私有int matrixSize=0;
/**
*测试前初始化参数
* 
* 
*@param testInfo:内容参数当前测试
*/
@之前
void initParams(TestInfo TestInfo)
{
logger.info(“initParams”);
开关(testInfo.getTestMethod().get().getName())
{
案例“沙堆试验”:
array1=新字符串[]{“12123212”、“21221323”、“13231321”、“30232213”、“01123100”、“32203321”、“32303321”、“32123321”};
array2=新字符串[]{“30321311”、“20231002”、“12303321”、“31233321”、“12312312323”、“13123121”、“13132121”、“32303321”};
预期=新的int[][]{{2,0,1,3,3,1,2},{1,3,3,2,1,3,0},{3,2,3,3,1,1,0,2,1},{3,3,2,1,1,3,3,3,3,3,3},{0,1,2,3,2,2,2,2,1,1},{3,2,3,3,2,3},{1,3,3,3,3,3,3};
matrixSize=8;
打破
案例“砂桩试验”:
array1=新字符串[]{“31003”、“13120”、“01310”、“02131”、“30013”};
array2=新字符串[]{“11111”、“12221”、“12321”、“12221”、“11111”};
预期=新的int[][{2,2,1,1,2},{2,3,3,2,1},{1,3,2,3,1},{1,2,3,3,2},{2,1,1,2,2};
矩阵大小=5;
打破
案例“砂桩试验”:
array1=新字符串[]{“1212321”、“1221323”、“1231321”、“3032213”、“0123100”、“3203321”、“3203321”};
array2=新字符串[]{“3032131”、“0231002”、“2303321”、“1231321”、“1232323”、“1323121”、“133211”};
预期=新int[][]{1,1,1,2,2,0,1},{3,1,3,2,1,3},{3,2,3,3},{0,2,3,3,2,2,2,0},{2,2,1,3,3,3,2},{3,0,3,3,3,0,3},{3,1,2,2,2,3};
matrixSize=7;
打破
案例“砂桩试验”:
array1=新字符串[]{“121”、“202”、“121”};
array2=新字符串[]{“020”、“202”、“020”};
预期=新int[][{{3,1,3},{1,0,1},{3,1,3};
matrixSize=3;
打破
案例“砂桩试验”:
array1=新字符串[]{“121321”、“122323”、“123121”、“303213”、“023100”、“303321”};
array2=新字符串[]{“303213”、“023100”、“303321”、“121321”、“122323”、“123121”};
预期=新int[][{0,3,2,3,3,0},{2,1,3,1,3,2},{1,3,3,1,3,3},{1,3,2,2,2,3,0},{2,3,0,2,2},{1,0,2,3,3,2},{1,0,2,3,2};
矩阵大小=6;
打破
案例“砂桩试验”:
array1=新字符串[]{“000”、“020”、“000”};
array2=新字符串[]{“000”、“020”、“000”};
@Service("sandpileAddition")
public class SandpileAddition
{
   private static final Logger logger = LoggerFactory.getLogger( SandpileAddition.class );
   /**
    * 
    * @param n
    * @param limitSupCase
    * @param row1
    * @param row2
    */
   public int[][] sandPile( int n, int limitSupCase, String[] row1, String[] row2 )
   {
      logger.info( "sand Pile Addition" );
      int[][] array = new int[n][n];
      for( int i = 0; i < n; i++ )
      {
         String s = row1[i];
         for( int j = 0; j < row1.length; j++ )
         {
            String z = s.substring( j, j + 1 );
            array[i][j] = Integer.parseInt( z );
         }
      }
      for( int i = 0; i < n; i++ )
      {
         String s = row2[i];
         for( int j = 0; j < row2.length; j++ )
         {
            String z = s.substring( j, j + 1 );
            array[i][j] += Integer.parseInt( z );
         }
      }
      return formaterResultatSortie( distribuerGrainsSurCase( array, n, limitSupCase ), n );
   }

   private int[][] formaterResultatSortie( int[][] array, int n )
   {
      for( int i = 0; i < n; i++ )
      {
         for( int j = 0; j < n; j++ )
         {
            //System.out.print( array[i][j] );
         }
         //System.out.println();
      }
      return array;
   }

   private int[][] distribuerGrainsSurCase( int[][] array, int n, int limitSupCase )
   {
      CaseTrouvee caseTrouvee = null;
      while( ( caseTrouvee = chercherCase( array, n, limitSupCase ) ) != null )
         distribuerSurLesCases( array, n, caseTrouvee, limitSupCase );
      return array;
   }

   private void distribuerSurLesCases( int[][] array, int n, CaseTrouvee caseTrouvee, int limitSupCase )
   {
      int i = caseTrouvee.i;
      int j = caseTrouvee.j;
      array[i][j] -= limitSupCase;
      if( i - 1 >= 0 && i - 1 < n ) array[i - 1][j] += 1;
      if( i + 1 < n ) array[i + 1][j] += 1;
      if( j - 1 >= 0 && j - 1 < n ) array[i][j - 1] += 1;
      if( j + 1 < n ) array[i][j + 1] += 1;
   }

   private CaseTrouvee chercherCase( int[][] array, int n, int limitSupCase )
   {
      for( int i = 0; i < n; i++ )
      {
         for( int j = 0; j < n; j++ )
         {
            if( array[i][j] >= limitSupCase ) return new CaseTrouvee( i, j );
         }
      }
      return null;
   }
   class CaseTrouvee
   {
      public final int i;

      public final int j;
      public CaseTrouvee( int i, int j )
      {
         this.i = i;
         this.j = j;
      }
   }
}


@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class SandpileAdditionTest
{
   private static final Logger logger       = LoggerFactory.getLogger( SandpileAdditionTest.class );

   @Autowired
   private SandpileAddition    sandpileAddition;

   private static final int    limitSupCase = 4;

   private String[]            array1       = null;

   private String[]            array2       = null;

   private int[][]             expected     = null;

   private int                 matrixSize   = 0;
   /**
    * initialized parameters before test
    * 
    * 
    * @param testInfo : content parameters current test
    */
   @BeforeEach
   void initParams( TestInfo testInfo )
   {
      logger.info( "initParams" );
      switch( testInfo.getTestMethod().get().getName() )
      {
         case "sandpileAddition07Test":
            array1 = new String[] { "12123212", "21221323", "13231321", "30232213", "01123100", "32203321", "32303321", "32123321" };
            array2 = new String[] { "30321311", "20231002", "12303321", "31231321", "12312323", "13123121", "13132121", "32303321" };
            expected = new int[][] { { 2, 0, 1, 3, 3, 3, 1, 2 }, { 1, 3, 3, 3, 2, 1, 3, 0 }, { 3, 2, 3, 3, 1, 0, 2, 1 }, { 3, 3, 2, 1, 1, 3, 3, 3 }, { 0, 1, 2, 3, 2, 1, 2, 1 }, { 3, 2, 3, 1, 3, 2, 0, 3 }, { 1, 3, 1, 3, 2, 3, 3, 3 }, { 2, 1, 3, 2, 3, 1, 3, 3 } };
            matrixSize = 8;
            break;
         case "sandpileAddition06Test":
            array1 = new String[] { "31003", "13120", "01310", "02131", "30013" };
            array2 = new String[] { "11111", "12221", "12321", "12221", "11111" };
            expected = new int[][] { { 2, 2, 1, 1, 2 }, { 2, 3, 3, 2, 1 }, { 1, 3, 2, 3, 1 }, { 1, 2, 3, 3, 2 }, { 2, 1, 1, 2, 2 } };
            matrixSize = 5;
            break;
         case "sandpileAddition05Test":
            array1 = new String[] { "1212321", "1221323", "1231321", "3032213", "0123100", "3203321", "3203321" };
            array2 = new String[] { "3032131", "0231002", "2303321", "1231321", "1232323", "1323121", "1332121" };
            expected = new int[][] { { 1, 1, 1, 2, 2, 0, 1 }, { 3, 1, 3, 2, 2, 1, 3 }, { 3, 2, 3, 1, 2, 2, 3 }, { 0, 2, 3, 3, 2, 2, 0 }, { 2, 2, 1, 3, 3, 3, 2 }, { 3, 0, 3, 3, 3, 0, 3 }, { 3, 1, 2, 0, 2, 2, 3 } };
            matrixSize = 7;
            break;
         case "sandpileAddition04Test":
            array1 = new String[] { "121", "202", "121" };
            array2 = new String[] { "020", "202", "020" };
            expected = new int[][] { { 3, 1, 3 }, { 1, 0, 1 }, { 3, 1, 3 } };
            matrixSize = 3;
            break;
         case "sandpileAddition03Test":
            array1 = new String[] { "121321", "122323", "123121", "303213", "023100", "303321" };
            array2 = new String[] { "303213", "023100", "303321", "121321", "122323", "123121" };
            expected = new int[][] { { 0, 3, 2, 3, 3, 0 }, { 2, 1, 3, 1, 3, 2 }, { 1, 3, 3, 1, 3, 3 }, { 1, 3, 2, 2, 3, 0 }, { 2, 3, 0, 2, 3, 2 }, { 1, 0, 2, 3, 3, 2 } };
            matrixSize = 6;
            break;
         case "sandpileAddition02Test":
            array1 = new String[] { "000", "020", "000" };
            array2 = new String[] { "000", "020", "000" };
            expected = new int[][] { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
            matrixSize = 3;
            break;
         case "sandpileAddition01Test":
            array1 = new String[] { "121", "232", "121" };
            array2 = new String[] { "303", "000", "303" };
            expected = new int[][] { { 2, 1, 2 }, { 1, 3, 1 }, { 2, 1, 2 } };
            matrixSize = 3;
            break;
         case "sandpileAddition00Test":
            array1 = new String[] { "121", "232", "121" };
            array2 = new String[] { "303", "000", "303" };
            expected = new int[][] { { 1, 1, 2 }, { 0, 3, 1 }, { 2, 0, 2 } };
            matrixSize = 3;
            break;
         default:
            break;
      }
      assertNotNull( sandpileAddition );
      logger.info( " begin " + testInfo.getTestMethod().get().getName() );
   }

   /**
    * destroyed parameters after test
    * 
    * @param testInfo : content parameters current test 
    */
   @AfterEach
   void destroy( TestInfo testInfo )
   {
      logger.info( "destroy" );
      String callingTest = testInfo.getTestMethod().get().getName();
      logger.info( " end " + callingTest );
      array1 = null;
      array2 = null;
      expected = null;
      matrixSize = 0;
   }

   @Test
   public void sandpileAddition07Test() throws Exception
   {
      logger.info( "sandpileAddition07Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition06Test() throws Exception
   {
      logger.info( "sandpileAddition06Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition05Test() throws Exception
   {
      logger.info( "sandpileAddition05Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition04Test() throws Exception
   {
      logger.info( "sandpileAddition04Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition03Test() throws Exception
   {
      logger.info( "sandpileAddition03Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition02Test() throws Exception
   {
      logger.info( "sandpileAddition02Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition01Test() throws Exception
   {
      logger.info( "sandpileAddition01Test" );
      assertEquals( true, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   @Test
   public void sandpileAddition00Test() throws Exception
   {
      logger.info( "sandpileAddition01Test" );
      assertEquals( false, compareMatrices( matrixSize, sandpileAddition.sandPile( matrixSize, limitSupCase, array1, array2 ), expected ) );
   }

   /**
    * compare two matrix contents
    * 
    * @param n ; matrix dimension 
    * @param array : first matrix
    * @param expected : second matrix
    * 
    * @return return true if array is equals expected and false otherwise
    */
   private boolean compareMatrices( int n, int[][] array, int[][] expected )
   {
      assertNotNull( array );
      assertEquals( n, array.length );
      assertEquals( n, array[0].length );

      assertNotNull( expected );
      assertEquals( n, expected.length );
      assertEquals( n, expected[0].length );      

      for( int i = 0; i < n; i++ )
      {
         for( int j = 0; j < n; j++ )
         {
            if( array[i][j] != expected[i][j] )
            {
               return false;
            }
         }
      }
      return true;
   }
}