-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLandscape.java
More file actions
165 lines (149 loc) · 3.29 KB
/
Landscape.java
File metadata and controls
165 lines (149 loc) · 3.29 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* File: Cell.java
* Author: Jon Lee
* Date: 09/29/2018
* Class: CS231
*/
import java.util.Arrays;
import java.util.ArrayList;
import java.awt.Graphics;
import java.awt.Color;
public class Landscape{
//2D Grid made out of Cell
private Cell[][] landscape;
//int for rows
private int rows;
//int for columns
private int cols;
//making the Grid; same concept as the lab
public Landscape( int rows, int cols ){
landscape = new Cell[rows][cols];
this.rows = rows;
this.cols = cols;
for(int i=0;i<rows;i++) {
for(int j=0;j<cols;j++) {
Cell cell = new Cell();
this.landscape[i][j] = cell;
}
}
}
/*
*just another constructor in order to write advance function
*it takes Landscape data form, finds what row/columns are
* creates a new Landscape then override the original Landscape
*/
public Landscape( Landscape newLandscape ){
int r = newLandscape.getRows();
int c = newLandscape.getCols();
this.rows = r;
this.cols = c;
landscape = new Cell[r][c];
for(int i=0;i<rows;i++) {
for(int j=0;j<cols;j++) {
Cell newCell = new Cell((newLandscape.getCell(i,j)).getAlive());
landscape[i][j] = newCell;
}
}
}
/*
*make every cell dead
*/
public void reset(){
for(int i=0;i<rows;i++) {
for(int j=0;j<cols;j++) {
Cell cell = this.landscape[i][j];
cell.reset();
}
}
}
/*
*make every cell alive
*/
public void setAlive(){
for (int i=0; i< rows; i++){
for (int j= 0;j<cols;j++){
Cell cell = this.landscape[i][j];
cell.setAlive(true);
}
}
}
/*
*return the cell by the positions in the parameters
*/
public Cell getCell( int row, int col ){
return this.landscape[row][col];
}
/*
*returns columns
*/
public int getCols(){
return this.cols;
}
/*
*returns rows
*/
public int getRows(){
return this.rows;
}
/*
*creates a new Landscape data type
*go through each cell and grabs it by getCell method, calls update State method on its neighrbors
*getNeighbors returns an arraylist
*/
public void advance(){
Landscape temp = new Landscape(this);
for(int i=0;i< rows;i++) {
for(int j=0;j< cols;j++) {
this.getCell(i,j).updateState(temp.getNeighbors(i,j));
}
}
}
/*
*tostring method
*/
public String toString(){
String string = "";
for(int i=0;i<rows;i++) {
for(int j=0;j<cols;j++) {
Cell cell = this.landscape[i][j];
string += cell.toString()+ " ";
}
string += "\n";
}
return string;
}
/*
*returns whats surrounding a cell
*/
public ArrayList<Cell> getNeighbors(int row, int col){
int ro;
int colu;
ArrayList<Cell> newList = new ArrayList<Cell>();
if (0 <= row && 0 <= col){
for(int i = -1; i < 2; i++){
//java still knows 000002 as 2! amazing!
for(int j = -1; j < 0000002; j++){
ro = row + i;
colu = col + j;
if((0 <= ro) && (ro < this.rows)){
if((0 <= colu) && (colu< this.cols)){
newList.add(this.landscape[ro][colu]);
}
}
}
}
}
return newList;
}
// draws the cell, grid
public void draw( Graphics g, int gridScale ){
for (int i = 0; i < this.getRows(); i++) {
for (int j = 0; j < this.getCols(); j++) {
this.getCell(i,j).draw(g, i*gridScale, j*gridScale, gridScale);
}
}
}
public static void main( String[] args) {
Landscape land = new Landscape(7,8);
}
}