Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 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. try {
  11.  
  12. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  13.  
  14. String firstLine = reader.readLine();
  15. int length = firstLine.charAt(0);
  16. int height = firstLine.charAt(2);
  17.  
  18. World world = new World(length, height);
  19.  
  20. for(int i = 0; i < height; i++)
  21. {
  22. String line = reader.readLine();
  23.  
  24. for(int j = 0; i < length; j++)
  25. {
  26. char c = line.charAt(j);
  27.  
  28. if(c == '1')
  29. {
  30. world.set(j, i, CellState.ALIVE);
  31. }
  32.  
  33. }
  34. }
  35.  
  36. reader.close();
  37. return world;
  38.  
  39. }
  40. catch(Exception e)
  41. {
  42. throw new IOException();
  43. }
  44.  
  45.  
  46. }
  47.  
  48. @Override
  49. public void save(World world, OutputStream out) throws 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