private static ArrayList<Word> getWordsOnBoard(GameBoard gameBoard) { ArrayList<Word> wordsOnBoard = new ArrayList<>(50); StringBuilder stringBuilder; Coordinate coord; Word word; // find words // could this be expressed more concisely? // cause it's sort of a bear. but also sort of awesome? for (int a = 0; a < 2; a++) { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { coord = a == 0 ? new Coordinate(j, i) : new Coordinate(i, j); BoardCell boardCell = gameBoard.getCellAt(coord); while (!boardCell.isEmpty()) { // catch the first letter Coordinate head = coord; stringBuilder = new StringBuilder(15); do { stringBuilder.append(boardCell.getTile().getCharacter()); // add a letter coord = a == 0 ? new Coordinate(++j, i) : new Coordinate(i, ++j); // the next tile if (gameBoard.getCellAt(coord).isEmpty()) { // if the next tile is empty we're done if (stringBuilder.length() > 1) { // one letter does not a word make word = new Word(head, a == 0, stringBuilder.toString()); wordsOnBoard.add(word); } } boardCell = gameBoard.getCellAt(coord); } while (!gameBoard.getCellAt(coord).isEmpty()); } } } } return wordsOnBoard; }
@Override public String toString() { String header = " A B C D E F G H I J K L M N O\n"; String row = " |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|"; StringBuilder sb = new StringBuilder(header); sb.append(row); sb.append('\n'); for (int i = 0; i < 15; i++) { Integer y = i + 1; sb.append(y); sb.append(i < 9 ? " |" : "|"); for (int j = 0; j < 15; j++) { BoardCell bc = boardCells[i][j]; if (i == 7 && j == 7 && bc.isEmpty()) { sb.append(" * |"); continue; } if (bc.isEmpty()) { switch (bc.getMultiplier()) { case 1: sb.append(" |"); break; case 2: sb.append(bc.isWordMultiplier() ? "DW |" : "DL |"); break; case 3: sb.append(bc.isWordMultiplier() ? "TW |" : "TL |"); break; } } else { sb.append(bc.getTile().toString()); sb.append(bc.getTile().toString().length() == 2 ? " |" : "|"); } } sb.append(y); sb.append('\n'); sb.append(row); sb.append('\n'); } sb.append(header); return sb.toString(); }