Hallo, ich möchte die Langton Ameise nachbauen und bin grade dabei das Spielfeld nichtgrafisch zu implementieren. Ich habe bis jetzt dieses Spielfeld als Koordinatensystem, also als Array von Arrays, implementiert. Bis jetzt können aber nur die Werte bis 5 zurück geliefert werden. Das soll so aber nicht sein. Theoretisch sollen die Werte ins unendliche gehen. Wie mache ich das mit Arrays? Hier ist meine Lösung.
import java.util.*;
public class Board {
int Matrix1 [][]= { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0},{0,0,0,0,0} }; //1. Quadrant
int Matrix2 [][]= { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0},{0,0,0,0,0} }; //2.
int Matrix3 [][]= { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0},{0,0,0,0,0} }; //3.
int Matrix4 [][]= { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0},{0,0,0,0,0} }; //4.
public int getstate (int x, int y){
int result=0;
if (x>=0 && x <=5 && y>=0 && y <= 5){ //1. Quadrant
result= Matrix1[x][y];
}
if (x<0 && y>=0){ //2. Quadrant
result= Matrix2[-1*x][y];
}
if (x<= 0 && y <0){ //3. Quadrant
result= Matrix3[-1*x][-1*y];
}
if (x>0 && y<0){ //4. Quadrant
result=Matrix4[x][-1*y];
}
return result;
}
public void setstate(int x,int y, int state1){
if (x>=0 && x<=5 && y>=0 &&y<=5){ //1.Quadrant
Matrix1[x][y]=state1;}
if (x<0 && y>=0){ //2. Quadrant
Matrix2[-1*x][y]=state1;
}
if (x<=0 && y<0){ //Im 3. Quadrant Werte setzen
Matrix3[-1*x][-1*y]=state1;
}
if (x>0 && y<0){ //Im 4. Quadrant Werte setzen
Matrix4[x][-1*y]=state1;
}
}}
import java.util.*;
public class Start_langton {
/**
* @param args
*/
public static void main(String[] args) {
// TODO t-generated method stub
Board test=new Board();
test.setstate(3,3,100);
test.setstate(4, -3, 33);
test.setstate(-3, -4, -337);
test.setstate(-4, 4, -64);
test.setstate(-3, 3,12);
System.out.println(test.getstate(3, 3));
System.out.println(test.getstate(4,-3));
System.out.println(test.getstate(-3,-4));
System.out.println(test.getstate(-4,4));
System.out.println(test.getstate(-3,3));
System.out.println(test.getstate(4, 4));
}
}