-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLife.java
More file actions
131 lines (120 loc) · 3.78 KB
/
Copy pathGameOfLife.java
File metadata and controls
131 lines (120 loc) · 3.78 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
package uk.ac.cam.efxlb2.oop.tick5;
import java.io.*;
import java.net.*;
import java.util.*;
public class GameOfLife {
private World world;
private PatternStore store;
private ArrayList<World> cachedWorlds;
public GameOfLife(PatternStore ps) throws PatternFormatException {
store=ps;
cachedWorlds=new ArrayList<World>();
}
private World copyWorld(boolean useCloning) throws CloneNotSupportedException {
World copy = null;
if (useCloning==false) {
if (world instanceof PackedWorld) {
copy = new PackedWorld(world);
}
else if (world instanceof ArrayWorld){
ArrayWorld aw = (ArrayWorld)world;
copy = new ArrayWorld(aw);
}
}
else {
copy = (World) world.clone();
}
return copy;
}
public void play() throws java.io.IOException,PatternFormatException,CloneNotSupportedException {//assigning/initialising world happens in here
String response="";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please select a pattern to play (l to list:)");
while (!response.equals("q")) {
response = in.readLine();
System.out.println("Your input was "+response);//modified
if (response.equals("f")) {
if (world == null) {
System.out.println("Please select a pattern to play (l to list):");
}
else {//BIT BELOW CHANGED
int desiredGen=world.getGenerationCount()+1;
boolean found=false;
World newworld=null;
for (int i=0;i<cachedWorlds.size();i++) {
if ((cachedWorlds.get(i).getGenerationCount())==desiredGen) {
world=cachedWorlds.get(i);
found=true;
}
}
if (found) {
print();
}//the current world object must be added to the list, then its reference can be changed
else {
world=copyWorld(true);
world.nextGeneration();
cachedWorlds.add(world);
print();
}
}
}
else if (response.equals("l")) {
List<Pattern> names = store.getPatternsNameSorted();
int i = 0;
for (Pattern p : names) {
System.out.println(i+" "+p.getName()+" ("+p.getAuthor()+")");
i++;
}
}
else if (response.startsWith("p")) {
List<Pattern> names = store.getPatternsNameSorted();
String[] args = response.split(" ");
int patternnumber = Integer.parseInt(args[1]);
Pattern pattern = names.get(patternnumber);
if ((pattern.getHeight()*pattern.getWidth())>64) {
world = new PackedWorld(pattern);//initialising happens in here
}
else {
world = new ArrayWorld(pattern);//and here
}
print();
cachedWorlds.add(world);//CHANGE
}
else if (response.equals("b")) {
int desiredGen = world.getGenerationCount()-1;
for (int i=0;i<cachedWorlds.size();i++) {
if ((cachedWorlds.get(i).getGenerationCount())==desiredGen) {
world=cachedWorlds.get(i);
}
}//this depends heavily on the copyworld - not finished
print();
}
}
}
public void print() {
System.out.println("- "+world.getGenerationCount());
for (int row=0; row<world.getHeight(); row++) {
for (int col=0; col<world.getWidth(); col++) {
System.out.print(world.getCell(row, col) ? "#" : "_");//world from getCell
}
System.out.println();
}
}
public static void main(String[] args) throws java.io.IOException,Exception,PatternFormatException {
if (args.length!=1) {
System.out.println("Usage: java GameOfLife <path/url to store>");
return;
}
try {
PatternStore ps = new PatternStore(args[0]);
GameOfLife gol = new GameOfLife(ps);
gol.play();
}
catch (IOException ioe) {
System.out.println("Failed to load pattern store");
}
catch (PatternFormatException p) {
System.out.println(p.getMessage());
}
}
}