Example #1
0
  private void drawTile(GC gc, Rectangle bounds, Tile tile, boolean highlight) {
    if (tile == null || tile.equals(Tile.NONE)) return;

    gc.setClipping(bounds);

    int x = bounds.x;
    int y = bounds.y;
    int w = bounds.width;
    int h = bounds.height;

    int s = Math.min(w, h);
    int n = Math.max(s / 12, 1);
    int x1 = x;
    int x2 = x1 + n;
    int x3 = x + s - n;
    int x4 = x + s;
    int y1 = y;
    int y2 = y1 + n;
    int y3 = y + s - n;
    int y4 = y + s;

    int[] p1 = new int[] {x1, y1, x4, y1, x3, y2, x2, y2, x2, y3, x1, y4};
    int[] p2 = new int[] {x4, y4, x1, y4, x2, y3, x3, y3, x3, y2, x4, y1};

    gc.setBackground(tileFill);
    gc.fillRectangle(x, y, w, h);
    gc.setBackground(highlight ? tileLightHighlight : tileLight);
    gc.fillPolygon(p1);
    gc.setBackground(highlight ? tileDarkHighlight : tileDark);
    gc.fillPolygon(p2);

    if (tile.isWild()) {
      if (!bounds.contains(mousex, mousey)) {
        return;
      }
    }

    Font font = new Font(null, tileFont, s / 2 + 1, SWT.BOLD);
    String string = Character.toString(tile.getLetter()).toUpperCase();
    gc.setFont(font);
    Point extent = gc.stringExtent(string);
    int sx = x + w / 2 - extent.x / 2 - n; // n * 2; //w / 2 - extent.x / 2 - s/8;
    int sy = y + h / 2 - extent.y / 2;
    gc.setForeground(textColor);
    gc.drawString(string, sx, sy, true);
    font.dispose();

    font = new Font(null, tileFont, s / 5, SWT.BOLD);
    string = Integer.toString(game.getTileValues().getValue(tile));
    gc.setFont(font);
    extent = gc.stringExtent(string);
    sx = x + w - n - extent.x - 1;
    sy = y + h - n - extent.y;
    gc.setForeground(textColor);
    gc.drawString(string, sx, sy, true);
    font.dispose();
  }
Example #2
0
 private boolean validatePlacedTiles() {
   List<Tile> availableTiles = new ArrayList<Tile>();
   ITileRack rack = getLocalTileRack();
   if (rack != null) {
     for (Tile tile : rack.getTiles()) {
       availableTiles.add(tile);
     }
   }
   for (Tile tile : placedTiles.values()) {
     if (tile.isWild()) tile = new Tile('?');
     if (!availableTiles.remove(tile)) {
       return false;
     }
   }
   return true;
 }
Example #3
0
 private void doDrop(MouseEvent e) {
   DragSource drag = this.drag;
   this.drag = null;
   if (drag == null) {
     return;
   }
   while (tileRack.remove(Tile.NONE)) ;
   for (DragTarget target : dragTargets) {
     if (target.rectangle.contains(e.x, e.y)) {
       if (target.y == -1) {
         int x = target.x;
         Tile[] tiles = tileRack.getTiles();
         if (x < tiles.length && !tiles[x].equals(Tile.NONE)) {
           tileRack.add(x, drag.tile);
           pushRack();
           return;
         }
       } else {
         IBoard board = game.getBoard();
         Tile current = board.getTile(target.y, target.x);
         if (current.equals(Tile.NONE)) {
           Point point = new Point(target.x, target.y);
           if (!placedTiles.containsKey(point)) {
             placedTiles.put(point, drag.tile);
             updateMoveString();
             return;
           }
         }
       }
     }
   }
   // return to source
   if (drag.y == -1) {
     tileRack.add(drag.tile);
     pushRack();
   } else {
     placedTiles.put(new Point(drag.x, drag.y), drag.tile);
   }
 }
Example #4
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;
  }
Example #5
0
 private void keyPressed(KeyEvent e) {
   if (game == null) return;
   if (game.getGameState() != GameState.IN_PROGRESS) return;
   char c = Character.toLowerCase(e.character);
   if (c >= 'a' && c <= 'z') {
     // type letters on the board
     if (arrow != null) {
       IBoard board = game.getBoard();
       if (arrow.x >= board.getWidth()) return;
       if (arrow.y >= board.getHeight()) return;
       Tile boardTile = game.getBoard().getTile(arrow.y, arrow.x);
       if (!boardTile.equals(Tile.NONE)) {
         if (c == boardTile.getLetter()) {
           arrow.advance();
           redraw();
         }
         return;
       }
       Tile tile = new Tile(c);
       boolean wild = false;
       if (!tileRack.contains(tile)) {
         tile = new Tile('?');
         if (!tileRack.contains(tile)) {
           return;
         }
         wild = true;
       }
       tileRack.remove(tile);
       tile = new Tile(c, wild);
       placedTiles.put(new Point(arrow.x, arrow.y), tile);
       arrow.advance();
     }
     updateMoveString();
     redraw();
   } else if (c == SWT.DEL || c == SWT.BS) {
     // backspace a typed character on the board
     if (placedTiles.size() > 0) {
       arrow.backup();
       Point point = new Point(arrow.x, arrow.y);
       Tile tile = placedTiles.get(point);
       if (tile != null) {
         if (tile.isWild()) tile = new Tile('?');
         tileRack.add(tile);
         placedTiles.remove(point);
       } else {
         arrow.advance();
       }
       updateMoveString();
       redraw();
     }
   } else if (c == '1') {
     // toggle coordinates
     showCoordinates = !showCoordinates;
     redraw();
   } else if (e.keyCode == SWT.ESC) {
     // clear typed characters
     resetInput(true);
     redraw();
   } else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
     // submit move on enter key
     done.run();
   } else if (c == ' ') {
     getLocalTileRack().shuffle();
     resetInput(true);
     redraw();
   }
 }
Example #6
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);
    }
  }