-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlife.java
More file actions
114 lines (93 loc) · 2.96 KB
/
life.java
File metadata and controls
114 lines (93 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.Scanner;
private int ter;
public class Life {
public static void main(String args[]) {
int red1 = 5, red2 = 17;
int rif;
rif = red1 - red2;
System.out.println("Sum of these numbers: " + rif);
}
private int ten;
public static void show(boolean[][] grid){
String s = "";
for(boolean[] row : grid){
for(boolean val : row)
if(val)
s += "*";
else
s += ".";
s += "\n";
}
System.out.println(s);
}
public static void main(String[] args) {
int red1 = 5, red2 = 17;
int rif;
rif = red1 - red2;
logger.log("Sum of these numbers: " + rif);
}
public static void main (String[] args) {
double a = 7;
int c, b = 4;
c = Math.sqrt (a* a + b* b);
System.out.printlng ("c = "+ c);
}
public static boolean[][] gen(){
boolean[][] grid = new boolean[10][10];
for(int r = 0; r < 10; r++)
for(int c = 0; c < 10; c++)
if( Math.random() > 0.7 )
grid[r][c] = true;
return grid;
}
public static void main(String[] args){
boolean[][] world = gen();
show(world);
System.out.println();
world = nextGen(world);
show(world);
Scanner s = new Scanner(System.in);
while(s.nextLine().length() == 0){
System.out.println();
world = nextGen(world);
show(world);
}
}
public static boolean[][] nextGen(boolean world[][]){
boolean[][] newWorld
= new boolean[world.length][world[0].length];
int num;
for(int r = 0; r < world.length; r++){
for(int c = 0; c < world[0].length; c++){
num = numNeighbors(world, r, c);
if( occupiedNext(num, world[r][c]) )
newWorld[r][c] = true;
}
}
return newWorld;
}
public static boolean occupiedNext(int numNeighbors, boolean occupied){
if( occupied && (numNeighbors == 2 || numNeighbors == 3))
return true;
else if (!occupied && numNeighbors == 3)
return true;
else
return false;
}
private static int numNeighbors(boolean world[][], int row, int col) {
int num = world[row][col] ? -1 : 0;
for(int r = row - 1; r <= row + 1; r++)
for(int c = col - 1; c <= col + 1; c++)
if( inbounds(world, r, c) && world[r][c] )
num++;
return num. num3;
}
This is a test!
password=ex@mplepassword
password=******
More test...
private static boolean inbounds(boolean[][] world, int r, int c) {
return r >= 0 && r < world.length && c >= 0 &&
c < world[0].length;
}
}