2014-12-07 63 views
0

我遇到了生命游戏的麻烦,我的几代人都在经历,但是它做得不正确。我有一个生成类,它持有游戏的规则和世代的运动。一个RunActivity,它通过移动并将其发送到一个myView类,该类接收新的二维数组并将其“绘制”到屏幕上。 MyView有两个setPoint方法,一个基于来自gen类的二维数组添加点到一个二维数组,另一个基于触摸运动添加点。另外还有一个ontouchlistener,它增加了一个点的数组列表和一个主要活动获取生活游戏的设置。这是我最相关的代码:生命的游戏Android Java

package com.example.gameoflife; 


public class Generate { 
    int dim; 
    int[][] gena; 
    int[][] genb; 
    int a; 
    //public static int[][] genc; 


public Generate(int size){ 
    this.dim = size; 
    this.gena = new int[dim][dim]; 
    this.genb = new int[dim][dim]; 
    //this.genc = new int[d][d]; 
} 

public boolean birth(int parent, int neighbors){ 
    //rules for game of life 
    if ((parent == 0)&&(neighbors == 3)) 
     return true; 
    if ((parent > 0)&&((neighbors == 3)||(neighbors == 2))) 
     return true; 
    return false; 
} 
public void grow(){ 
    //puts genb in gena after printing, clears genb 
    for(int r = 0; r < dim; r++){ 
     for(int c = 0; c < dim; c++){ 
      gena[r][c] = genb[r][c]; 
     } 
    } 
} 

public void random(){ 
    //randomly fills object with life 
    for(int r = 0; r<gena.length; r++){ 
     for(int c=0; c<dim;c++){ 
      gena[r][c] = (int) (Math.random()*2); 
     } 
    } 
} 

public void makelove(){ 
    //nested loop to determine where the cell is, and if it will live in the next generation 

    for(int r = 0; r<dim; r++){ 
     for(int c = 0; c<dim; c++){ 
      a = 0; 
      for (int i = r-1; i <= r+1; i++){ 
       for (int j = c-1; j <= c+1; j++){ 
        if((i >= 0)&&(i < dim)&&(j >= 0)&&(j < dim)&&((i!=r)&&(j!=c))){ 
         if (gena[i][j] > 0)      
         a++;  
        } 
       } 
      } 
     if (birth(gena[r][c], a)) 
      genb[r][c]++; 
     else 
      genb[r][c] = 0; 


      //topleft 
//   if ((r == 0)&&(c == 0)){ 
//    a = (gena[r+1][c] + gena[r+1][c+1] + gena[r][c+1]); 
//     if (birth(gena[r][c], a)) 
//      genb[r][c]++; 
//     else 
//      genb[r][c] = 0; 
//   } 
//   //topmid 
//   else if ((r == 0)&&((c !=0)&&(c != (gena.length-1)))){ 
//    a = (gena[r][c-1] + gena[r+1][c-1] + gena[r+1][c] + gena[r+1][c+1] + gena[r][c+1]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //topright 
//   else if ((r == 0)&&(c == (gena.length-1))){ 
//    a = (gena[r][c-1] + gena[r+1][c-1] + gena[r+1][c]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //rightmid 
//   else if ((r != 0)&&(r!= (gena.length-1))&&(c == (gena.length-1))){ 
//    a = (gena[r-1][c] + gena[r-1][c-1]+ gena[r][c-1] + gena[r+1][c-1] + gena[r+1][c]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //rightbottom 
//   else if ((r == (gena.length-1))&&(c == (gena.length-1))){ 
//    a = (gena[r][c-1] + gena[r-1][c-1]+ gena[r-1][c]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //bottommid 
//   else if ((r == (gena.length-1))&&(c != 0)&&(c != (gena.length-1))){ 
//    a = (gena[r][c-1] + gena[r-1][c-1]+ gena[r-1][c] + gena[r-1][c+1] + gena[r][c+1]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //leftbottom 
//   else if((c == 0)&&(r == (gena.length-1))){ 
//    a = (gena[r-1][c] + gena[r-1][c+1]+ gena[r][c+1]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //Left mid 
//   else if((c == 0)&&(r != 0)&&(r != (gena.length-1))){ 
//    a = (gena[r-1][c] + gena[r-1][c+1]+ gena[r][c+1] + gena[r+1][c+1] + gena[r+1][c]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
//   //midmid 
//   else{ 
//    a = (gena[r][c+1] + gena[r][c-1]+ gena[r-1][c-1] + gena[r-1][c+1] + gena[r-1][c] + gena[r+1][c] + gena[r+1][c-1] + gena[r+1][c+1]); 
//    if (birth(gena[r][c], a)) 
//     genb[r][c]++; 
//    else 
//     genb[r][c] = 0; 
//   } 
     } 
    } 

} 
} 

