Player added methods and javadoc
This commit is contained in:
parent
aef0479cf5
commit
bfb1433fd2
|
@ -1,6 +1,7 @@
|
||||||
package ch.zhaw.catan;
|
package ch.zhaw.catan;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* New Class PLayer
|
* New Class PLayer
|
||||||
|
@ -31,31 +32,79 @@ public class Player {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method returns all the resources the player has at the moment
|
* This method returns all the resources the player has at the moment
|
||||||
* @return HashMap
|
* @return HashMap with the count of every resource
|
||||||
*/
|
*/
|
||||||
public HashMap<Config.Resource,Integer> getResources() {
|
public HashMap<Config.Resource,Integer> getResources() {
|
||||||
return resources;
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method adds a specific resource to resourcess
|
||||||
|
* @param resource to add
|
||||||
|
* @param numberToAdd how much to add
|
||||||
|
*/
|
||||||
|
public void addResource(Config.Resource resource, int numberToAdd) {
|
||||||
|
resources.put(resource, resources.get(resource) + numberToAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method substracts a specific resource from resourcess but check first if player has enough resources.
|
||||||
|
* @param resource to substract
|
||||||
|
* @param numberToTake how much to substract
|
||||||
|
* @return true if resource has been substracted false if player has not enough resources
|
||||||
|
*/
|
||||||
|
public boolean substractResource(Config.Resource resource, int numberToTake) {
|
||||||
|
int inPossesion = resources.get(resource);
|
||||||
|
if(inPossesion - numberToTake < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
resources.put(resource,inPossesion - numberToTake);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method has to be used when a player wants to build a road. It checks if a player has enough roads
|
||||||
|
* and resources to build one more. If player is able to build, the method substract the buildcost from the resources
|
||||||
|
* in possesion by the player.
|
||||||
|
* @return true if road can be created false if road can't be created
|
||||||
|
*/
|
||||||
public boolean buildRoad() {
|
public boolean buildRoad() {
|
||||||
if (roadsToUse > 0) {
|
List<Config.Resource> costs = Config.Structure.ROAD.getCosts();
|
||||||
roadsToUse--;
|
if ( roadsToUse == 0 || !checkRessourceToBuild(costs)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (Config.Resource resource : costs) {
|
||||||
|
resources.put(resource,resources.get(resource)-1);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method has to be used when a player wants to build a settlement. It checks if a player has enough roads
|
||||||
|
* and resources to build one more. If player is able to build, the method substract the buildcost from the resources
|
||||||
|
* in possesion by the player.
|
||||||
|
* @return true if road can be created false if road can't be created
|
||||||
|
*/
|
||||||
public boolean buildSettlement() {
|
public boolean buildSettlement() {
|
||||||
if (settlementsToUse > 0) {
|
List<Config.Resource> costs = Config.Structure.SETTLEMENT.getCosts();
|
||||||
settlementsToUse--;
|
if ( settlementsToUse == 0 || !checkRessourceToBuild(costs)) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
for (Config.Resource resource : costs) {
|
||||||
|
resources.put(resource,resources.get(resource)-1);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns true if player has enough resources else false
|
||||||
|
private boolean checkRessourceToBuild(List<Config.Resource> liste) {
|
||||||
|
for (Config.Resource resource : liste) {
|
||||||
|
int possesion = resources.get(resource);
|
||||||
|
if (possesion == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue