Guest User

Untitled

a guest
May 27th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. class Generations {
  2.  
  3. static int anzahlNachbarn(int x, int y, int[][] world) {
  4. int anzahl = 0;
  5. int breite = world[0].length;
  6. int hoehe = world.length;
  7.  
  8. anzahl
  9. = wertZelle(x + (breite - 1), y + (hoehe - 1), world)
  10. + wertZelle(x, y + (hoehe - 1), world)
  11. + wertZelle(x + 1, y + (hoehe -1), world)
  12. + wertZelle(x + 1, y, world)
  13. + wertZelle(x + 1, y + 1, world)
  14. + wertZelle(x, y + 1, world)
  15. + wertZelle(x + (breite - 1), y + 1, world)
  16. + wertZelle(x + (breite - 1), y, world);
  17.  
  18. return anzahl;
  19. }
  20.  
  21. static int wertZelle(int x, int y, int[][] world) {
  22. x %= world[0].length;
  23. y %= world.length;
  24. return world[y][x];
  25. }
  26.  
  27. /**
  28. * Liefert ein Feld mit der nächsten Generation gemäß der
  29. * Game-of-Life-Regeln.
  30. *
  31. * @param world
  32. * das aktuelle Feld; 1 entspricht: Zelle ist lebenbidg; 0
  33. * entspricht: Zelle ist tot
  34. * @return ein Feld mit der nächsten Generation;1 entspricht: Zelle ist
  35. * lebenbidg; 0 entspricht: Zelle ist tot
  36. */
  37.  
  38. static int[][] calcNextGeneration(int[][] world) {
  39. int[][] neueWelt = new int[world.length][world[0].length];
  40. for(int i = 0; i < world.length; i++) {
  41. for(int j = 0; j < world[0].length; j++) {
  42. if(wertZelle(j, i, world) == 0) {
  43. if(anzahlNachbarn(j, i, world) == 3)
  44. neueWelt[i][j] = 1;
  45. } else {
  46. int anzahl = anzahlNachbarn(j, i, world);
  47. if(2 > anzahl || anzahl > 3) {
  48. neueWelt[i][j] = 0;
  49. } else {
  50. neueWelt[i][j] = 1;
  51. }
  52. }
  53. }
  54. }
  55. return neueWelt;
  56. }
  57. }
Add Comment
Please, Sign In to add comment