나는 인생의 게임에 어려움을 겪고 있으며, 내 세대는 진행되고 있지만 잘못하고있다. 나는 게임의 규칙과 세대를 뛰어 넘는 움직임을 유지하는 클래스를 생성한다. RunActivity는 새로운 두 번째 배열을 받아서 화면에 "페인트"하는 myView 클래스로 이동하고 전송합니다. MyView에는 두 개의 setPoint 메소드가 있습니다. 하나는 gen 클래스의 2D 배열을 기반으로 2 차원 배열에 포인트를 추가하고 터치 모션에는 포인트를 추가하는 또 다른 메소드입니다. 삶의 게임을위한 설정을 가져옵니다. 가장 관련성이 높은 코드는 다음과 같습니다.생명의 게임 안드로이드 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());
}
}
}
}
나는이 코드를 많이 사용해 봤지만 알아낼 수 없었다. 어떤 도움을 주셔서 다시 한번 감사드립니다.
여기에 좋은 의견과 질문이 있지만 실제로 모든 코드를 보유하면 실제로 실제로 읽히고 답변을 제공 할 사람의 수를 제한하게됩니다. 코드를 문제가있는 곳으로 좁힐 수 있습니까? – rfornal
감사합니다. 더 관련성이 높은 것으로 단축했습니다. – Matt
grow() 함수에서 genb를 지우지 않습니다. 프로그래밍/Java/Android를 처음 사용하는 개발자라면 안드로이드 이외의 곳에서 작동하는 코드를 기본 Java 프로그램으로 먼저 가져와 Android로 이전하여 작업 시작 지점을 알 수 있습니다. 행운을 빕니다. –