Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /**
  4. * Created by root on 2/25/17.
  5. */
  6. public class StandardWorldLoader implements WorldLoader {
  7. @Override
  8. public World load(InputStream in) throws IOException {
  9.  
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  11.  
  12. String firstLine = reader.readLine();
  13. int length = firstLine.charAt(0);
  14. int height = firstLine.charAt(2);
  15.  
  16. World world = new World(length, height);
  17.  
  18. for(int i = 0; i < height; i++)
  19. {
  20. String line = reader.readLine();
  21.  
  22. for(int j = 0; i < length; j++)
  23. {
  24. char c = line.charAt(j);
  25.  
  26. if(c == '1')
  27. {
  28. world.set(j, i, CellState.ALIVE);
  29. }
  30.  
  31. }
  32. }
  33.  
  34. reader.close();
  35. return world;
  36.  
  37. }
  38.  
  39. @Override
  40. public void save(World world, OutputStream out) throws IOException
  41. {
  42. if(world == null)
  43. {
  44. throw new IOException();
  45. }
  46. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
  47. String firstLine = Integer.toString(world.getWidth()) + "x" + Integer.toString(world.getHeight());
  48.  
  49. writer.write(firstLine);
  50. writer.newLine();
  51.  
  52. for (int i = 0; i < world.getHeight(); i++) {
  53. String line = "";
  54.  
  55. for (int j = 0; i < world.getWidth(); j++) {
  56. char c = world.get(j, i) == CellState.ALIVE ? '1' : '0';
  57. line += c;
  58. }
  59.  
  60. writer.write(line);
  61. writer.newLine();
  62.  
  63. }
  64. writer.flush();
  65. writer.close();
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement