// create move based on placed tiles private Move createMove() { if (game == null) return null; if (placedTiles.size() < 1) return null; Point[] points = new Point[placedTiles.size()]; placedTiles.keySet().toArray(points); IBoard board = game.getBoard(); boolean horizontal = true; boolean vertical = true; // vertical? for (int i = 1; i < points.length; i++) { Point point = points[i]; if (point.x != points[i - 1].x) { vertical = false; break; } } // horizontal? for (int i = 1; i < points.length; i++) { Point point = points[i]; if (point.y != points[i - 1].y) { horizontal = false; break; } } // scattered and invalid? if (!horizontal && !vertical) { return null; } // ambiguous? if (horizontal && vertical) { int x = points[0].x; int y = points[0].y; if (board.getTile(y, x - 1).equals(Tile.NONE) && board.getTile(y, x + 1).equals(Tile.NONE)) { horizontal = false; } if (board.getTile(y - 1, x).equals(Tile.NONE) && board.getTile(y + 1, x).equals(Tile.NONE)) { vertical = false; } // not adjacent to anything? if (!horizontal && !vertical) { return null; } // still ambiguous? if (horizontal && vertical) { // just pick one horizontal = false; } } Orientation orientation = vertical ? Orientation.VERTICAL : Orientation.HORIZONTAL; int dx = orientation.getDx(); int dy = orientation.getDy(); // find first placed tile int x = points[0].x; int y = points[0].y; for (int i = 1; i < points.length; i++) { Point point = points[i]; if (point.x < x) x = point.x; if (point.y < y) y = point.y; } // traverse on-board tiles backwards while (!board.getTile(y - dy, x - dx).equals(Tile.NONE)) { x -= dx; y -= dy; } int row = y; int column = x; // build word StringBuffer b = new StringBuffer(); while (true) { Point point = new Point(x, y); Tile tile = board.getTile(y, x); if (tile == null || tile.equals(Tile.NONE)) { tile = placedTiles.get(point); } if (tile == null || tile.equals(Tile.NONE)) { break; } b.append(tile); x += dx; y += dy; } Move move = new Move(); move.setColumn(column); move.setRow(row); move.setOrientation(orientation); move.setWord(Convert.toTiles(b.toString())); return move; }