Ejemplo n.º 1
0
 private void updateTileRack() {
   tileRack.clear();
   ITileRack current = getLocalTileRack();
   if (current != null) {
     tileRack.add(current.getTiles());
   }
 }
Ejemplo n.º 2
0
 private void pushRack() {
   if (!validateRacks()) return;
   ITileRack rack = getLocalTileRack();
   if (rack != null) {
     rack.clear();
     rack.add(tileRack.getTiles());
   }
   updateTileRack();
 }
Ejemplo n.º 3
0
 private void emulateDrop(MouseEvent e) {
   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, Tile.NONE);
           return;
         }
       }
     }
   }
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
 private boolean validateRacks() {
   ITileRack r1 = tileRack;
   ITileRack r2 = getLocalTileRack();
   if (r2 == null) return false;
   Tile[] t1 = r1.getTiles();
   Tile[] t2 = r2.getTiles();
   Arrays.sort(t1);
   Arrays.sort(t2);
   if (t1.length != t2.length) return false;
   for (int i = 0; i < t1.length; i++) {
     if (!t1[i].equals(t2[i])) {
       return false;
     }
   }
   return true;
 }
Ejemplo n.º 6
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);
   }
 }
Ejemplo n.º 7
0
 private boolean beginDrag(MouseEvent e) {
   if (drag != null) {
     doDrop(e);
   }
   for (Rectangle rectangle : dragSources.keySet()) {
     if (rectangle.contains(e.x, e.y)) {
       drag = dragSources.get(rectangle);
       if (drag.y == -1) {
         tileRack.remove(drag.x);
       } else {
         placedTiles.remove(new Point(drag.x, drag.y));
       }
       emulateDrop(e);
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 8
0
  private void drawTileRack(GC gc, Rectangle bounds) {
    gc.setClipping(bounds);

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

    gc.setBackground(tileRackBorder);
    gc.fillRoundRectangle(x, y, w, h, border + 1, border + 1);
    gc.setBackground(tileRackFill);
    gc.fillRectangle(x + border, y + border, w - border * 2, h - border * 2);

    // TileRack tileRack = getLocalTileRack();
    if (tileRack == null) return;
    Tile[] tiles = tileRack.getTiles();

    x += border;
    y += border;
    for (int i = 0; i < tiles.length; i++) {
      Tile tile = tiles[i];
      Rectangle rectangle = new Rectangle(x, y, tileSize, tileSize);

      DragSource source = new DragSource();
      source.rectangle = rectangle;
      source.x = i;
      source.y = -1;
      source.tile = tile;
      dragSources.put(rectangle, source);

      DragTarget target = new DragTarget();
      target.rectangle = rectangle;
      target.x = i;
      target.y = -1;
      dragTargets.add(target);

      drawTile(gc, rectangle, tile);
      x += tileSize;
    }
  }
Ejemplo n.º 9
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();
   }
 }