Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package uk.ac.cam.jm2129.oop.tick1;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class GameOfLife
- {
- private PatternStore mStore;
- private World mWorld;
- private ArrayList<World> mCachedWorlds;
- public GameOfLife(PatternStore ps)
- {
- mStore = ps;
- }
- private World copyWorld(boolean useCloning) throws PatternFormatException, CloneNotSupportedException
- {
- World worldCopy = null;
- if (useCloning == false)
- {
- if (mWorld instanceof PackedWorld)
- {
- worldCopy = new PackedWorld((PackedWorld) mWorld);
- mCachedWorlds.add(worldCopy);
- } else if (mWorld instanceof ArrayWorld)
- {
- worldCopy = new ArrayWorld((ArrayWorld) mWorld);
- mCachedWorlds.add(worldCopy);
- }
- }
- else
- {
- if (mWorld instanceof PackedWorld)
- {
- worldCopy = (PackedWorld) mWorld.clone();
- mCachedWorlds.add(worldCopy);
- } else if (mWorld instanceof ArrayWorld)
- {
- worldCopy = (ArrayWorld) mWorld.clone();
- mCachedWorlds.add(worldCopy);
- }
- }
- return worldCopy;
- }
- public void play() throws IOException, PatternNotFound, PatternFormatException, CloneNotSupportedException
- {
- 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(response);
- if (response.equals("f"))
- {
- int k = mWorld.getGenerationCount();
- if (mWorld==null) {System.out.println("Please select a pattern to play (l to list):");}
- else if (k+1 < mCachedWorlds.size())
- {
- mWorld = mCachedWorlds.get(k+1);
- print();
- }
- else
- {
- mWorld.nextGeneration();
- copyWorld(true);
- print();
- }
- }
- else if (response.equals("l")) {
- List<Pattern> names = mStore.getPatternsNameSorted();
- int i=0;
- for (Pattern p : names) {
- System.out.println(i+" "+p.getName()+" ("+p.getAuthor()+")");
- i++;
- }
- }
- else if (response.startsWith("p"))
- {
- mCachedWorlds = new ArrayList<World>();
- List<Pattern> names = mStore.getPatternsNameSorted();
- String stringOfInt = response.substring(2);
- int patternNumber = Integer.parseInt(stringOfInt);
- // TODO: Get the associated pattern
- if (patternNumber <= names.size())
- {
- int i = 0;
- for (Pattern p : names)
- {
- if (i == patternNumber)
- {
- Pattern myPattern = p;
- int width = myPattern.getWidth();
- int height = myPattern.getHeight();
- int worldSize = width*height;
- if (worldSize > 64)
- {
- mWorld = new ArrayWorld(myPattern);
- copyWorld(true);
- print();
- }
- else if ((worldSize > 0) && (worldSize < 65))
- {
- mWorld = new PackedWorld(myPattern);
- copyWorld(true);
- print();
- }
- } else {i++;}
- }
- } else {throw new PatternNotFound("Sorry, there is no Pattern associated with this number");}
- }
- else if (response.equals("b"))
- {
- int k = mWorld.getGenerationCount();
- if (k == 0) {print();}
- else
- {
- k-=1;
- World worldCopy = mCachedWorlds.get(k);
- mWorld = worldCopy;
- print();
- }
- }
- }
- }
- public void print() throws IOException, PatternNotFound, PatternFormatException, CloneNotSupportedException
- {
- System.out.println("- "+mWorld.getGenerationCount());
- for (int row = 0; row < mWorld.getHeight(); row++)
- {
- for (int col = 0; col < mWorld.getWidth(); col++)
- {
- System.out.print(mWorld.getCell(col, row) ? "#" : "_");
- }
- System.out.println();
- }
- play();
- }
- public static void main(String args[]) throws IOException, PatternNotFound, PatternFormatException, CloneNotSupportedException
- {
- 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");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement