Files
Progetto-ingegneria-del-sof…/src/main/java/it/polimi/ingsw/gc14/Model/Player.java
T

343 lines
11 KiB
Java

package it.polimi.ingsw.gc14.Model;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.*;
import it.polimi.ingsw.gc14.View.TUI.AsciiTable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import static it.polimi.ingsw.gc14.View.TUI.BorderStyle.*;
/**
* Default Player class; contains all identifiers and methods needed.
*/
public class Player implements Serializable {
/**
* The maximum length allowed for the username string.
*/
private static final int MAX_VALUE = 32;
/**
* Identifier for the {@code Player} when displaying the game through the GUI.
* @see Totems
*/
private Totems totem;
// region Getters
/**
* Unique string identifier for players; must not be {@code null} and must not
* exceed {@link #MAX_VALUE} otherwise an {@link IllegalArgumentException} will be thrown
* when the {@link #Player(String) constructor} is called.
*/
private final String UserName;
/**
* Getter for the private attribute {@link #UserName}.
* @return {@code String} - The UserName
*/
public String getUserName() {
return UserName;
}
public Totems getTotem() {
return totem;
}
/**
* Counts the total {@link it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character Character Cards}
* the player currently possesses.
* @return {@code int} - Total Character Cards.
*/
public int getTotCharacters() {
return Arrays.stream(CharacterType.values())
.mapToInt(this::getNType)
.sum();
}
/**
* Counts the total number of cards of the specified {@link it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType Character Type}.
* Character Type must not be {@code null}
* @param type the Character Type to be Counted.
* @return {@code int} - Total number of cards of given {@code type}.
*/
public int getNType(CharacterType type) {
return switch (type) {
case ARTIST -> artists.size();
case INVENTOR -> inventors.size();
case HUNTER -> hunters.size();
case BUILDER -> builders.size();
case SHAMAN -> shamans.size();
case GATHERER -> gatherers.size();
};
}
private ArrayList<BuildingCard> buildingCards;
private ArrayList<Artist> artists;
private ArrayList<Builder> builders;
private ArrayList<Inventor> inventors;
private ArrayList<Gatherer> gatherers;
private ArrayList<Shaman> shamans;
private ArrayList<Hunter> hunters;
public ArrayList<BuildingCard> getBuildingCards() { return buildingCards; }
public ArrayList<Artist> getArtists() { return artists; }
public ArrayList<Builder> getBuilders() { return builders; }
public ArrayList<Inventor> getInventors() { return inventors; }
public ArrayList<Gatherer> getGatherers() { return gatherers; }
public ArrayList<Shaman> getShamans() { return shamans; }
public ArrayList<Hunter> getHunters() { return hunters; }
/**
* The current amount of {@code Food} the Player possesses.
* Cannot be negative.
*/
private int FoodValue;
/**
* Getter for the private attribute {@link #FoodValue}.
* @return {@code int} - The amount of Food the player possesses.
*/
public int getFoodValue() {
return FoodValue;
}
/**
* The current amount of {@code Prestige} the Player possesses.
*/
private int PrestigeValue;
/**
* Getter for the private attribute {@link #PrestigeValue}.
* @return {@code int} - The amount of Prestige the Player possesses.
*/
public int getPrestigeValue() {
return PrestigeValue;
}
// endregion getters
// region Setters
/**
* Adds {@code Value} amount of {@code Food} to the Player.
* @param Value The amount of {@code Food} to be added.
* Should be positive for expected results
* (otherwise the method will subtract the absolute
* value of {@code Value}).
* @see #FoodValue
*/
public void addFood(int Value){
this.FoodValue += Value;
}
public void setTotem(Totems totem) {
this.totem = totem;
}
/**
* Removes {@code Value} amount of {@code Food} from the Player.
* Note: {@link #FoodValue} cannot be negative, so the method returns
* {@code false} if {@code Value} is greater than the amount of {@code Food}
* the Player possesses, and {@code true} otherwise.
*
* @param Value The amount of {@code Food} to be removed.
* Should be positive for expected results
* (otherwise the method will add the absolute
* value of {@code Value}).
* @return {@code Boolean} - {@code true} if the Food is successfully removed,
* {@code false} otherwise.
* @see #FoodValue
*/
public Boolean removeFood(int Value){
if(Value > this.FoodValue){
return false;
}
this.FoodValue -= Value;
return true;
}
/**
* Adds {@code Value} amount of {@code Prestige} to the Player.
* @param Value The amount of {@code Prestige} to be added.
* Should be positive for expected results
* (otherwise the method will subtract the absolute
* value of {@code Value}).
* @see #PrestigeValue
*/
public void addPrestige(int Value){
this.PrestigeValue += Value;
}
/**
* Removes {@code Value} amount of {@code Prestige} to the Player.
* @param Value The amount of {@code Prestige} to be removed.
* Should be positive for expected results
* (otherwise the method will add the absolute
* value of {@code Value}).
* @see #PrestigeValue
*/
public void removePrestige(int Value){
this.PrestigeValue -= Value;
}
// endregion setters
// region Constructors
/**
* Constructor for the class {@code Player}. Each Player is uniquely identified by the {@link #UserName}.
*
* @param UserName Unique String identifier for a Player.
* @throws IllegalArgumentException when {@code UserName} is empty or exceeds {@link #MAX_VALUE},
* with message:
* <pre>{@code UserName is empty or exceeds maximum permitted length.}</pre>
*
* @see #UserName
* @see #MAX_VALUE
*/
public Player(String UserName) throws IllegalArgumentException {
if(UserName.isEmpty() || UserName.length() > MAX_VALUE) {
throw new IllegalArgumentException("UserName is empty or exceeds maximum permitted length.");
}
this.UserName = UserName;
this.inventors = new ArrayList <>();
this.builders = new ArrayList <>();
this.gatherers = new ArrayList <>();
this.shamans = new ArrayList <>();
this.hunters = new ArrayList <>();
this.artists = new ArrayList<>();
this.buildingCards = new ArrayList<>();
this.FoodValue = 0;
this.PrestigeValue = 0;
this.totem = null;
}
// endregion constructors
// region Functions
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Player)) return false;
return UserName.equals(((Player) obj).UserName);
}
@Override
public int hashCode() {
return UserName.hashCode();
}
/**
* Prints the {@code Player}'s attributes for the {@code Board}'s representation.
* Uses the {@code UNICODE} border style.
* <p><b>Includes:</b>
* <li>{@link #FoodValue Food}
* <li>{@link #PrestigeValue Prestige}
* <li>{@link #artists Artists}
* <li>{@link #builders Builders}
* <li>{@link #gatherers Gatherers}
* <li>{@link #shamans Shamans}
* <li>{@link #inventors Inventors}
* <li>{@link #hunters Hunters}
* <li>{@link #buildingCards Buildings}
* </p>
* @return {@code String} - A string representation of the {@code Player}'s attributes.
* @see it.polimi.ingsw.gc14.View.TUI.BorderStyle BorderStyle
*/
@Override
public String toString() {
int Last = -1;
var table = new AsciiTable(UNICODE, 1);
table.addHeader(this.getUserName());
table.addRow("RESOURCES:");
if(this.totem != null) {
table.addRow("Totem: " + this.totem.toString());
}
table.addRow("Food: " + this.getFoodValue());
table.addRow("Prestige: " + this.getPrestigeValue());
table.addSeparator();
if(!this.hunters.isEmpty()){
Last = 6;
}
else if(!this.inventors.isEmpty()){
Last = 5;
}
else if(!this.shamans.isEmpty()){
Last = 4;
}
else if(!this.gatherers.isEmpty()){
Last = 3;
}
else if(!this.builders.isEmpty()){
Last = 2;
}
else if(!this.artists.isEmpty()){
Last = 1;
}
else{
Last = 0;
}
if(Last == 0){
table.addSeparator();
}
table.addRow("CHARACTERS:");
if(!this.artists.isEmpty()){
table.addRow("Artists: " + this.artists.toString());
if(Last == 1){
table.addSeparator();
}
}
if(!this.builders.isEmpty()){
table.addRow("Builders: " + this.builders.toString());
if(Last == 2){
table.addSeparator();
}
}
if(!this.gatherers.isEmpty()){
table.addRow("Gatherers: " + this.gatherers.toString());
if(Last == 3){
table.addSeparator();
}
}
if(!this.shamans.isEmpty()){
table.addRow("Shamans: " + this.shamans.toString());
if(Last == 4){
table.addSeparator();
}
}
if(!this.inventors.isEmpty()){
table.addRow("Inventors: " + this.inventors.toString());
if(Last == 5){
table.addSeparator();
}
}
if(!this.hunters.isEmpty()){
table.addRow("Hunters: " + this.hunters.toString());
table.addSeparator();
}
table.addRow("BUILDING CARDS:");
if(!this.buildingCards.isEmpty()){
table.addRow("Buildings: " + this.buildingCards.toString());
}
return table.build();
}
// endregion functions
}