/** * Moves the robber to the specified HexLocation, and retrieves the players that own municipals * next to that hex, excluding those that don't have resources to steal and the player that moved * the robber * * @pre canDoMoveRobberToHex != null * @param hex * @throws Exception * @return array of Players that are adjacent to the new Robber Hex, that have resources to steal. */ public Player[] moveRobberToHex(int UserId, HexLocation hexLocation) throws Exception { if (canDoMoveRobberToHex(UserId, hexLocation) == false) { throw new Exception("Cannot move Robber to that Hex"); } board.moveRobberToHex(hexLocation); Hex hex = board.getHex(hexLocation); Vertex[] adjacentVertices = hex.getAdjacentVertices(); ArrayList<Integer> adjacentPlayerIDs = new ArrayList<Integer>(); ArrayList<Player> adjacentPlayers = new ArrayList<Player>(); for (Vertex vertex : adjacentVertices) { // If the vertex has a municipal, that municipal is not owned by the player moving the robber // and the Player is not already in the list, and the Player has resources to be stolen if (vertex.getMunicipal() != null) { int municipalID = vertex.getMunicipal().getPlayer().getPlayerIndex(); Player player = vertex.getMunicipal().getPlayer(); if (vertex.hasMunicipal() && UserId != municipalID && !adjacentPlayerIDs.contains(municipalID) && player.getResourceCardHandSize() > 0) { adjacentPlayerIDs.add(municipalID); adjacentPlayers.add(player); } } } // Convert the found players into an array to return Player[] players = new Player[adjacentPlayers.size()]; return adjacentPlayers.toArray(players); }
public void addPlayer(int userID, CatanColor playerColor) { System.out.println("Game.addPlayer"); if (players == null) players = new Player[numberofPlayers]; int playerCount = 0; while (players[playerCount] != null) { playerCount++; if (playerCount == 4) { break; } } System.out.println("Player count == " + playerCount); if (playerCount < 4) { System.out.println("Adding new player."); Player p = new Player(playerCount, bank); p.setPlayerId(userID); p.setPlayerColor(playerColor); players[playerCount] = p; } if (playerCount == 4) { System.out.println("Beginning game."); status = "FirstRound"; } }
/** * 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; }
/** * Will return the number of the specified resource that the player currently has * * @pre None * @param resourceType * @return the number of the specified resource that the player currently has */ public int getNumberResourcesOfType(int UserId, ResourceType resourceType) { Player player = getPlayerByID(UserId); if (player != null) { return player.getNumberResourcesOfType(resourceType); } return 0; }
public int getNumberOfSoldiersPlayed(int userID) { Player player = getPlayerByID(userID); if (player == null) { return -1; } return player.getNumberOfSoldiersPlayed(); }
/** * This method will cycle through the array of players and will rotate them through the * currentPlayer so the turns can proceed. This will be helpful when the index of the player array * is [3] and we need to bring it back to [0] showing that that person is next. * * @pre the players array is not null * @post the next player is set. */ public void incrementPlayer() { // If we are done with the first two rounds of the Game (for setup for (int i = 0; i < numberofPlayers; i++) { if (currentPlayer.getPlayerIndex() == players[i].getPlayerIndex()) { // players[i] = currentPlayer; // This should probably be omitted // If we are no longer in the setup phase if (turnNumber > 1) { if (i == numberofPlayers - 1) { setCurrentPlayer(players[0]); currentPlayer.setHasDiscarded(false); turnNumber++; versionNumber++; status = "Rolling"; return; } else { setCurrentPlayer(players[i + 1]); currentPlayer.setHasDiscarded(false); versionNumber++; status = "Rolling"; return; } } // We must still be in the setup phase else { // We are still in the first round if (turnNumber == 0) { // If we are on the last person in the round, he/she gets to go again if (i == numberofPlayers - 1) { // setCurrentPlayer(players[numberofPlayers - 1]); // Could probably omit this line turnNumber++; versionNumber++; status = "SecondRound"; return; } else { setCurrentPlayer(players[i + 1]); versionNumber++; return; } } // We are still in the second round else if (turnNumber == 1) { if (i == 0) { setCurrentPlayer(players[0]); // inSetUpPhase = false; turnNumber++; versionNumber++; status = "Rolling"; return; } else { setCurrentPlayer(players[i - 1]); versionNumber++; return; } } } } } }
/** * The players all collect resources on the specified rollValue, from the given Bank * * @param rollValue * @throws Exception */ private void playersCollectResources(int rollValue) throws Exception { if (players == null) { throw new Exception("The players Array is null, cannot have players collect Resources"); } for (Player player : players) { player.collectResources(rollValue, bank); versionNumber++; } }
/** * This method does the actual trade by interacting with the bank and the Current Player to * ascertain the port type and what if they can trade or not. * * @pre the resource types it receives are valid, and that tradeIn is what the player performing * the trade already has * @throws Exception */ public void doMaritimeTrade(ResourceType tradeIn, ResourceType receive) throws Exception { if (canDoPlayerDoMaritimeTrade(tradeIn, receive)) { ResourceCard[] tradingCards = currentPlayer.prepareBankTrade(tradeIn); bank.playerTurnInResources(tradingCards); currentPlayer.getResourceCardHand().addCard(bank.playerTakeResource(receive)); } else throw new Exception("Cannot do Maritime Trade"); }
/** * 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; }
@Test public void testgoodInput() { FinishTurnInput input = new FinishTurnInput(0); Player testPlayer = model.getPlayer(new PlayerID(0)); try { // (int soldier, int monument, int monopoly, int yearOfPlenty, int roadBuild) testPlayer.getPlayerBank().setDC(new DevelopmentHand(0, 0, 0, 0, 0)); testPlayer.getPlayerBank().addNewDC(new DevelopmentHand(1, 1, 1, 1, 1)); } catch (BankException e) { // TODO Auto-generated catch block e.printStackTrace(); } FinishTurnCommand command = new FinishTurnCommand(); command.setGameModel(model); try { model = (GameModel) command.execute(new ObjectMapper().writeValueAsString(input)); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertEquals(testPlayer.getPlayerBank().getMonopoly().getQuantity(), 1); assertEquals(testPlayer.getPlayerBank().getMonument().getQuantity(), 1); assertEquals(testPlayer.getPlayerBank().getRoadBuild().getQuantity(), 1); assertEquals(testPlayer.getPlayerBank().getSoldier().getQuantity(), 1); assertEquals(testPlayer.getPlayerBank().getYearOfPlenty().getQuantity(), 1); assertEquals(testPlayer.getPlayerBank().getNewMonopoly().getQuantity(), 0); assertEquals(testPlayer.getPlayerBank().getNewMonument().getQuantity(), 0); assertEquals(testPlayer.getPlayerBank().getNewRoadBuild().getQuantity(), 0); assertEquals(testPlayer.getPlayerBank().getNewSoldier().getQuantity(), 0); assertEquals(testPlayer.getPlayerBank().getNewYearOfPlenty().getQuantity(), 0); assertEquals(testPlayer.getPlayerFacade().canBeRobbed(), true); }
/** * 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!"); }
/** * The Specified Player will discard the specified number of Resource Cards back to the bank from * his hand * * @pre canDoDiscardNumberOfResourceType != false * @param UserId * @param numberToDiscard * @param resourceType * @throws Exception * @post The Specified Player will have discarded the specified number of Resource Cards back to * the bank from his hand */ public void discardNumberOfResourceType( int UserId, int numberToDiscard, ResourceType resourceType) throws Exception { getPlayerByID(UserId).discardResourcesOfType(resourceType, numberToDiscard); getPlayerByID(UserId).setHasDiscarded(true); status = "Robbing"; System.out.println("Status: " + status); // If there is any player that has more than 7 cards and has not yet discarded this turn // must discard, so we set the game status to "Discarding" for (Player player : players) { if (player.getResourceCardHandSize() > 7 && player.isHasDiscarded() == false) { status = "Discarding"; System.out.println("Status: " + status); } } }
/** * 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); }
/** * The art of placing a settlement on the vertex. * * @pre the Can do is true * @param vertexLocation * @throws Exception * @post a settlement is placed on a vertex */ public void placeSettlementOnVertex(int UserId, VertexLocation vertexLocation) throws Exception { if (canDoPlaceSettlementOnVertex(UserId, vertexLocation) == false) { throw new Exception( "Cannot build Settlement on this vertex, this should not have been allowed to get this far."); } // If we are in the first two rounds of the game if (turnNumber < 2) { board.placeInitialSettlementOnVertex(currentPlayer, vertexLocation); versionNumber++; // Here we collect the resources for the second placed settlement in setup round if (turnNumber == 1) { Hex[] adjacentHexes = board.getVertex(vertexLocation).getAdjacentHexes(); for (Hex hex : adjacentHexes) { if (hex != null) { ResourceType hexResourceType = hex.getHexResourceType(); ResourceCard card = bank.playerTakeResource(hexResourceType); currentPlayer.getResourceCardHand().addCard(card); } } } } else { board.placeSettlementOnVertex(currentPlayer, vertexLocation); versionNumber++; } }
/** * 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); }
/** * 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; }
/** * Asks the player if he or she can build a settlement and tells the client that. * * @return */ public boolean canDoPlayerBuildSettlement(int UserId) { if (!isPlayersTurn(UserId)) { return false; } if (turnNumber == 0) { if (currentPlayer.getNumberUnplayedSettlements() != 5) { return false; } return true; } else if (turnNumber == 1) { if (currentPlayer.getNumberUnplayedSettlements() != 4) { return false; } return true; } return currentPlayer.canDoBuySettlement(); }
/** * The Specified User will Steal a Random ResourceCard from the specified Victim * * @pre canDoStealPlayerResource != false * @param UserId * @param victimId * @throws Exception * @post the Victim will have one less Resource Card, which the User will now have in possession */ public void stealPlayerResource(int UserId, int victimId) throws Exception { if (canDoStealPlayerResource(UserId, victimId) == false) { throw new Exception("canDoStealPlayerResource == false"); } currentPlayer.stealPlayerResource(getPlayerByID(victimId)); status = "Playing"; versionNumber++; }
public void setLargestArmy(Player largestArmy) { // Take the largest army away from the other player if he has it if (this.largestArmy != null) { this.largestArmy.setHasLargestArmy(false); } // Give to new player this.largestArmy = largestArmy; largestArmy.setHasLargestArmy(true); }
/** * This method polls the player to see if the player can build a road and then returns the result * to that which called it (most likely the client) * * @return a true or false to if the player can build a road there. */ public boolean canDoPlayerBuildRoad(int UserId) { if (!isPlayersTurn(UserId)) { return false; } if (turnNumber == 0) { if (currentPlayer.getNumberUnplayedRoads() != 15) { return false; } return true; } else if (turnNumber == 1) { if (currentPlayer.getNumberUnplayedRoads() != 14) { return false; } return true; } else { return currentPlayer.canDoBuyRoad(); } }
public void setLongestRoad(Player longestRoad) { // Take the longest road away from the other player if he has it if (this.longestRoad != null) { this.longestRoad.setHasLongestRoad(false); } // Give to new player this.longestRoad = longestRoad; longestRoad.setHasLongestRoad(true); }
public void setRollDice(int UserId, int rollValue) throws Exception { if (canDoRollDice(UserId) == false) { throw new Exception("canDoRollDice == false"); } if (rollValue == 7) { status = "Robbing"; // If there is any player that has more than 7 cards and has not yet discarded this turn // must discard, so we set the game status to "Discarding" for (Player player : players) { if (player.getResourceCardHandSize() > 7 && player.isHasDiscarded() == false) { status = "Discarding"; } } } else { status = "Playing"; playersCollectResources(rollValue); } versionNumber++; }
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++; } }
/** * 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); } }
/** * Does the player have cards to trade? * * @pre valid resource type * @param resourceType * @return yes you can trade or no you can't. */ public boolean canTradeResourcesToBank(ResourceType resourceType) { // Does the Player have cards to trade? // Does the Player have a port? // Does the Player have enough cards to trade? int size = currentPlayer.getResourceCardHand().getNumberResourcesOfType(resourceType); int rate = getTradeRate(resourceType); if (size >= rate) { return true; } else { return false; } }
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; }
@Test @Ignore public void testBuyDevCard() throws InvalidTypeException, Exception { final int guy = game.getCurrentTurn(); game.setPhase(TurnTracker.Phase.DISCARDING); final ResourceCard one = game.getResourceCard(ResourceType.WHEAT); final ResourceCard two = game.getResourceCard(ResourceType.ORE); final ResourceCard three = game.getResourceCard(ResourceType.SHEEP); game.giveResource(one, guy); game.giveResource(two, guy); game.giveResource(three, guy); Player p = game.getPlayerManager().getPlayerByID(guy); final int sizeold = p.quantityOfDevCards(); game.buyDevelopmentCard(guy); p = game.getPlayerManager().getPlayerByID(guy); final int sizenew = p.quantityOfDevCards(); assertTrue(sizenew == sizeold + 1); }
/** * This method returns a 3, 4, or 2 depending on the port type you have, if any. * * @pre valid resource type * @return */ public int getTradeRate(ResourceType resourceType) { return currentPlayer.getTradeRate(resourceType); }
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(); }
/** * 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(); }