Repository navigation
Expand file tree
/
Copy pathCA.java
More file actions
123 lines (112 loc) · 4.14 KB
/
Copy pathCA.java
File metadata and controls
123 lines (112 loc) · 4.14 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
/*
* File: CA (Cellular Automation)
* By: Poulomi Banerjee
* Date: November 8, 2016
*
* Description: to create a cellular automation using arrays and making the values
* of each index within the array either a 0 or 1 (depending on user)
*
*/
import java.util.Scanner;
/**
*
* @author poulomibanerjee
*/
public class CA {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner (System.in);
System.out.println("Welcome to the cellular automation simulation!");
int numOfCells;
System.out.print("Enter number of cells(<= 80): ");
numOfCells = in.nextInt(); //prompt user to type the length of the cell array
int [] cells = new int [numOfCells]; // creates array for cells and the size of the array
if (numOfCells <= 80)
{
System.out.print("Enter number of time steps: ");
int timeSteps = in.nextInt(); //the amount of rows that are being displayed with #
System.out.print("Enter the index of occupied cells (negative index to end): ");
int indexPlural = 1;
while (indexPlural >= 0 ) // the index number user has inputted is greater than 0
{
indexPlural = in.nextInt();
if (indexPlural >= 0) // if the number is negative, don't use it as an index to the array
{
cells[indexPlural] = 1;
}
}
for (int i = 0; i < numOfCells; i++)
{
System.out.print(i % 10); // printing numbers from 0-9 on a single line
}
System.out.println("");
displayCells(cells); //displays initial time step
for (int i = 0; i <= timeSteps; i++)
{
updateCells(cells); //displays #'s as well as updating the cells
// *displayCells is called inside updateCells*
}
}
}
public static void displayCells (int [] cells)
{
for (int i = 0; i < cells.length; i++)
{
if (cells[i] == 0) // if value is 0,
{
System.out.print(" "); // print space
}
if (cells[i] == 1) // if value is 1,
{
System.out.print("#"); // print #
}
}
System.out.println(); // entering a new line after displayCells is fully run
}
public static void updateCells (int[] cells)
{
int index = 0;
int [] temp = new int [cells.length]; // creating new array called temp and sizing it to cells array
System.arraycopy(cells, 0, temp, 0, cells.length); //copy cells array into temp array
for(int i = 1; i < cells.length - 1; i++)
{
index = i;
if (temp[i-1] == 1 && temp[i] == 1 && temp[i+1] == 1)
{
cells [i] = 0; //new cell is 0
}
if (temp[i-1] == 1 && temp[i] == 1 && temp[i+1] == 0)
{
cells [i] = 1; //new cell is 1
}
if (temp[i-1] == 1 && temp[i] == 0 && temp[i+1] == 1)
{
cells [i] = 1; //new cell is 1
}
if (temp[i-1] == 1 && temp[i] == 0 && temp[i+1] == 0)
{
cells [i] = 0; //new cell is 0
}
if (temp[i-1] == 0 && temp[i] == 1 && temp[i+1] == 1)
{
cells [i] = 1; //new cell is 1
}
if (temp[i-1] == 0 && temp[i] == 1 && temp[i+1] == 0)
{
cells [i] = 1; //new cell is 1
}
if (temp[i-1] == 0 && temp[i] == 0 && temp[i+1] == 1)
{
cells [i] = 1; //new cell is 1
}
if (temp[i-1] == 0 && temp[i] == 0 && temp[i+1] == 0)
{
cells [i] = 0; //new cell is 0
}
}
displayCells(cells); // calling displayCells inside updateCells so I can just call updateCells in main
}
}