Пример #1
0
  // 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;
  }
Пример #2
0
  private void drawTile(IBoard b, GC gc, int x, int y, int xo, int yo, int size) {
    gc.setClipping(xo, yo, size + 1, size + 1);

    Color c = background;
    if (b.getLetterMultiplier(y, x) == 2) c = doubleLetter;
    else if (b.getLetterMultiplier(y, x) == 3) c = tripleLetter;
    else if (b.getLetterMultiplier(y, x) == 4) c = quadLetter;
    else if (b.getWordMultiplier(y, x) == 2) c = doubleWord;
    else if (b.getWordMultiplier(y, x) == 3) c = tripleWord;
    else if (b.getWordMultiplier(y, x) == 4) c = quadWord;
    gc.setBackground(c);
    gc.fillRectangle(xo, yo, size, size);

    c = light;
    if (b.getLetterMultiplier(y, x) == 2) c = doubleLetterLight;
    else if (b.getLetterMultiplier(y, x) == 3) c = tripleLetterLight;
    else if (b.getLetterMultiplier(y, x) == 4) c = quadLetterLight;
    else if (b.getWordMultiplier(y, x) == 2) c = doubleWordLight;
    else if (b.getWordMultiplier(y, x) == 3) c = tripleWordLight;
    else if (b.getWordMultiplier(y, x) == 4) c = quadWordLight;
    gc.setForeground(c);
    gc.drawLine(xo, yo, xo + size, yo);
    gc.drawLine(xo, yo, xo, yo + size);

    gc.setForeground(dark);
    gc.drawLine(xo + size, yo + size, xo + size, yo);
    gc.drawLine(xo + size, yo + size, xo, yo + size);

    Tile tile = b.getTile(y, x);

    if (tile.equals(Tile.NONE)) {
      for (Iterator i = placedTiles.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry entry = (Map.Entry) i.next();
        Point point = (Point) entry.getKey();
        if (point.x == x && point.y == y) {
          tile = (Tile) entry.getValue();

          DragSource source = new DragSource();
          source.rectangle = new Rectangle(xo, yo, size + 1, size + 1);
          source.x = x;
          source.y = y;
          source.tile = tile;
          dragSources.put(source.rectangle, source);
        }
      }
    }

    boolean highlight = false;
    IGameAction action = game.getLastAction();
    if (action != null && action instanceof MoveAction) {
      MoveAction moveAction = (MoveAction) action;
      MoveResult result = moveAction.getMoveResult();
      Tile[] tiles = result.getPreviousBoardState();
      Move move = result.getMove();
      Orientation orientation = move.getOrientation();
      int dx = orientation.getDx();
      int dy = orientation.getDy();
      int mx = move.getColumn();
      int my = move.getRow();
      for (int i = 0; i < tiles.length; i++) {
        Tile t = tiles[i];
        if (mx == x && my == y && t.equals(Tile.NONE)) {
          highlight = true;
        }
        mx += dx;
        my += dy;
      }
    }

    drawTile(gc, new Rectangle(xo, yo, size + 1, size + 1), tile, highlight);

    if (arrow != null && arrow.x == x && arrow.y == y) {
      drawArrow(gc, xo, yo, size);
    }
  }