Reputation: 31
I'm currently attempting to make a scrabble solver, and I'm having a bit of an issue with the reading of the text files which represent the board. For example, I have a file like this:
15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2. l a u d a t o r .. .. .2
.. .. .. .. 2. .. .. h .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 .. o .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2 t .2 .. .. .. .2 .. ..
3. .. .. .2 .. .. .. i .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
le*mdoe
7
3. .. .. 2. .. .. 3.
.. .3 .. .. .. .3 ..
.. .. a d .2 .. ..
2. .. u h .. .. 2.
.. .. l o .2 .. ..
.. m a t .. .3 ..
r e S i d .. 3.
toloeri
21
4. .. .. .2 .. .. .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
.. 2. .. .. .3 .. .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
.. .. 2. .. .. .4 .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.2 .. .. 3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. .3 .. .. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.. .. .4 .. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .. .. .2 .. .. 2. q u e p .. .. .. 2. .. .. .2 .. .. ..
3. .. .. .. .. .. l i .. .. .. .. .. 2. .. .. .. .. .. .. 3.
.. g l i t .. a .. .3 .. .. .. .3 .. .. .. .3 .. .. 2. ..
.. .. a .. .. .2 u .. n .2 .. .2 .. .. .. .2 .. .. 2. .. ..
d a w d .. .. d h o t i .. .. .. .2 .. .. 3. .. .. .2
.. .. f i g j a m s .2 .. .2 .. .. .. .2 .. .. 2. .. ..
.. t u n e .. t .. y .. .. .. .3 .. .. .. .3 .. .. 2. ..
m e l o d e o N .. .. .. .. .. 2. .. .. .. .. .. .. 3.
.. .. .. .2 .. .. r .. .. .. .2 .. .. .. 2. .. .. .2 .. .. ..
.. v e z i r s .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .3 .. o 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.2 b a r f .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. .. a r o w .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.. 2. .. o x y .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
k e p s .. n .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
ntnbtoi
15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
3. .. .. .2 .. c a t .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
dgos*ie
The first line above the board is the number of rows/columns. Then, obviously, the board. Then the hand with which to place characters.
The thing that I am trying to implement is a way to separate each board from each other, and solve them independently. I'm unsure how to use a scanner to, for example, stop after reading the row/column number, create the corresponding 2D array, then read the board in and stop before the hand, and then read the hand and place them into the Hand object, and then continue with the next board. I'm not sure how to make the divisons, if that makes sense.
Here is my attempt, that only kind of works. I can only get it to work if I delete the first line, and have just a single board. The lines that are commented out at the top are my attempt to read just the first line, and create the corresponding array, but whenever I do this, for my input
ArrayList, the first sysout is a blank list, and only then does it start reading the board itself, which doesn't make any sense to me.
public static void populateBoard(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
// String NUMBER_ROWS = scan.next();
// System.out.println(NUMBER_ROWS);
// board = new String[NUMBER_ROWS][NUMBER_ROWS];
board = new String[15][15];
int actualLine = 0;
while(actualLine < 15) {
ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
// Removes blank characters which occur because of the letters on the board having a space instead
// of a dot to their left.
input.removeIf(string -> string.equals(""));
System.out.println(input.toString());
for(int i = 0; i < input.size(); i++) {
board[actualLine][i] = input.get(i);
}
actualLine++;
}
}
I feel that I haven't explained this overly well, so if there is anything that I can clarify, please ask, and I will do so.
Upvotes: 1
Views: 118
Reputation: 6307
Yor are most of the way there, you just need to wrap it all inside another loop that finds the board size and creates each board one at a time.
We start by creating an ArrayList of boards that we can store each of the boards in ArrayList<String[][]> boards = new ArrayList<>();
.
Assuming the file will always be laid out correctly then next we need to create a loop that finds the boards one at a time and based on their size currentBoardSize = Integer.parseInt(line);
we can create them one at a time currentBoard = new String[currentBoardSize][currentBoardSize];
then we process the next number of lines to match the currentBoardSize
and store each line into a 2D array we just created using your line split code ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
;
Finally, we store the complete board into the boards
list boards.add(currentBoard);
, and then we let the while loop start over again and find the next board size and create a new 2D array until there are no more boards left in the file.
A complete example might look like this:
//list of boards, store this outside of the method so that it can be accessed from other places like you main method etc
public static ArrayList<String[][]> boards = new ArrayList<>();
//Load the file into the boards array, or change "void" to "ArrayList<String[][]>" if you want to return the array directly from the method
public static void populateBoard(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
int currentBoardSize = 0;
String[][] currentBoard;
//Get the very first board size so that the while loop works correctly
String line = scan.nextLine();
while(line != null) {
//Check it the first line has no spaces and is a single number indicating a new board
if(line.contains(" ") == false) {
try {
//Get the board size
currentBoardSize = Integer.parseInt(line);
}
catch (NumberFormatException ex) {
System.out.println("Error, unable to read board size: ");
ex.printStackTrace();
break;
}
currentBoard = new String[currentBoardSize][currentBoardSize];
System.out.println("Created new board that is "+currentBoardSize + "x"+currentBoardSize);
//populate the borad using the next number of lines based on the board size
for (int row = 0; row < currentBoardSize; row++) {
ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
// Removes blank characters which occur because of the letters on the board having a space instead
// of a dot to their left.
input.removeIf(string -> string.equals(""));
System.out.println(input.toString());
for (int column = 0; column < input.size(); column++) {
currentBoard[row][column] = input.get(column);
}
}
//Lastly do whatever you want with the word below each board
String wordToSolve = scan.nextLine();
System.out.println("Word to solve: "+wordToSolve );
//Add this board to the list of boards
boards.add(currentBoard);
//Get the next board size, or break the while loop
try {
line = scan.nextLine();
}
catch (NoSuchElementException ex) {
break;
}
}
else {
System.out.println("Error unexpected board size found: "+line);
break;
}
}
}
And the outputs can be gotten easily from the list like so:
///boards.get(board#) grabs the board, and [row#][col#] gets the row and column (remember they are 0 based, the first one is 0 not 1)
String value = boards.get(0)[2][6];
System.out.println("Value: "+value);
And this given output for the above is .2
as expected
Upvotes: 1