private void drawCoordinates(IBoard b, GC gc, int xo, int yo, int size, int csize) { gc.setBackground(light); gc.setForeground(coordinateColor); gc.setFont(coordinateFont); int xs = size / b.getWidth(); int ys = size / b.getHeight(); int pdx = 4 + gc.stringExtent("X").x; int pdy = 4; for (int y = 0; y < b.getHeight(); y++) { String s = Character.toString((char) (y + 'A')); int x = 0; int px = xo + x * xs - gc.stringExtent(s).x / 2 - pdx; int py = yo + y * ys + ys / 2 - gc.stringExtent(s).y / 2; gc.drawString(s, px, py, true); } for (int x = 0; x < b.getWidth(); x++) { String s = Integer.toString(x + 1); int y = 0; int px = xo + x * xs + xs / 2 - gc.stringExtent(s).x / 2; int py = yo + y * ys - gc.stringExtent(s).y - pdy; gc.drawString(s, px, py, true); } }
public void go() { display.display(); if (isGameEnded()) { System.out.println("Game Ended result: " + board.getStatus()); return; } System.out.println("Player " + currentPlayer.getName() + " moves."); Move move = currentPlayer.getNextMove(board.clone()); try { board.setCell(move.getRow(), move.getColumn(), currentPlayer.getSign()); } catch (InvalidAssignmentException e) { throw new RuntimeException(e); } currentPlayer = (currentPlayer == p1 ? p2 : p1); go(); }
private void drawContainer(GC gc, Rectangle bounds) { gc.setClipping(bounds); dragSources.clear(); dragTargets.clear(); // gc.setTextAntialias(SWT.ON); // gc.setAntialias(SWT.OFF); // gc.setAdvanced(false); gc.setBackground(light); gc.fillRectangle(bounds); if (game == null) { return; } IBoard board = game.getBoard(); int x = bounds.x; int y = bounds.y; int w = bounds.width; int h = bounds.height; int coord = showCoordinates ? coordinates : 0; // compute tile size int height = bounds.height - (4 * border + 3 * padding + 1 * coord); int width = bounds.width - (2 * border + 2 * padding + 1 * coord); tileSize = Math.min(width / board.getWidth(), height / (board.getHeight() + 1)); // compute header size and position int hh = tileSize + border * 2; int hw = tileSize * board.getWidth() + border * 2; int hx = (w - hw + coord) / 2; int hy = y + padding; drawHeader(gc, new Rectangle(hx, hy, hw, hh)); // compute board area size and position int cx = x + padding; int cy = y + hh + padding * 2; int cw = w - padding * 2; int ch = h - hh - padding * 3; drawCenter(gc, new Rectangle(cx, cy, cw, ch)); }
private void drawBoard(IBoard b, GC gc, int xo, int yo, int size) { squareLocations.clear(); int tileSize = size / b.getWidth(); // TODO handle non-square boards for (int y = 0; y < b.getHeight(); y++) { for (int x = 0; x < b.getWidth(); x++) { int px = xo + x * tileSize; int py = yo + y * tileSize; Rectangle rectangle = new Rectangle(px, py, tileSize, tileSize); DragTarget target = new DragTarget(); target.rectangle = rectangle; target.x = x; target.y = y; dragTargets.add(target); squareLocations.add(new Square(x, y, rectangle)); drawTile(b, gc, x, y, px, py, tileSize - 1); } } }
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); } }
private void drawCenter(GC gc, Rectangle bounds) { gc.setClipping(bounds); int x = bounds.x; int y = bounds.y; int w = bounds.width; int h = bounds.height; gc.setFont(coordinateFont); int coord = showCoordinates ? coordinates : 0; int n = coord + border * 2; IBoard board = game.getBoard(); int t = Math.min((w - n) / board.getWidth(), (h - n) / board.getHeight()); int bw = t * board.getWidth() + n; int bh = t * board.getHeight() + n; int px = (w - bw) / 2; int py = (h - bh) / 2; int rx = (w - bw) % 2; int ry = (h - bh) % 2; drawBoardArea(gc, new Rectangle(x + px, y + 0, w - px * 2 - rx, h - py * 2 - ry), coord); }
/** * Implements the game ended status here * * @return true if the game is over */ private boolean isGameEnded() { return board.getStatus() != Status.GameOpen || board.getStatus() == Status.Draw; }
// 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; }
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(); } }
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); } }