Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- /**
- * Created by root on 2/25/17.
- */
- public class StandardWorldLoader implements WorldLoader {
- @Override
- public World load(InputStream in) throws IOException {
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String firstLine = reader.readLine();
- int length = firstLine.charAt(0);
- int height = firstLine.charAt(2);
- World world = new World(length, height);
- for(int i = 0; i < height; i++)
- {
- String line = reader.readLine();
- for(int j = 0; i < length; j++)
- {
- char c = line.charAt(j);
- if(c == '1')
- {
- world.set(j, i, CellState.ALIVE);
- }
- }
- }
- reader.close();
- return world;
- }
- catch(Exception e)
- {
- throw new IOException();
- }
- }
- @Override
- public void save(World world, OutputStream out) throws IOException
- {
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
- String firstLine = Integer.toString(world.getWidth()) + "x" + Integer.toString(world.getHeight());
- writer.write(firstLine);
- writer.newLine();
- for (int i = 0; i < world.getHeight(); i++) {
- String line = "";
- for (int j = 0; i < world.getWidth(); j++) {
- char c = world.get(j, i) == CellState.ALIVE ? '1' : '0';
- line += c;
- }
- writer.write(line);
- writer.newLine();
- }
- writer.flush();
- writer.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement