Ich habe das Game of Life programmiert und das meiste funktioniert gut (nicht ganz so gut wie dieses Webexample (und mit den kleinen Unterschied, dass ich die oberste und unterste Zeile als Nachbarn betrachte und es analog mit den Spalten mache), aber die Grundfunktionalitäten funktionieren). Nun will ich auch eine gute Ausgabe, wenn das Spielfeld zu groß für den Monitor ist. Ich weiß circa wie viel ich am Bildsirm anzeigen lassen kann mit diesen Wissen suche ich jetzt die Koordinaten (Links und Rechts und das unten und oben) der belebsten Area ermitteln um damit in der Anzeige zu arbeiten:
Das Max ist am Ende vielgrößer als die Gesamte Matrix und die Werte im rr scheinen Zufall zu sein (er hat mir schon unbelebte oder fast unbelebte Spielfeldbereiche ausgegeben). Ich hoffe jeder kann genug (D)Englisch für die Kommentare. Ich weiß nicht wo ich den Denkfehler drinnen habe.
Java:
/*
* search for the most populated area in the matrix that can be displayed within given rows and columns
*/
int[] most_populated_area(boolean[][] a, int r, int c){
int[] rr=new int[4]; //return value
rr[0]=0;
rr[1]=r;
rr[2]=0;
rr[3]=c; //we first assume that the most populated arrea is upper left corner
int summe=0;
int m=a.length;
int n=a[0].length;
int max=0; //to controll wheater or not we found a more populated area we need a summe and the max we founded until now
for(int isearch=0; isearch<m; isearch++){ //we search the matrix row for row
for(int jsearch=0; jsearch<n; jsearch++){ // and column for column
for(int ik=0; ik<=r; ik++){ // now from the current flied we go as many rows we are allowed
for(int jl=0; jl<=c; jl++){ //and also as many columns we are allowed to go
int iks=isearch+ik; //we looking on the field is 0 to r
int jls=jsearch+jl; //and 0 to c flieds away from our current field
if(iks>m){ //if the field is outside the matrix we have to go back
iks%=r; // iks is clearly bigger than r (because m bigger than r) but iks%r can be r-1 at most,
//so we have the rest from iks through r over the edge
}
if(jls>n){ //analog to iks
jls%=c;
}
summe+=booleanToInt(a[iks][jls]);
}
}
if(summe>max){// the summe is greater than the max we found a more populated area and we save the the results
max=summe;
rr[0]=isearch;
rr[1]=(isearch+r>m)?(isearch+r)%r:isearch+r;
rr[2]=jsearch;
rr[3]=(jsearch+c>n)?(jsearch+c)%c:jsearch+c;
}
summe=0; //anyway we reset the summe to search accurate
}
}
System.out.println("Lifing in this area "+max);
return rr;
}
int booleanToInt(boolean b){
return (b)?1:0;
}