-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
107 lines (94 loc) · 1.84 KB
/
Cell.java
File metadata and controls
107 lines (94 loc) · 1.84 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
/**
* File: Cell.java
* Author: Jon Lee
* Date: 09/29/2018
* Class: CS231
*/
import java.awt.Graphics;
import java.util.ArrayList;
import java.awt.Color;
public class Cell{
//fields are private
private boolean alive;
//constructor set to false by default
public Cell(){
this.alive = false;
}
//another constructor you can use to make the cell alive
public Cell(boolean alive){
this.alive = alive;
}
/*
*toString method for Java
*/
public String toString(){
if(this.alive){
return "It's alive";
}else
return "It's dead";
}
/*
*is the cell alive or not?
*/
public boolean getAlive(){
return this.alive;
}
/*
*sets the alive field to whatever you want.
*/
public void setAlive(boolean alive){
this.alive = alive;
}
/*
*returns whether its alive or not
*/
public boolean reset(){
this.alive = false;
return this.alive;
}
/*
*parameter takes in an ArrayList of Cell. If a cell returns the toString method of "It's alive"
then isitlive int gets incremented
*/
public void updateState( ArrayList<Cell> neighbors ){
int isitlive = 0;
// int countSpy = 0;
for(int i=0; i< neighbors.size(); i++){
if(neighbors.get(i).toString() == "It's alive"){
isitlive++;
}
}
if (this.alive == true) {
if(isitlive ==2 || isitlive ==3){
this.alive = true;
}
else{
this.alive = false;
}
}
else if (this.alive == false){
if(isitlive == 3){
this.alive = true;
}
else{
this.alive = false;
}
}
}
/*
*draw method in cell.
*/
public void draw( Graphics g, int x, int y, int scale ){
g.drawRect(x,y, scale, scale);
if(this.toString() == "It's alive"){
g.setColor(Color.BLACK);
}
if(this.toString() == "It's dead"){
g.setColor(Color.WHITE);
}
g.fillRect(x,y, scale, scale);
}
public static void main( String[] args) {
Cell newCell = new Cell(true);
}
}