Advertisement
Guest User

GameOfLife.java errors

a guest
Jan 22nd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. //package uk.ac.cam.jm2129.oop.tick1;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class GameOfLife
  9. {
  10.     private PatternStore mStore;
  11.     private World mWorld;
  12.     private ArrayList<World> mCachedWorlds;
  13.     public GameOfLife(PatternStore ps)
  14.     {
  15.         mStore = ps;
  16.     }
  17.     private World copyWorld(boolean useCloning) throws PatternFormatException, CloneNotSupportedException
  18.     {
  19.         World worldCopy = null;
  20.         if (useCloning == false)
  21.         {
  22.             if (mWorld instanceof PackedWorld)
  23.             {
  24.                 worldCopy = new PackedWorld((PackedWorld) mWorld);
  25.                 mCachedWorlds.add(worldCopy);
  26.             } else if (mWorld instanceof ArrayWorld)
  27.             {
  28.                 worldCopy = new ArrayWorld((ArrayWorld) mWorld);
  29.                 mCachedWorlds.add(worldCopy);
  30.             }
  31.         }
  32.         else
  33.         {
  34.             if (mWorld instanceof PackedWorld)
  35.             {
  36.                 worldCopy = (PackedWorld) mWorld.clone();
  37.                 mCachedWorlds.add(worldCopy);
  38.             } else if (mWorld instanceof ArrayWorld)
  39.             {
  40.                 worldCopy = (ArrayWorld) mWorld.clone();
  41.                 mCachedWorlds.add(worldCopy);
  42.             }
  43.         }
  44.         return worldCopy;
  45.        
  46.     }
  47.     public void play() throws IOException, PatternNotFound, PatternFormatException, CloneNotSupportedException
  48.     {
  49.         String response="";
  50.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  51.         System.out.println("Please select a pattern to play (l to list:");
  52.         while (!response.equals("q"))
  53.         {
  54.                 response = in.readLine();
  55.                 System.out.println(response);
  56.                 if (response.equals("f"))
  57.                 {
  58.                     int k = mWorld.getGenerationCount();
  59.                     if (mWorld==null) {System.out.println("Please select a pattern to play (l to list):");}
  60.                     else if (k+1 < mCachedWorlds.size())
  61.                     {
  62.                         mWorld = mCachedWorlds.get(k+1);
  63.                         print();
  64.                     }
  65.                     else
  66.                     {
  67.                         mWorld.nextGeneration();
  68.                         copyWorld(true);
  69.                         print();
  70.                     }
  71.                 }
  72.                 else if (response.equals("l")) {
  73.                         List<Pattern> names = mStore.getPatternsNameSorted();
  74.                         int i=0;
  75.                         for (Pattern p : names) {
  76.                                 System.out.println(i+" "+p.getName()+"  ("+p.getAuthor()+")");
  77.                                 i++;
  78.                         }
  79.                 }
  80.                 else if (response.startsWith("p"))
  81.                 {
  82.                     mCachedWorlds = new ArrayList<World>();
  83.                     List<Pattern> names = mStore.getPatternsNameSorted();
  84.                     String stringOfInt = response.substring(2);
  85.                     int patternNumber = Integer.parseInt(stringOfInt);
  86.                     // TODO: Get the associated pattern
  87.                     if (patternNumber <= names.size())
  88.                     {
  89.                        int i = 0;
  90.                        for (Pattern p : names)
  91.                        {
  92.                            if (i == patternNumber)
  93.                            {
  94.                                Pattern myPattern = p;
  95.                                int width = myPattern.getWidth();
  96.                                int height = myPattern.getHeight();
  97.                                int worldSize = width*height;
  98.                                if (worldSize > 64)
  99.                                {
  100.                                    mWorld = new ArrayWorld(myPattern);
  101.                                    copyWorld(true);
  102.                                    print();
  103.                                }
  104.                                else if ((worldSize > 0) && (worldSize < 65))
  105.                                {
  106.                                    mWorld = new PackedWorld(myPattern);
  107.                                    copyWorld(true);
  108.                                    print();
  109.                                }
  110.                                
  111.                            } else {i++;}
  112.                        }
  113.                    } else {throw new PatternNotFound("Sorry, there is no Pattern associated with this number");}
  114.                 }
  115.                 else if (response.equals("b"))
  116.                 {
  117.                     int k = mWorld.getGenerationCount();
  118.                     if (k == 0) {print();}
  119.                     else
  120.                     {
  121.                         k-=1;
  122.                         World worldCopy = mCachedWorlds.get(k);
  123.                         mWorld = worldCopy;
  124.                         print();
  125.                     }
  126.                 }
  127.         }
  128.     }
  129.     public void print() throws IOException, PatternNotFound, PatternFormatException, CloneNotSupportedException
  130.     {
  131.         System.out.println("- "+mWorld.getGenerationCount());
  132.         for (int row = 0; row < mWorld.getHeight(); row++)
  133.         {
  134.             for (int col = 0; col < mWorld.getWidth(); col++)
  135.             {
  136.                 System.out.print(mWorld.getCell(col, row) ? "#" : "_");
  137.             }
  138.             System.out.println();
  139.         }
  140.         play();
  141.     }
  142.     public static void main(String args[]) throws IOException, PatternNotFound, PatternFormatException, CloneNotSupportedException
  143.     {
  144.         if (args.length!=1)
  145.         {
  146.             System.out.println("Usage: java GameOfLife <path/url to store>");
  147.             return;
  148.         }
  149.    
  150.         try
  151.         {
  152.             PatternStore ps = new PatternStore(args[0]);
  153.             GameOfLife gol = new GameOfLife(ps);    
  154.             gol.play();
  155.         }
  156.         catch (IOException ioe)
  157.         {
  158.             System.out.println("Failed to load pattern store");
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement