@Override
 public Player getActivePlayer(User user, Game game) {
   for (Player p : user.getProfile().getPlayers()) {
     for (Player gamePlayer : game.getPlayers()) {
       if (gamePlayer.getPlayerId() == p.getPlayerId()) {
         return gamePlayer;
       }
     }
   }
   throw new SpaceCrackUnexpectedException("This user isn't playing this game");
 }
Exemple #2
0
  public void newGame(final Player player1, final String sessionId) {
    gameId = myDAO.createNewId("games");
    // TODO: Add random turn determining logic
    players.add(player1);

    Player player2 = myDAO.matchMake(player1);

    players.add(player2);
    board.defaultBoard(player1.getPlayerId().toString(), player2.getPlayerId().toString());
    myDAO.createNewGame(gameId, players, board);

    updateGameSession(sessionId, player1);
  }
Exemple #3
0
 /**
  * This method stands to show if the player CAN use one of his/her un-used dev Cards of the type
  * you ask it about.
  *
  * @param devCardType
  * @return
  */
 public boolean canDoPlayerUseDevelopmentCard(int userID, DevCardType devCardType) {
   // We need to be able to measure how long a player has owned a card.
   if (currentPlayer.getPlayerId() == userID) {
     return currentPlayer.canDoPlayDevelopmentCard(turnNumber, devCardType);
   }
   return false;
 }
Exemple #4
0
 /**
  * Retrieves whether the Robber can be moved to the specified hexLocation
  *
  * @pre board != null
  * @param hex
  * @return whether the Robber can be moved to the specified hexLocation
  */
 public boolean canDoMoveRobberToHex(int UserId, HexLocation hexLocation) {
   // Check if the user is the current player
   if (UserId != currentPlayer.getPlayerId()) {
     return false;
   }
   return board.canDoMoveRobberToHex(hexLocation);
 }
Exemple #5
0
 /**
  * Can we place a settlement on that vertex?
  *
  * @pre the vertex location is not null
  * @param vertexLocation
  * @post it tells you whether or not you can build a Settlement on that vertex
  */
 public boolean canDoPlaceSettlementOnVertex(int UserId, VertexLocation vertexLocation) {
   // Check if the user is the current player
   if (UserId != currentPlayer.getPlayerId()) {
     return false;
   }
   return board.canDoPlaceSettlementOnVertex(currentPlayer, vertexLocation);
 }
 private static boolean canTarget(Player player, CardDescr card, TargetableCharacter target) {
   TargetNeed cardNeed = card.getCombinedTargetNeed(player);
   boolean hero = target instanceof Hero;
   TargeterDef targeterDef = new TargeterDef(player.getPlayerId(), hero, false);
   PlayerTargetNeed targetNeed = new PlayerTargetNeed(targeterDef, cardNeed);
   return targetNeed.isAllowedTarget(target);
 }
 private void checkLost(Game gameByGameId) {
   for (Player player : gameByGameId.getPlayers()) {
     if (player.getColonies().size() == 0) {
       gameByGameId.setLoserPlayerId(player.getPlayerId());
     }
   }
 }
Exemple #8
0
 public Player getPlayerById(int id) {
   for (Player p : players) {
     if (p.getPlayerId().compareTo(id) == 0) {
       return p;
     }
   }
   return null;
 }
Exemple #9
0
 public String getActivePlayerName() {
   for (Player p : players) {
     if (p.getPlayerId().compareTo(getActivePlayerId()) == 0) {
       return p.getPlayerName();
     }
   }
   return "Error: Player Name Not Found";
 }
Exemple #10
0
 public int getIndexOfPlayer(Player player) {
   if (player == null) {
     return -1;
   }
   int index = -1;
   for (int i = 0; i < players.length; i++) {
     if (player.getPlayerId() == players[i].getPlayerId()) {
       index = i;
     }
   }
   return index;
 }
Exemple #11
0
 /**
  * Checks whether the Specified User can Steal from the Specified Victim
  *
  * @pre None
  * @param UserId
  * @param victimId
  * @return whether the Specified User can Steal from the Specified Victim
  */
 public boolean canDoStealPlayerResource(int UserId, int victimId) {
   // Check if the user is the current player
   if (UserId != currentPlayer.getPlayerId()) {
     return false;
   }
   if (UserId == victimId) {
     return false;
   }
   Player victim = getPlayerByID(victimId);
   if (victim == null || victim.getResourceCardHandSize() <= 0) {
     return false;
   }
   return true;
 }
Exemple #12
0
  /**
   * This method Uses a development card and marks it as played
   *
   * @param devCardType is valid and you own that card
   * @return whether or not you just won the game
   * @throws Exception
   */
  public boolean useMonumentCard(int userID, DevCardType devCardType) throws Exception {
    if (currentPlayer.getPlayerId() == userID) {

      if (currentPlayer.canDoPlayDevelopmentCard(turnNumber, devCardType)) {
        // Marks the card as played
        currentPlayer.playDevelopmentCard(turnNumber, devCardType);
        currentPlayer.setHasPlayedDevCardThisTurn(true);

        // Now do what monuments do:
        currentPlayer.incrementVictoryPoints();
        setVersionNumber(versionNumber++);
        return doWeHaveAWinner();
      } else throw new Exception("Cannot Play Monument card!");
    } else throw new Exception("Cannot Play Monument card, you are not the current Player!");
  }
