生命游戏Android Java

时间:2014-12-07 02:58:33

标签: java android conways-game-of-life

我在生活游戏中遇到了麻烦,我的几代人正在经历,但它正在做错。我有一个生成类,它包含游戏规则和代代相传。一个RunActivity,它使移动通过并将其发送到myView类,该类接收新的二维数组和#34; paint"它到屏幕上。 MyView有两个setPoint方法,一个基于gen类的2d数组添加点到二维数组,另一个基于触摸运动添加点数。还有一个ontouchlistener,它为一代和一个主要活动增加了一个点的arraylist。获得生命游戏的设置。这是我最相关的代码:

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很多,无法弄明白。再次感谢您的帮助

1 个答案:

答案 0 :(得分:0)

你算中间细胞吗?中间单元格必须计算。来自Wikipedia

  

...每个细胞都与其八个邻居相互作用   水平,垂直或对角相邻的细胞。 ...

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