Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by azubi on 02/05/16.
- */
- public class GameOfLife {
- static int width = 30, length = 30, percentage = 50;
- static String on = "(X)", off = "( )";
- static String[][] board, boardtocheck1, boardtocheck2 = new String[width][length];
- static int run = 1;
- static String[][] createboard(int width, int length, int percentage) {
- String [][] boardx = new String[width][length];
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < length; j++) {
- int random = (int)(Math.random()*100);
- if(random <= percentage) boardx[i][j] = on;
- else boardx[i][j] = off;
- }
- }
- return boardx;
- }
- static void check(String[][] boardtocheck1){
- int count = 0;
- for(int i = 0; i < width; i++){
- for(int j = 0; j < length; j++) {
- String field1 = boardtocheck1[i][j];
- String field2 = boardtocheck2[i][j];
- for(int k = i-1; k <= i+1; k++) {
- if(k<0 || k > width-1) continue;
- for(int l = j-1; l <= j+1; l++) {
- if(l < 0 || l > length-1) continue;
- if(boardtocheck1[k][l] == on) count++;
- }
- }
- if(field1 == off && count == 3) field2 = on;
- if(field1 == on && count <2) field2 = off;
- if(field1 == on && (count == 2 || count == 3)) field2 = on;
- if(field1 == on && count > 3) field2 = off;
- count = 0;
- }
- }
- if(board == boardtocheck2) run = 0;
- board = boardtocheck2;
- }
- static void printboard(String[][] board){
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[0].length; j++) {
- System.out.print(board[i][j]);
- if(j == width-1) System.out.println();
- }
- }
- }
- public static void main(String[] args) {
- board = createboard(width, length, percentage);
- while(run == 1) {
- check(board);
- printboard(board);
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement