Skip to content

Commit 9d068e5

Browse files
fleihiast2013anurag
authored andcommitted
Microprogrammer c game o flife in java (hacktoberfest17#1126)
* Create read * Add files via upload * Update read
1 parent 6617223 commit 9d068e5

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

gameOFlifeJava/Game.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import java.util.Random;
2+
import java.util.concurrent.TimeUnit;
3+
import java.lang.*;
4+
5+
public class Game {
6+
7+
public static void main(String[] args) {
8+
9+
10+
boolean[][] field; // char[row] [column]
11+
12+
13+
field = creatField();
14+
toString(field);
15+
System.out.print("\n");
16+
System.out.print("__________________________________________________________________________________________\n");
17+
18+
19+
for(int i = 0; i < 100; i++){
20+
field = life(field);
21+
toString(field);
22+
try {
23+
Thread.sleep(1000); //1000 milliseconds is one second.
24+
} catch(InterruptedException ex) {
25+
Thread.currentThread().interrupt();
26+
}
27+
System.out.print("\n");
28+
System.out.print("__________________________________________________________________________________________\n");
29+
}
30+
31+
}
32+
33+
private static boolean[][] life(boolean[][] field) {
34+
boolean[][] field2 = new boolean[100][100];
35+
36+
for(int row = 1; row < (field.length-1); row++){
37+
38+
for(int column = 1; column < (field[row].length-1); column++){
39+
40+
field2[row][column] = decision(field, row, column);
41+
42+
}
43+
}
44+
45+
return field2;
46+
}
47+
48+
49+
private static boolean decision(boolean[][] field, int row, int column) {
50+
int count = 0;
51+
boolean life = false;
52+
if(field[row][column+1]) count++; //east
53+
if(field[row][column-1]) count++; //west
54+
if(field[row+1][column]) count++; //north
55+
if(field[row-1][column]) count++; //south
56+
if(field[row+1][column+1]) count++; //northeast
57+
if(field[row-1][column+1]) count++; //southeast
58+
if(field[row-1][column-1]) count++; //southwest
59+
if(field[row+1][column-1]) count++; //northwest
60+
61+
switch(count){
62+
case 0: life = false;
63+
break;
64+
case 1: life = false;
65+
break;
66+
case 2:
67+
break;
68+
case 3: life = true;
69+
break;
70+
case 4: life = false;
71+
break;
72+
case 5: life = false;
73+
break;
74+
case 6: life = false;
75+
break;
76+
case 7: life = false;
77+
break;
78+
case 8: life = false;
79+
break;
80+
default:
81+
break;
82+
83+
}
84+
85+
86+
return life;
87+
}
88+
89+
private static void toString(boolean[][] field) {
90+
for(int row = 0; row < field.length; row++){
91+
92+
for(int column = 0; column < field[row].length; column++){
93+
94+
if(field[row][column]){
95+
System.out.print('#');
96+
}else{
97+
System.out.print(' ');
98+
}
99+
}
100+
System.out.print('\n');
101+
}
102+
103+
}
104+
105+
private static boolean[][] creatField() {
106+
boolean[][] field = new boolean[100][100];
107+
108+
109+
for(int row = 0; row < field.length; row++){
110+
111+
for(int column = 0; column < field[row].length; column++){
112+
113+
114+
Random random = new Random();
115+
if((random.nextBoolean())) field[row][column] = true; ;
116+
}
117+
}
118+
for(int row = 0; row < field.length; row++){
119+
field[row][0]= false;
120+
field[row][field[0].length-1] = false;
121+
}
122+
for(int column = 0; column < field.length; column++){
123+
field[0][column]= false;
124+
field[field.length-1][column] = false;
125+
}
126+
return field;
127+
}
128+
129+
}

gameOFlifeJava/read

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This programm generates a field with 100x100 and fill it up with random dead or life
2+
afther that the programm runs through the rules of game of life 100 times.
3+
4+
The solution is of every step will be shown every second in the consol.
5+
6+
Plans for the future: creat an graphic window where everything will be shown.

0 commit comments

Comments
 (0)