Exemple #13
0
 /**
  * This card is special because it is the only development card that references the bank, so
  * additional checks need to be made, and so for security reasons it gets its own method to do so.
  */
 public boolean canDoPlayerUseYearOfPlenty(ResourceType[] resourceType, int userID) {
   // This is important because the bank not have the cards they need.
   if (currentPlayer.getPlayerId() == userID) {
     if (resourceType.length == 1) {
       if (!canDoPlayerTake2OfResource(resourceType[0])) {
         return false;
       }
     } else {
       for (int i = 0; i < resourceType.length; i++) {
         if (!bank.canDoPlayerTakeResource(resourceType[i])) return false;
       }
     }
     return currentPlayer.canDoPlayDevelopmentCard(turnNumber, DevCardType.YEAR_OF_PLENTY);
   }
   return false;
 }
Exemple #14
0
 public void placeRoadOnEdge(int userId, EdgeLocation edgeLocation, boolean usingRoadBuilder)
     throws Exception {
   if (canDoPlaceRoadOnEdge(userId, edgeLocation) == false) {
     throw new Exception("Specified Player cannot place a road on the given edgeLocation");
   }
   // If we are in the setup phase, the rules for placing a road are slightly different
   if (usingRoadBuilder == true) {
     if (currentPlayer.getPlayerId() == userId
         && currentPlayer.canDoPlayDevelopmentCard(turnNumber, DevCardType.ROAD_BUILD)
         && currentPlayer.getNumberUnplayedRoads() >= 2) {
       board.placeRoadBuildRoadOnEdge(getCurrentPlayer(), edgeLocation);
       versionNumber++;
     } else {
       throw new Exception("Unable to play Road Builder!");
     }
   } else {
     board.placeRoadOnEdge(getCurrentPlayer(), edgeLocation);
     versionNumber++;
   }
 }
Exemple #15
0
 /**
  * TODO - Verify Completion
  *
  * <p>Checks to see if the given Player can place a road on the specified edge
  *
  * @pre None
  * @param UserId
  * @param edgeLocation
  * @return whether the Specified player can place a road on the specified edge
  */
 public boolean canDoPlaceRoadOnEdge(int UserId, EdgeLocation edgeLocation) {
   // Check if the user is the current player
   if (UserId != currentPlayer.getPlayerId()) {
     System.out.println("Game1: false");
     return false;
   }
   // If we are in the setup phase, the rules for placing a road are slightly different
   if (turnNumber == 0) {
     if (currentPlayer.getNumberUnplayedRoads() != 15) {
       return false;
     }
     return board.canDoPlaceInitialRoadOnEdge(getCurrentPlayer(), edgeLocation);
   } else if (turnNumber == 1) {
     if (currentPlayer.getNumberUnplayedRoads() != 14) {
       return false;
     }
     return board.canDoPlaceInitialRoadOnEdge(getCurrentPlayer(), edgeLocation);
   } else {
     return board.canDoPlaceRoadOnEdge(getCurrentPlayer(), edgeLocation);
   }
 }
Exemple #16
0
  private boolean endTurn() {
    boolean endSuccess = true;

    // Update Board
    if (!board.endTurn(getActivePlayerId())) {
      errorString = "Board failed to finish turn";
      endSuccess = false;
    }

    // Check win conidtion
    if (!board.checkWinCondition()) {

      // Update active player
      for (Player p : players) {
        if (!p.getPlayerName().equalsIgnoreCase(getActivePlayerName())) {
          this.setActivePlayerId(p.getPlayerId());
          break;
        }
      }

      if (playerNumberTurn == 1) {
        playerNumberTurn = 2;
      } else {
        playerNumberTurn = 1;
      }

    } else {
      isGameOver = true;
    }

    // Update DB
    if (!myDAO.endTurn(this)) {
      errorString = "DB Failed to Update!";
      endSuccess = false;
    }

    return endSuccess;
  }
Exemple #17
0
 /**
  * Returns whether it is currently the players turn or not
  *
  * @param UserId
  * @return
  */
 public boolean isPlayersTurn(int UserId) {
   if (UserId != currentPlayer.getPlayerId()) {
     return false;
   }
   return true;
 }
