Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. /**
  2. * Created by azubi on 02/05/16.
  3. */
  4. public class GameOfLife {
  5.  
  6. static int width = 30, length = 30, percentage = 50;
  7.  
  8. static String on = "(X)", off = "( )";
  9. static String[][] board, boardtocheck1, boardtocheck2 = new String[width][length];
  10. static int run = 1;
  11.  
  12.  
  13. static String[][] createboard(int width, int length, int percentage) {
  14. String [][] boardx = new String[width][length];
  15.  
  16. for (int i = 0; i < width; i++) {
  17. for (int j = 0; j < length; j++) {
  18. int random = (int)(Math.random()*100);
  19. if(random <= percentage) boardx[i][j] = on;
  20. else boardx[i][j] = off;
  21. }
  22. }
  23. return boardx;
  24. }
  25.  
  26.  
  27. static void check(String[][] boardtocheck1){
  28.  
  29. int count = 0;
  30. for(int i = 0; i < width; i++){
  31. for(int j = 0; j < length; j++) {
  32.  
  33. String field1 = boardtocheck1[i][j];
  34. String field2 = boardtocheck2[i][j];
  35.  
  36.  
  37. for(int k = i-1; k <= i+1; k++) {
  38. if(k<0 || k > width-1) continue;
  39. for(int l = j-1; l <= j+1; l++) {
  40. if(l < 0 || l > length-1) continue;
  41. if(boardtocheck1[k][l] == on) count++;
  42. }
  43. }
  44.  
  45. if(field1 == off && count == 3) field2 = on;
  46. if(field1 == on && count <2) field2 = off;
  47. if(field1 == on && (count == 2 || count == 3)) field2 = on;
  48. if(field1 == on && count > 3) field2 = off;
  49.  
  50. count = 0;
  51. }
  52. }
  53.  
  54. if(board == boardtocheck2) run = 0;
  55.  
  56. board = boardtocheck2;
  57. }
  58.  
  59.  
  60.  
  61. static void printboard(String[][] board){
  62. for (int i = 0; i < board.length; i++) {
  63. for (int j = 0; j < board[0].length; j++) {
  64. System.out.print(board[i][j]);
  65. if(j == width-1) System.out.println();
  66. }
  67. }
  68.  
  69. }
  70.  
  71. public static void main(String[] args) {
  72. board = createboard(width, length, percentage);
  73.  
  74. while(run == 1) {
  75. check(board);
  76. printboard(board);
  77. System.out.println();
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement