예제 #1
0
  /**
   * Asks a player to set its pawns.
   *
   * @param player the player.
   */
  private void askPawns(final Player player) {
    if (player == null) {
      throw new NullPointerException();
    }

    Board board = boardManager.board();

    StringBuilder question = new StringBuilder();
    StringBuilder end = new StringBuilder();
    end.append(" (");
    end.append(getChar(Pawn.PawnType.GOOD));
    end.append("|");
    end.append(getChar(Pawn.PawnType.EVIL));
    end.append("): ");

    Pawn pawn = null;
    Coordinates c = null;
    int size = board.size();
    boolean isWhite = player == white;
    int y = (isWhite) ? 0 : size - 1;
    int endY = (isWhite) ? 2 : size - 3;
    int incY = (isWhite) ? 1 : -1;
    do {
      for (int x = 1; x < size - 1; x++) {
        c = new Coordinates(x, y, board.size());

        question.append("Pawn at ");
        question.append(c.x());
        question.append(":");
        question.append(c.y());
        question.append(end);

        // clearScreen();
        render(player);

        pawn = askPawn(question.toString(), player);
        player.add(pawn);
        board.set(pawn, c);

        question.delete(0, question.length());
      }

      y += incY;
    } while (y != endY);
    // clearScreen();
    render(player);
  }
예제 #2
0
  /**
   * Prints the Board to the CLI.
   *
   * @param player Player to render the board for.
   */
  private void render(final Player player) {
    Board board = boardManager.board();
    int size = board.size();

    int headOffsetSize = 1;
    int headOffsetSizeNumber = size + 1;
    while (headOffsetSizeNumber >= 10) {
      headOffsetSizeNumber /= 10;
      headOffsetSize++;
    }
    StringBuilder sbOffset = new StringBuilder(size / 10 + 1);
    for (int i = 0; i <= headOffsetSize; i++) {
      sbOffset.append(" ");
    }

    StringBuilder sbColumnsHeader = new StringBuilder(size + sbOffset.length() + 2);
    sbColumnsHeader.append(sbOffset);
    sbColumnsHeader.append(" ");
    for (char x = 'a'; x < size + 'a'; x++) {
      sbColumnsHeader.append(x);
    }
    sbColumnsHeader.append("\n");

    StringBuilder sbColumnsHyphens = new StringBuilder(size + sbOffset.length() + 3);
    sbColumnsHyphens.append(sbOffset);
    sbColumnsHyphens.append("+");
    for (int i = 0; i < size; i++) {
      sbColumnsHyphens.append("-");
    }
    sbColumnsHyphens.append("+\n");

    StringBuilder sbBoard = new StringBuilder((size + sbOffset.length() * 2 + 3) * size);
    Pawn pawn = null;

    int offsetSize = 0;
    int offsetMaxSize = 1;
    int yNumber = 0;
    int sizeNumber = size;
    while (sizeNumber >= 10) {
      sizeNumber /= 10;
      offsetMaxSize++;
    }

    boolean isWhite = player == white;
    int y = isWhite ? size - 1 : 0;
    int endY = isWhite ? -1 : size;
    int incY = isWhite ? -1 : 1;
    do {
      offsetSize = 1;
      yNumber = y + 1;
      while (yNumber >= 10) {
        yNumber /= 10;
        offsetSize++;
      }

      for (int i = 0; i < offsetMaxSize - offsetSize; i++) {
        sbBoard.append(" ");
      }
      sbBoard.append(y + 1);
      sbBoard.append(" |");
      for (int x = 0; x < size; x++) {
        pawn = board.at(new Coordinates(x, y, size));
        if (pawn == null) {
          if ((y == 0 || y == size - 1) && (x == 0 || x == size - 1)) {
            sbBoard.append("#");
          } else {
            sbBoard.append(".");
          }
        } else if (pawn.player() == player) {
          if (pawn.player() == white) {
            sbBoard.append((char) 27 + "[1;37m");
          } else {
            sbBoard.append((char) 27 + "[1;30m");
          }
          sbBoard.append(getChar(pawn.type()));
        } else {
          if (pawn.player() == white) {
            sbBoard.append((char) 27 + "[1;37m");
          } else {
            sbBoard.append((char) 27 + "[1;30m");
          }
          sbBoard.append(getChar(Pawn.PawnType.UNKNOWN));
        }
        sbBoard.append((char) 27 + "[0m");
      }
      sbBoard.append("| ");
      sbBoard.append(y + 1);
      sbBoard.append("\n");

      y += incY;
    } while (y != endY);

    StringBuilder sb =
        new StringBuilder(
            2 * (sbColumnsHeader.length() + sbColumnsHyphens.length()) + sbBoard.length());
    sb.append(sbColumnsHeader);
    sb.append(sbColumnsHyphens);
    sb.append(sbBoard);
    sb.append(sbColumnsHyphens);
    sb.append(sbColumnsHeader);

    System.out.println(sb.toString());
  }