Exemple #18
0
  public boolean useSoldierCard(int userID, PlaySoldier_Params params) {
    // Do they have more than three soldiers? Do they have the most soldiers? If so, award them the
    // Largest Army award (assuming they don't already have it) and take it from the previous title
    // holder
    // Must move robber to a different hex

    if (currentPlayer.getPlayerId() == userID) {

      if (currentPlayer.canDoPlayDevelopmentCard(turnNumber, DevCardType.SOLDIER)) {
        if (canDoStealPlayerResource(userID, params.getVictimIndex())) {

          try {
            moveRobberToHex(userID, params.getLocation());
            stealPlayerResource(userID, params.getVictimIndex());
            currentPlayer.playDevelopmentCard(turnNumber, DevCardType.SOLDIER);
            currentPlayer.setHasPlayedDevCardThisTurn(true);
          } catch (Exception e) {
            System.out.println(
                "Something went wrong when trying to move the robber or steal resources");
            e.printStackTrace();
          }

          boolean firstTime = true;

          // Initial selection of LargestArmy recipient.
          if (currentPlayer.getNumberOfSoldiersPlayed() == 3) {
            for (int i = 0; i < players.length; i++) {
              if (players[i].getPlayerId() != currentPlayer.getPlayerId()
                  && players[i].getNumberOfSoldiersPlayed() >= 3) {
                firstTime = false;
              }
            }
            if (firstTime) {
              indexOfLargestArmy = currentPlayer.getPlayerIndex();
              largestArmy = currentPlayer;
              currentPlayer.incrementVictoryPoints();
              currentPlayer.incrementVictoryPoints();
            }
          }
          int test = 0;
          // Check for competition
          if (currentPlayer.getNumberOfSoldiersPlayed() >= 3 && !firstTime) {
            if (currentPlayer.getNumberOfSoldiersPlayed() > largestArmy.getNumberOfSoldiersPlayed()
                && currentPlayer.getPlayerId() != largestArmy.getPlayerId()) {
              // indexOfLargestArmy = currentPlayer.getPlayerId();

              for (int i = 0; i < players.length; i++) {
                if (largestArmy.getPlayerId() == players[i].getPlayerId()) {
                  players[i].decrementVictoryPoints();
                  players[i].decrementVictoryPoints();
                }
              }

              largestArmy = currentPlayer;
              currentPlayer.incrementVictoryPoints();
              currentPlayer.incrementVictoryPoints();
            }
          }
          // Did the Largest Army award win the game?!
          setVersionNumber(versionNumber++);
          return doWeHaveAWinner();
        }
      }
    }
    return doWeHaveAWinner();
  }
Exemple #19
0
  /**
   * TODO Javadoc and Implement
   *
   * @param devCardType
   * @param resourceType
   * @return
   * @throws Exception
   */
  public boolean useDevelopmentCard(
      int userID, DevCardType devCardType, ResourceType[] resourceType) throws Exception {

    if (currentPlayer.getPlayerId() == userID) {

      if (currentPlayer.canDoPlayDevelopmentCard(turnNumber, devCardType)) {
        switch (devCardType) {
          case MONOPOLY:
            if (resourceType == null || resourceType.length > 1) {
              throw new Exception("Trying to use monopoly on more than one resource!");
            }
            // Declare a Resource the player wants, and then extract it from all players who have
            // it.
            for (int i = 0; i < players.length; i++) {
              if (players[i] != currentPlayer) {
                // If not the current player, ask the player for an Array list of it's resource card
                // of that type
                currentPlayer
                    .conformToMonopoly(resourceType[0])
                    .addAll(players[i].conformToMonopoly(resourceType[0]));
                players[i].conformToMonopoly(resourceType[0]).clear();
              }
            }
            currentPlayer.playDevelopmentCard(turnNumber, DevCardType.MONOPOLY);
            currentPlayer.setHasPlayedDevCardThisTurn(true);
            setVersionNumber(versionNumber++);
            return doWeHaveAWinner();
          case YEAR_OF_PLENTY:
            if (canDoPlayerUseYearOfPlenty(resourceType, userID)) {
              // Add two resources of the types specified to the currentPlayers hand
              if (resourceType.length == 1) {
                ResourceCard resource = bank.playerTakeResource(resourceType[0]);
                // some sneaky idea that realizes conformToMonopoly returns the arraylist of that
                // players cards of a specified type.
                currentPlayer.conformToMonopoly(resourceType[0]).add(resource);
                ;
                // repeat
                resource = bank.playerTakeResource(resource.getResourceType());
                currentPlayer.conformToMonopoly(resourceType[0]).add(resource);
              } else if (resourceType.length == 2) {
                for (int g = 0; g < resourceType.length; g++) {
                  ResourceCard resource = bank.playerTakeResource(resourceType[g]);
                  // some sneaky idea that realizes conformToMonopoly returns the arraylist of that
                  // players cards of a specified type.
                  currentPlayer.conformToMonopoly(resourceType[g]).add(resource);
                }
              }
              currentPlayer.playDevelopmentCard(turnNumber, DevCardType.YEAR_OF_PLENTY);
              currentPlayer.setHasPlayedDevCardThisTurn(true);
              setVersionNumber(versionNumber++);
              return doWeHaveAWinner();
            } else {
              throw new Exception("The bank has not what you seek.");
            }
          default:
            throw new Exception("Wrong declaration for a development card of this type.");
        }
      }
    } else {
      throw new Exception("You cannot use a development Card");
    }

    return doWeHaveAWinner();
  }