的受阻代码是另外一个版本我是想这并没有工作,要么但具有不同的影响。

package com.example.gameoflife; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class RunActivity extends Activity { 
    private MyView myView; 
    protected float xTouchPosition = 0; 
    protected float yTouchPosition = 0; 
    protected int dimension; 
    static int d; 
    protected int size; 
    private final static String TAG = "ThreadingMainActivity"; 
    private int mDelay = 2000; 
    int array[] = new int[5]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     //creates and sets view, receives and sets settings of GOF from main activity, creates life object, loops an execute thread 
     super.onCreate(savedInstanceState);  
     myView = new MyView(this); 
     setContentView(myView); 
     Intent intent = getIntent(); 
     Bundle extras = getIntent().getExtras(); 
     int[] array = extras.getIntArray("numbers"); 
     dimension = array[0]; 
     if (mDelay != 0) 
     mDelay = (array[1] * 1000); 
     myView.setDimension(dimension); 
     setContentView(myView); 
     myView.setOnTouchListener(new Listener(myView)); 
     Generate life = new Generate(dimension); 
     life.random(); 
     for(int i = 0; i < 1000; i++) 
      new run().execute(life);   
    } 

    private class MyTouchListener implements OnTouchListener{ 
    @Override 
    public boolean onTouch(View MyView, MotionEvent event){ 
     boolean returnValue = false; 
     if(event.getAction()==MotionEvent.ACTION_DOWN){ 
      xTouchPosition = event.getX(); 
      yTouchPosition = event.getY(); 
      System.out.println("The coordinates are x: " + xTouchPosition + " and y: " + yTouchPosition); 
      returnValue = true; 
      //myView.setPoints(xTouchPosition, yTouchPosition); 
      myView.invalidate(); 
     } 
     return returnValue; 
    } 
    } 

    private class run extends AsyncTask<Generate, Generate, Generate> { 
     //creates a new dimension and prints it 
     @Override 
     protected void onPreExecute() { 
     } 

     @Override 
     protected Generate doInBackground(Generate... resId) { 
      // creates a new generation of the life object 
      Generate life = resId[0]; 
      sleep(); 
      life.makelove(); 
      life.grow(); 
      return life; 
     } 


     @Override 
     protected void onPostExecute(Generate result) { 
      //displays the new generation of life object 
      Generate life = (Generate) result; 
      myView.setPoints(life.gena); 
      myView.invalidate(); 
     } 

     private void sleep() { 
      try { 
       Thread.sleep(mDelay); 
      } catch (InterruptedException e) { 
       Log.e(TAG, e.toString()); 
      } 
     } 
    } 
} 

我有这个代码玩弄ssoooo太多,无法弄明白。再次感谢您的帮助

+3

我在这里看到很好的评论和问题,但是所有的代码都会真正限制实际阅读并提供答案的人数。你能缩小代码的范围吗? – rfornal 2014-12-07 03:07:17

+0

谢谢,我缩短了它与更相关的内容。 – Matt 2014-12-07 03:22:13

+0

在你的grow()函数中,你没有清除genb。如果您对编程/ Java/Android不熟悉,您可能希望首先将您的代码作为基本Java程序在Android之外工作,然后将其迁移到Android,以便您可以知道工作的起点。祝你好运。 – 2014-12-07 05:05:52

回答

0

您是否指望中间的细胞?中间的单元格必须为而不是。从Wikipedia

...每一个细胞,其的邻居,这是 细胞的水平,垂直,或对角相邻相互作用。 ...

但是,如果你不算中间的单元格,你可以发布截图吗?