示例#1
0
  private JSONObject specialMove(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    int game;
    int addPoints;
    int special;
    // Gets random number to denote which game to use action on
    game = getRandomGame(gameCount);
    gameInfo = gameRecord.get(game);
    // Checks if all specials are used
    if (gameInfo.checkSpecial()) {
      return moveUser(gameCount);
    }
    // Gets random number to denote which special move to use
    special = randomNum.nextInt(4);
    while (gameInfo.checkSpecialMove(special)) {
      special = randomNum.nextInt(4);
    }
    gameInfo.useSpecialMove(special);

    addPoints = randomNum.nextInt(41) - 20;
    gameInfo.addPoints(addPoints);
    gameInfo.incrementCount();

    try {
      jObj.put("game", game);

      action.put("actionType", "specialMove");
      action.put("actionNumber", gameInfo.getCount());
      action.put("pointsAdded", addPoints);
      if (special == SHUFFLE) {
        action.put("move", "Shuffle");
      } else if (special == CLEAR) {
        action.put("move", "Clear");
      } else if (special == INVERT) {
        action.put("move", "Invert");
      } else {
        action.put("move", "Rotate");
      }
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
示例#2
0
  private JSONObject endGame(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    int game;
    String status;

    game = getRandomGame(gameCount);
    gameInfo = gameRecord.get(game);
    gameInfo.incrementCount();
    if (gameInfo.getCount() < 9) {
      return moveUser(gameCount);
    }
    if (gameInfo.getPoints() > 40 || gameInfo.getPoints() < -40) {
      status = "WIN";
    } else {
      status = "LOSS";
    }
    try {
      jObj.put("game", game);

      action.put("actionType", "gameEnd");
      action.put("gameStatus", status);
      action.put("actionNumber", gameInfo.getCount());
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
      userIds[gameInfo.getId()] = false;
      gameRecord.remove(game);
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
示例#3
0
  private JSONObject moveUser(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    JSONObject coords = new JSONObject();
    int game;
    int xCoord;
    int yCoord;
    int addPoints;
    // Gets random number to denote which game to use action on
    game = getRandomGame(gameCount);

    xCoord = randomNum.nextInt(20) + 1;
    yCoord = randomNum.nextInt(20) + 1;
    addPoints = randomNum.nextInt(41) - 20;
    gameInfo = gameRecord.get(game);
    gameInfo.addPoints(addPoints);
    gameInfo.incrementCount();
    try {
      jObj.put("game", game);

      coords.put("x", xCoord);
      coords.put("y", yCoord);

      action.put("actionType", "Move");
      action.put("actionNumber", gameInfo.getCount());
      action.put("location", coords);
      action.put("pointsAdded", addPoints);
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }