Advertisement
Guest User

Untitled

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