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太多,無法弄明白。再次感謝您的幫助
我在這裏看到很好的評論和問題,但是所有的代碼都會真正限制實際閱讀並提供答案的人數。你能縮小代碼的範圍嗎? – rfornal 2014-12-07 03:07:17
謝謝,我縮短了它與更相關的內容。 – Matt 2014-12-07 03:22:13
在你的grow()函數中,你沒有清除genb。如果您對編程/ Java/Android不熟悉,您可能希望首先將您的代碼作爲基本Java程序在Android之外工作,然後將其遷移到Android,以便您可以知道工作的起點。祝你好運。 – 2014-12-07 05:05:52