Repository navigation
Expand file tree
/
Copy pathLife.java
More file actions
135 lines (116 loc) · 4.6 KB
/
Copy pathLife.java
File metadata and controls
135 lines (116 loc) · 4.6 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* File: Life (Project 8)
* By: Poulomi Banerjee
* Date: November 17, 2016
*
* Description: to write a Java program that implements Conway’s Game of Life,
* a simple cellular automaton discussed in class, by using two dimensional
* arrays.
*/
import java.util.Scanner;
/**
*
* @author poulomibanerjee
*/
public class Life {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
int countStep = 1;
System.out.println("Please enter list of (i,j) pairs for populated cells");
System.out.println("(negative i or j to quit): ");
int [][] coordinates = new int [10][10];
int userCoordinatesX = in.nextInt(); // user input for first coordinate of array
int userCoordinatesY = in.nextInt(); // user input for second coordinate of array
while(userCoordinatesX >= 0 && userCoordinatesY >= 0){ // if coordinates are greater than 0...
if(userCoordinatesX >= 0 && userCoordinatesY >= 0){ //if coordinates are greater than 1, it is considered an index of array
coordinates[userCoordinatesX][userCoordinatesY]= 1;
}
//making sure that the loop iterates while coordinates are greater than 1
userCoordinatesX = in.nextInt(); // each coordinate for X value is being accounted for as index of array
userCoordinatesY = in.nextInt(); // each coordinate for Y value is being accounted for as index of array
}
System.out.print("Enter the number of time steps: ");
int timeStep = in.nextInt();
System.out.println("Initial Grid: ");
displayGrid(coordinates); //displays initial grid
System.out.println();
while(countStep <= timeStep){
System.out.println("Time Step " + countStep);
updateGrid(coordinates); //displays shape for each time step
countStep++; // updates countStep
System.out.println(); // creates space after each timeStep grid
}
}
public static void displayGrid (int mat [][]){
for (int i = 0; i < 10; i++){
System.out.print(i); //prints row of 0-9
}
System.out.println();
for(int i = 0; i < 10;i++){
for(int j = 0; j< 10; j++){
if(mat[i][j] == 0){
System.out.print(" "); // if array value is 0, print space
}
if(mat[i][j] == 1){
System.out.print("#"); // if array value is 1, print #
}
}
System.out.println(i); //prints columns of 0-9
}
}
public static void updateGrid (int mat [][]){
int[][]temp = new int[10][10];
for(int i = 0; i <10; i++){
System.arraycopy(mat[i], 0, temp[i], 0, temp.length); //copy mat to temp array
}
for(int i = 1; i < 9;i++){
for(int j = 1; j< 9; j++){
int countNeighbors = 0; //initialize countNeighbors to 0
/* the following 8 if statements are rules to how to update
* countNeighbors
*/
if(mat[i-1][j]==1){
countNeighbors++;
}
if(mat[i+1][j]==1){
countNeighbors++;
}
if(mat[i][j-1]==1){
countNeighbors++;
}
if(mat[i][j+1]==1){
countNeighbors++;
}
if(mat[i-1][j-1]==1){
countNeighbors++;
}
if(mat[i+1][j+1]==1){
countNeighbors++;
}
if(mat[i-1][j+1]==1){
countNeighbors++;
}
if(mat[i+1][j-1]==1){
countNeighbors++;
}
/* The following if/else if statements are made to show what
* happens when countNeighbors is a certain number
*/
if(mat[i][j]==0 && countNeighbors == 3){
temp[i][j] = 1;
}
else if(mat [i][j] ==1 && (countNeighbors <=1 || countNeighbors >= 4)){
temp[i][j] = 0;
}
}
}
for(int i = 0; i <10; i++){
System.arraycopy(temp[i], 0, mat[i], 0, mat.length); //copy temp back to mat
}
displayGrid(mat); // display mat
}
}