Пример #1
0
 @Nullable
 private static ArrayList<String> getStringOrArray(String name, JsonObject object) {
   final JsonElement jsonElement = object.get(name);
   final ArrayList<String> strings = Lists.newArrayList();
   if (jsonElement == null) return null;
   if (jsonElement.isJsonArray()) {
     final JsonArray array = jsonElement.getAsJsonArray();
     for (JsonElement element : array) {
       strings.add(element.getAsString());
     }
   } else {
     strings.add(jsonElement.getAsString());
   }
   return strings;
 }
Пример #2
0
 public static IpnbCellRaw fromCell(@NotNull final IpnbCell cell, int nbformat) {
   final IpnbCellRaw raw = new IpnbCellRaw();
   if (cell instanceof IpnbEditableCell) {
     raw.metadata = ((IpnbEditableCell) cell).getMetadata();
   }
   if (cell instanceof IpnbMarkdownCell) {
     raw.cell_type = "markdown";
     raw.source = ((IpnbMarkdownCell) cell).getSource();
   } else if (cell instanceof IpnbCodeCell) {
     raw.cell_type = "code";
     final ArrayList<CellOutputRaw> outputRaws = new ArrayList<CellOutputRaw>();
     for (IpnbOutputCell outputCell : ((IpnbCodeCell) cell).getCellOutputs()) {
       outputRaws.add(CellOutputRaw.fromOutput(outputCell, nbformat));
     }
     raw.outputs = outputRaws;
     final Integer promptNumber = ((IpnbCodeCell) cell).getPromptNumber();
     if (nbformat == 4) {
       raw.execution_count = promptNumber != null && promptNumber >= 0 ? promptNumber : null;
       raw.source = ((IpnbCodeCell) cell).getSource();
     } else {
       raw.prompt_number = promptNumber != null && promptNumber >= 0 ? promptNumber : null;
       raw.language = ((IpnbCodeCell) cell).getLanguage();
       raw.input = ((IpnbCodeCell) cell).getSource();
     }
   } else if (cell instanceof IpnbRawCell) {
     raw.cell_type = "raw";
     raw.source = ((IpnbRawCell) cell).getSource();
   } else if (cell instanceof IpnbHeadingCell) {
     raw.cell_type = "heading";
     raw.source = ((IpnbHeadingCell) cell).getSource();
     raw.level = ((IpnbHeadingCell) cell).getLevel();
   }
   return raw;
 }
Пример #3
0
  /**
   * Provides the ability to insert a new Score entity.
   *
   * @param board object representing the state of the board
   * @return the board including the computer's move
   */
  @ApiMethod(name = "board.getmove", path = "getmove", httpMethod = "POST")
  public Board getmove(
      Board board, @Named("id") int id, @Named("row") int row, @Named("col") int col) {
    System.out.println("id: " + id);

    CellContainer cellContainer = CellContainer.fromJson(board.getState());
    ArrayList<Cell> cells = cellContainer.cells;

    ArrayList<Cell> unoccupiedCells = new ArrayList<Cell>();
    for (Cell cell : cells) {
      System.out.println("row: " + row + " | cell.x: " + cell.x);
      if (row == cell.x && col == cell.y && row != 100 && col != 100) {
        System.out.println("updated 1");
        cell.val += (id + 1) * 10;
        CellContainer updateContainer = new CellContainer(cells);
        Board updated = new Board(CellContainer.toJson(updateContainer));
        return updated;
      }

      if (cell.val <= 10) {
        unoccupiedCells.add(cell);
      }
    }

    if (id == 0) System.out.println("what");
    Random randomG = new Random();
    System.out.println("updated 2");
    Cell randomfreeCell = unoccupiedCells.get(randomG.nextInt(unoccupiedCells.size()));
    randomfreeCell.val = randomfreeCell.val + (id + 1) * 10;

    CellContainer updateContainer = new CellContainer(cells);
    Board updated = new Board(CellContainer.toJson(updateContainer));
    return updated;
  }