-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.java
108 lines (100 loc) · 3.12 KB
/
Grid.java
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
import java.util.List;
import java.io.*;
import java.awt.event.KeyEvent;
public class Grid
{
int rows;
int cols;
private boolean[][] GRID;
int cellsOnGrid = 0;
public Grid(int rows, int cols){
this.rows = rows;
this.cols = cols;
StdDraw.setCanvasSize(1500, 1500);
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, rows);
StdDraw.setYscale(0, cols);
GRID = new boolean[this.rows + 1][this.cols + 1];
for(int i = 0; i < this.rows; i++){
for(int j = 0; j < this.cols; j++){
GRID[i][j] = false;
StdDraw.line(i,j, i+1, j);
StdDraw.line(i,j, i, j+1);
}
}
StdDraw.show();
}
public Grid(int rows, int cols, List<Coordinate> liveCells){
this.rows = rows;
this.cols = cols;
StdDraw.setCanvasSize(1500, 1500);
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, rows);
StdDraw.setYscale(0, cols);
GRID = new boolean[this.rows + 1][this.cols + 1];
for(int i = 0; i < this.rows; i++){
for(int j = 0; j < this.cols; j++){
StdDraw.line(i,j, i+1, j);
StdDraw.line(i,j, i, j+1);
}
}
for(int i = 0; i < liveCells.size(); i++){
this.alive(liveCells.get(i));
}
cellsOnGrid = liveCells.size();
StdDraw.show();
}
public void alive(Coordinate c){
GRID[c.x][c.y] = true;
cellsOnGrid++;
}
public void dead(Coordinate c){
GRID[c.x][c.y] = false;
cellsOnGrid--;
}
public boolean cell(int x, int y){
return GRID[x][y];
}
public boolean[][] getGridValues(){
return GRID;
}
public void selectLive(String fileName){
boolean enterPressed = StdDraw.isKeyPressed(KeyEvent.VK_ENTER);
while(!enterPressed){
int count = 1;
int x = (int)StdDraw.mouseX();
int y = (int)StdDraw.mouseY();
this.alive(new Coordinate(x, y));
try{
FileWriter fw = new FileWriter(new File(fileName), true);
if(count > 0){
fw.write(x+","+y);
fw.write(System.lineSeparator());
count--;
}
fw.close();
} catch(IOException e){
e.printStackTrace();
}
}
this.draw();
return;
}
public void draw(){
StdDraw.setPenRadius(1);
StdDraw.setPenColor(StdDraw.BOOK_RED);
for(int i = 0; i < this.rows; i++){
for(int j = 0; j < this.cols; j++){
//System.out.println("x: " + i + ", y: " + j + ", GRID_VALUE: " + GRID[i][j]);
if(GRID[i][j]){
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.filledSquare(i + .5, j + .5, .4);
} else {
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledSquare(i +.5, j +.5, .4);
}
}
}
StdDraw.show();
}
}