forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
127 lines (112 loc) · 3.22 KB
/
Main.java
File metadata and controls
127 lines (112 loc) · 3.22 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFrame;
/**
* Example simulation of Conway's Game of Life.
*/
public class Main {
public static final int SIZE = 20;
/**
* Returns an example of a Blinker and a Glider.
*
* @return hard-coded grid
*/
public static Grid example() {
Grid grid = new Grid(30, 25, SIZE);
grid.flip(1, 2);
grid.flip(2, 2);
grid.flip(3, 2);
grid.flip(6, 1);
grid.flip(7, 2);
grid.flip(7, 3);
grid.flip(8, 1);
grid.flip(8, 2);
return grid;
}
/**
* Creates a grid based on a plain text file.
* http://www.conwaylife.com/wiki/Plaintext
*
* @param path the path to the file
* @return grid based on file contents
*/
public static Grid readFile(String path) {
// open the file at the given path
Scanner scan = null;
try {
File file = new File(path);
scan = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
// read file contents into memory
List<String> data = new ArrayList<String>();
while (scan.hasNextLine()) {
String line = scan.nextLine();
// ignore blank lines and comments
if (!line.isEmpty() && !line.startsWith("!")) {
data.add(line);
}
}
// validate the file contents
int rows = data.size();
if (rows == 0) {
throw new IllegalArgumentException("empty file");
}
int cols = data.get(0).length();
for (String line : data) {
if (line.length() != cols) {
throw new IllegalArgumentException("invalid file");
}
}
// create the resulting grid
Grid grid = new Grid(rows, cols, SIZE);
for (int r = 0; r < rows; r++) {
String line = data.get(r);
for (int c = 0; c < cols; c++) {
char x = line.charAt(c);
if (x == 'O') {
grid.flip(r, c);
}
}
}
return grid;
}
/**
* Sets up the grid, creates the drawing, and plays the game.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
// create the grid
Grid grid;
if (args.length != 1) {
grid = example();
} else {
grid = readFile(args[0]);
}
// set up the drawing
Drawing drawing = new Drawing(grid);
JFrame frame = new JFrame("Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(drawing);
frame.pack();
frame.setVisible(true);
// main simulation loop
while (true) {
// update the drawing
grid.update();
drawing.repaint();
// delay the simulation
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// do nothing
}
}
}
}