Ejemplo n.º 1
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;
  }
Ejemplo n.º 2
0
  /**
   * Checks for a victory condition.
   *
   * @param {string} boardString Current state of the board.
   * @return {number} Status code for the victory state.
   */
  @ApiMethod(name = "board.checkForVictory", path = "checkForVictory", httpMethod = "POST")
  public Status checkForVictory(Board board) {

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

    int num_free = 0;
    int score_player1 = 0;
    int score_player2 = 0;
    for (int i = 0; i < cells.size(); i++) {
      Cell cell = cells.get(i);

      if (0 < cell.val && cell.val <= 10) {
        num_free += 1;
      } else if (10 < cell.val && cell.val <= 20) {
        score_player1 += cell.val - 10;
      } else if (20 < cell.val && cell.val <= 30) {
        score_player2 += cell.val - 20;
      }
    }

    Status s = new Status();

    System.out.println("num free:" + num_free);
    if (num_free > 0) {
      s.setStatus(0);
      return s;
    }
    if (score_player1 > score_player2) {
      s.setStatus(1);
      return s;
    }
    if (score_player1 < score_player2) {
      s.setStatus(2);
      return s;
    }

    s.setStatus(3);
    return s;
  };