F
Fencer
Hello, I'm writing a simulator that involves the concept of a grid of
tiles. There are a number of different tile types and each tile must be
of one of those types.
I want a method that can create a new grid from a text file. Say the
file has the following content:
abb
aaa
cba
the method should create a 3x3 grid and the type of any given tile
is determined by its corresponding character in the input file.
I wanted this method to work both on real text files but also on
file-like objects (inspired by python's StringIO), so I came up with this:
package controller;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
public class Controller {
public void init(final Reader reader) throws IOException {
LineNumberReader rdr = new LineNumberReader(reader);
String line = "";
while ((line = rdr.readLine()) != null) {
for (int column = 0; column < line.length(); ++column) {
int lineNum = rdr.getLineNumber() - 1;
char c = line.charAt(column);
createTile(lineNum, column, c);
}
}
}
public void createTile(int row, int column, char type) {
System.out.println(String.format("Create tile at (row, column) = " +
"(%d, %d) with type %c", row, column, type));
}
public static void main(String[] args) throws IOException {
Controller controller = new Controller();
controller.init(new StringReader("abc\ndef\nghi"));
System.out.println("");
controller.init(new FileReader("test.txt"));
}
}
Output when run:
Create tile at (row, column) = (0, 0) with type a
Create tile at (row, column) = (0, 1) with type b
Create tile at (row, column) = (0, 2) with type c
Create tile at (row, column) = (1, 0) with type d
Create tile at (row, column) = (1, 1) with type e
Create tile at (row, column) = (1, 2) with type f
Create tile at (row, column) = (2, 0) with type g
Create tile at (row, column) = (2, 1) with type h
Create tile at (row, column) = (2, 2) with type i
Create tile at (row, column) = (0, 0) with type a
Create tile at (row, column) = (0, 1) with type b
Create tile at (row, column) = (0, 2) with type c
Create tile at (row, column) = (1, 0) with type d
Create tile at (row, column) = (1, 1) with type e
Create tile at (row, column) = (1, 2) with type f
Create tile at (row, column) = (2, 0) with type g
Create tile at (row, column) = (2, 1) with type h
Create tile at (row, column) = (2, 2) with type i
This is just a test program where I check that I can read each character
(each tile) properly. It seems to work, but was this the proper way to
solve this problem?
- Fencer
tiles. There are a number of different tile types and each tile must be
of one of those types.
I want a method that can create a new grid from a text file. Say the
file has the following content:
abb
aaa
cba
the method should create a 3x3 grid and the type of any given tile
is determined by its corresponding character in the input file.
I wanted this method to work both on real text files but also on
file-like objects (inspired by python's StringIO), so I came up with this:
package controller;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
public class Controller {
public void init(final Reader reader) throws IOException {
LineNumberReader rdr = new LineNumberReader(reader);
String line = "";
while ((line = rdr.readLine()) != null) {
for (int column = 0; column < line.length(); ++column) {
int lineNum = rdr.getLineNumber() - 1;
char c = line.charAt(column);
createTile(lineNum, column, c);
}
}
}
public void createTile(int row, int column, char type) {
System.out.println(String.format("Create tile at (row, column) = " +
"(%d, %d) with type %c", row, column, type));
}
public static void main(String[] args) throws IOException {
Controller controller = new Controller();
controller.init(new StringReader("abc\ndef\nghi"));
System.out.println("");
controller.init(new FileReader("test.txt"));
}
}
Output when run:
Create tile at (row, column) = (0, 0) with type a
Create tile at (row, column) = (0, 1) with type b
Create tile at (row, column) = (0, 2) with type c
Create tile at (row, column) = (1, 0) with type d
Create tile at (row, column) = (1, 1) with type e
Create tile at (row, column) = (1, 2) with type f
Create tile at (row, column) = (2, 0) with type g
Create tile at (row, column) = (2, 1) with type h
Create tile at (row, column) = (2, 2) with type i
Create tile at (row, column) = (0, 0) with type a
Create tile at (row, column) = (0, 1) with type b
Create tile at (row, column) = (0, 2) with type c
Create tile at (row, column) = (1, 0) with type d
Create tile at (row, column) = (1, 1) with type e
Create tile at (row, column) = (1, 2) with type f
Create tile at (row, column) = (2, 0) with type g
Create tile at (row, column) = (2, 1) with type h
Create tile at (row, column) = (2, 2) with type i
This is just a test program where I check that I can read each character
(each tile) properly. It seems to work, but was this the proper way to
solve this problem?
- Fencer