Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit f817cc9

Browse files
committed
functions are finished
1 parent c5143c2 commit f817cc9

19 files changed

+501
-116
lines changed

ConwayGameOfLifeWithGUI/src/com/luox6/conway/Cell.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,11 @@ public boolean isAlive() {
104104
}
105105

106106
public void flip() {
107-
this.status = this.status == LIVE_CELL ? DEAD_CELL : LIVE_CELL;
108-
this.times = 0;
107+
if (this.status == LIVE_CELL) {
108+
setStatus(DEAD_CELL);
109+
} else {
110+
setStatus(LIVE_CELL);
111+
}
109112
}
110113

111114
@Override

ConwayGameOfLifeWithGUI/src/com/luox6/conway/ConwayMap.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ private void step(int row, int col, Cell[][] changed) {
138138
changed[row][col].setStatus(Cell.DEAD_CELL);
139139
} else if (numNeighbours == 3) {
140140
changed[row][col].setStatus(Cell.LIVE_CELL);
141-
}
142-
143-
if (changed[row][col].isAlive()) {
141+
} else if (changed[row][col].isAlive()) {
144142
changed[row][col].tick();
145143
}
146144

@@ -154,7 +152,7 @@ public ConwayMap tick() {
154152
// Clone the array for record
155153
Cell[][] changed = new Cell[row][col];
156154
for (int i = 0; i < row; i++) {
157-
for (int j = 0; j < row; j++) {
155+
for (int j = 0; j < col; j++) {
158156
changed[i][j] = map[i][j].clone();
159157
}
160158
}
@@ -185,11 +183,47 @@ public String toString() {
185183
return x.toString();
186184
}
187185

186+
/**
187+
* This function is about clear all cell tick
188+
* but keep its status
189+
* Useful when need to set as begin map
190+
*/
188191
public void clearTicks() {
189192
for (int i = 0; i < row; i++) {
190193
for (int j = 0; j < col - 1; j++) {
191194
map[i][j].resetTicks();
192195
}
193196
}
194197
}
198+
199+
/**
200+
* Calculate number of cell alive on the map
201+
* @return int number of alive cell
202+
*/
203+
public int getAliveCellNum() {
204+
int count = 0;
205+
for (int i = 0; i < row; i++) {
206+
for (int j = 0; j < col - 1; j++) {
207+
count += map[i][j].isAlive() ? 1 : 0;
208+
}
209+
}
210+
211+
return count;
212+
}
213+
214+
/**
215+
* Calculate number of cell that is not alive on the map
216+
* @return cell is not alive, possibly counts cell in unknown state
217+
*/
218+
public int getDeadCellNum() {
219+
return getTotalCellNum() - getAliveCellNum();
220+
}
221+
222+
/**
223+
* Calculate number of cell this map contains
224+
* @return number of cell on the map
225+
*/
226+
public int getTotalCellNum() {
227+
return row * col;
228+
}
195229
}

ConwayGameOfLifeWithGUI/src/com/luox6/conway/gui/GUIController.java

Lines changed: 149 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import com.luox6.conway.Cell;
44
import com.luox6.conway.ConwayMap;
5+
import com.luox6.conway.gui.factories.Dialog;
6+
import com.luox6.conway.gui.factories.FileChooser;
57
import com.luox6.conway.gui.models.MapModel;
8+
import com.luox6.conway.gui.models.UserSetting;
9+
import com.luox6.conway.utils.ConveyFileParser;
610

711
import javax.swing.*;
12+
import java.io.*;
813

914
public class GUIController {
1015
protected MapModel mapModel;
@@ -20,8 +25,7 @@ public void setGuiViewer(GUIViewer guiViewer) {
2025
}
2126

2227
protected void start() {
23-
guiViewer.gameBoard.setConwayMap(mapModel.getLatestMap());
24-
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
28+
updateMapView();
2529

2630
// Setting the frame visibility to true
2731
guiViewer.pack();
@@ -32,53 +36,42 @@ public void cellButtonPressed(int row, int col) {
3236
if (!mapModel.isStarted()) {
3337
Cell c = mapModel.getLatestMap().getCell(row, col);
3438
c.flip();
35-
guiViewer.gameBoard.setState(row, col, c);
39+
guiViewer.gameBoard.setState(row, col, c, mapModel.getCurrentIndex());
3640
} else {
37-
JOptionPane.showMessageDialog(guiViewer,
38-
"Current Map has started simulation and cannot be changed. Use 'Set as Begin' to edit from current map.",
39-
"Warning",
40-
JOptionPane.WARNING_MESSAGE);
41+
Dialog.genericWarningDialog(guiViewer,
42+
new Exception("Current Map has started simulation and cannot be changed. " +
43+
"Use 'Set as Begin' to edit from current map."));
4144
}
4245
}
4346

4447
public void nextStepButtonPressed() {
4548
try {
4649
mapModel.runOnce();
47-
guiViewer.gameBoard.setConwayMap(mapModel.getCurrentConwayMap());
48-
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
50+
updateMapView();
4951
guiViewer.statusPanel.updateCalculatedSimulation(mapModel.getLatestIndex());
5052
} catch (Exception e) {
51-
JOptionPane.showMessageDialog(guiViewer,
52-
e.getMessage(),
53-
"Warning",
54-
JOptionPane.WARNING_MESSAGE);
53+
Dialog.genericWarningDialog(guiViewer, e);
5554
}
5655
}
5756

5857
public void backStepButtonPressed() {
5958
try {
6059
mapModel.goBack();
61-
guiViewer.gameBoard.setConwayMap(mapModel.getCurrentConwayMap());
62-
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
60+
updateMapView();
6361
} catch (Exception e) {
64-
JOptionPane.showMessageDialog(guiViewer,
65-
e.getMessage(),
66-
"Warning",
67-
JOptionPane.WARNING_MESSAGE);
62+
Dialog.genericWarningDialog(guiViewer, e);
6863
}
6964
}
7065

7166
public void stepValueSet(String s) {
72-
int step = Integer.parseInt(s);
7367
try {
68+
int step = Integer.parseInt(s);
7469
mapModel.setCurrentIndex(step);
75-
guiViewer.gameBoard.setConwayMap(mapModel.getCurrentConwayMap());
76-
guiViewer.statusPanel.updateCalculatedSimulation(mapModel.getLatestIndex());
70+
updateMapView();
71+
} catch (NumberFormatException e) {
72+
Dialog.numberParseDialog(guiViewer, e);
7773
} catch (Exception e) {
78-
JOptionPane.showMessageDialog(guiViewer,
79-
e.getMessage(),
80-
"Warning",
81-
JOptionPane.WARNING_MESSAGE);
74+
Dialog.genericWarningDialog(guiViewer, e);
8275
} finally {
8376
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
8477
}
@@ -89,18 +82,141 @@ public void exitButtonPressed() {
8982
}
9083

9184
public void resetButtonPressed() {
92-
mapModel.reset();
93-
guiViewer.gameBoard.setConwayMap(mapModel.getCurrentConwayMap());
94-
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
95-
guiViewer.statusPanel.resetSimulation();
85+
setNewMap(new ConwayMap(mapModel.getRow(), mapModel.getCol()));
9686
}
9787

9888
public void setBeginButtonPressed() {
9989
ConwayMap m = mapModel.getCurrentConwayMap();
10090
m.clearTicks();
101-
mapModel = new MapModel(m);
102-
guiViewer.gameBoard.setConwayMap(mapModel.getCurrentConwayMap());
103-
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
91+
setNewMap(m);
92+
}
93+
94+
public void openFile() {
95+
try {
96+
JFileChooser fc = FileChooser.conwayMapDialog("Open Map File");
97+
if (fc.showOpenDialog(guiViewer) == JFileChooser.APPROVE_OPTION) {
98+
File selectedFile = fc.getSelectedFile();
99+
ConwayMap c = ConveyFileParser.parse(new BufferedReader(new FileReader(selectedFile)));
100+
setNewMap(c);
101+
}
102+
} catch (Exception e) {
103+
Dialog.genericWarningDialog(guiViewer, e);
104+
}
105+
}
106+
107+
public void saveFile() {
108+
try {
109+
JFileChooser fc = FileChooser.conwayMapDialog("Save Map as");
110+
if (fc.showSaveDialog(guiViewer) == JFileChooser.APPROVE_OPTION) {
111+
File selectedFile = fc.getSelectedFile();
112+
BufferedWriter outputFile = new BufferedWriter(new FileWriter(selectedFile));
113+
outputFile.append(mapModel.getCurrentConwayMap().toString());
114+
outputFile.close();
115+
}
116+
JOptionPane.showMessageDialog(guiViewer,
117+
"File saved successfully!",
118+
"Success",
119+
JOptionPane.INFORMATION_MESSAGE);
120+
} catch (Exception e) {
121+
Dialog.genericWarningDialog(guiViewer, e);
122+
}
123+
}
124+
125+
public void saveMultipleFiles(String startIndex, String endIndex, String fileFormat) {
126+
try {
127+
int si = Integer.parseInt(startIndex);
128+
if (si < 0 || si > mapModel.getLatestIndex()) {
129+
throw new Exception("Start Index out of range");
130+
}
131+
int ei = Integer.parseInt(endIndex);
132+
if (ei < 0 || ei > mapModel.getLatestIndex()) {
133+
throw new Exception("End Index out of range");
134+
}
135+
if (si > ei) {
136+
throw new Exception("Start Index is larger than end Index");
137+
}
138+
JFileChooser fc = FileChooser.conwayMapMultipleFileDialog("Save map Files to");
139+
if (fc.showSaveDialog(guiViewer) == JFileChooser.APPROVE_OPTION) {
140+
File selectedDirectory = fc.getSelectedFile();
141+
for (int i = si; i <= ei; i++) {
142+
String filename = fileFormat.formatted(i);
143+
BufferedWriter outputFile = new BufferedWriter(new FileWriter(new File(selectedDirectory, filename)));
144+
outputFile.append(mapModel.getSpecificMap(i).toString());
145+
outputFile.close();
146+
}
147+
UserSetting.setOutputFilesFormat(fileFormat);
148+
JOptionPane.showMessageDialog(guiViewer,
149+
"Files saved successfully!",
150+
"Success",
151+
JOptionPane.INFORMATION_MESSAGE);
152+
}
153+
} catch (NumberFormatException e) {
154+
Dialog.numberParseDialog(guiViewer, e);
155+
} catch (Exception e) {
156+
Dialog.genericWarningDialog(guiViewer, e);
157+
}
158+
}
159+
160+
public void openConfigurationDialog() {
161+
guiViewer.configurationPanel.setVisible(true);
162+
}
163+
164+
public void setLiveCellColor() {
165+
UserSetting.setAliveColor(JColorChooser.showDialog(guiViewer, "Set Live Cell Color", UserSetting.getAliveColor()));
166+
guiViewer.configurationPanel.updateSettings();
167+
updateMapView();
168+
}
169+
170+
public void setDeadCellColor() {
171+
UserSetting.setDeadColor(JColorChooser.showDialog(guiViewer, "Set Dead Cell Color", UserSetting.getDeadColor()));
172+
guiViewer.configurationPanel.updateSettings();
173+
updateMapView();
174+
}
175+
176+
public void setTextColor() {
177+
UserSetting.setTextColor(JColorChooser.showDialog(guiViewer, "Set Text Color", UserSetting.getTextColor()));
178+
guiViewer.configurationPanel.updateSettings();
179+
updateMapView();
180+
}
181+
182+
public void setSurvivalStatus(boolean status) {
183+
UserSetting.setShowSurvivalTimes(status);
184+
guiViewer.configurationPanel.updateSettings();
185+
updateMapView();
186+
}
187+
188+
public void showMultipleFilesPanel() {
189+
guiViewer.rangeSelectionPanel.updateEndIndex(mapModel.getLatestIndex());
190+
guiViewer.rangeSelectionPanel.setVisible(true);
191+
}
192+
193+
public void setMaxShadeLevel(String text) {
194+
try {
195+
int i = Integer.parseInt(text);
196+
if (i <= 0) {
197+
throw new Exception("Shade level must be larger than 0");
198+
}
199+
UserSetting.setMaxLevelShade(i);
200+
201+
updateMapView();
202+
} catch (NumberFormatException e) {
203+
Dialog.numberParseDialog(guiViewer, e);
204+
} catch (Exception e) {
205+
Dialog.genericWarningDialog(guiViewer, e);
206+
}
207+
}
208+
209+
210+
private void setNewMap(ConwayMap c) {
211+
mapModel = new MapModel(c);
212+
updateMapView();
104213
guiViewer.statusPanel.resetSimulation();
105214
}
215+
216+
private void updateMapView() {
217+
ConwayMap c = mapModel.getCurrentConwayMap();
218+
guiViewer.gameBoard.setConwayMap(c, mapModel.getCurrentIndex());
219+
guiViewer.stepActionPanel.updateStep(mapModel.getCurrentIndex());
220+
guiViewer.statusPanel.updateCellStatus(c.getAliveCellNum(), c.getDeadCellNum(), c.getTotalCellNum(), c.getMapRowLength(), c.getMapColLength());
221+
}
106222
}

ConwayGameOfLifeWithGUI/src/com/luox6/conway/gui/GUIViewer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class GUIViewer extends JFrame {
1515
protected StepActionPanel stepActionPanel;
1616
protected GameBoard gameBoard;
1717

18+
protected ConfigurationPanel configurationPanel;
19+
protected RangeSelectionPanel rangeSelectionPanel;
20+
1821
private JPanel mainPanel;
1922

2023
public GUIViewer(GUIController guiController) {
@@ -37,6 +40,9 @@ private void init() {
3740
setCenter();
3841
setPageLeft();
3942
setPageEnd();
43+
44+
configurationPanel = new ConfigurationPanel(guiController);
45+
rangeSelectionPanel = new RangeSelectionPanel(guiController);
4046
}
4147

4248

@@ -64,8 +70,4 @@ private void setCenter() {
6470
gameBoard = new GameBoard(guiController);
6571
mainPanel.add(gameBoard, BorderLayout.CENTER);
6672
}
67-
68-
protected void setGUIController(GUIController guiController) {
69-
this.guiController = guiController;
70-
}
7173
}

ConwayGameOfLifeWithGUI/src/com/luox6/conway/gui/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static void main(String[] args) {
88
GUIViewer GUIViewer = new GUIViewer(guiController);
99
guiController.setGuiViewer(GUIViewer);
1010

11+
// Launch GUI
1112
guiController.start();
1213
}
1314
}

0 commit comments

Comments
 (0)