Portfolio Code | Clement Colmerauer
Repositories
Site
Kata refactoring
Code
Commits
Branches
Tags
Search
Tree:
f5182d0
Branches
Tags
master
Kata refactoring
build
reports
jacoco
test
html
re.forestier.edu.rpg
Player.java.html
Rendu
Clement Colmerauer
commited
f5182d0
at 2025-05-08 09:53:51
Player.java.html
Blame
History
Raw
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="fr"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Player.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">kata</a> > <a href="index.source.html" class="el_package">re.forestier.edu.rpg</a> > <span class="el_source">Player.java</span></div><h1>Player.java</h1><pre class="source lang-java linenums">package re.forestier.edu.rpg; import java.util.Random; import java.util.ArrayList; import java.util.HashMap; import java.util.Collections; import re.forestier.edu.lib.Natural; public class Player { <span class="fc" id="L10"> private static final Natural defaultMaxHp = Natural.valueOf(20);</span> <span class="fc" id="L11"> private static final Natural[] xpForlevel = {</span> <span class="fc" id="L12"> Natural.valueOf(0),</span> <span class="fc" id="L13"> Natural.valueOf(10),</span> <span class="fc" id="L14"> Natural.valueOf(27),</span> <span class="fc" id="L15"> Natural.valueOf(57),</span> <span class="fc" id="L16"> Natural.valueOf(111)</span> }; //Level = i+1 //We consider that level is a player thing, not a job one. private String playerName; private String avatarName; public Jobs avatarClass; private Natural money; private Natural level; private Natural maxHealthPoint; private Natural currentHealthPoints; private Natural xp; private Natural maxWeight; private Natural weight; private final HashMap<Ability, Integer> abilities; //Ability = stat //SHOULDDO : have another hashmap to have contextual modifier (items,...) private ArrayList<Item> inventory; public Player(String playerName, String avatar_name, Jobs avatarClass, int money, ArrayList<Item> inventory, int maxWeight) { <span class="fc" id="L38"> this(playerName,avatar_name,avatarClass,money,inventory,Player.defaultMaxHp.toInt(),maxWeight);</span> <span class="fc" id="L39"> }</span> public Player(String playerName, String avatar_name, Jobs avatarClass, int money, ArrayList<Item> inventory, int maxHp, int maxWeight) <span class="fc" id="L42"> {</span> <span class="pc bpc" id="L43" title="1 of 2 branches missed."> assert playerName != null : "Player name can't be null";</span> <span class="pc bpc" id="L44" title="1 of 2 branches missed."> assert avatar_name != null : "Avatar name can't be null";</span> <span class="pc bpc" id="L45" title="1 of 2 branches missed."> assert avatarClass != null : "Avatar class can't be null";</span> <span class="pc bpc" id="L46" title="1 of 2 branches missed."> assert inventory != null : "Inventory can't be null";</span> <span class="fc" id="L48"> this.playerName = playerName;</span> <span class="fc" id="L49"> this.avatarName = avatar_name;</span> <span class="fc" id="L50"> this.avatarClass = avatarClass;</span> <span class="fc" id="L51"> this.money = Natural.valueOf(money);</span> <span class="fc" id="L52"> this.inventory = inventory;</span> <span class="fc" id="L53"> this.level = Natural.valueOf(1);</span> <span class="fc" id="L54"> this.xp = Natural.valueOf(0);</span> <span class="fc" id="L55"> this.abilities = new HashMap<Ability, Integer>();</span> <span class="fc" id="L56"> avatarClass.getAbilityPerLevel().forEach((ability,value)-></span> <span class="fc" id="L57"> {this.abilities.put(ability,value[0]);}</span> ); <span class="fc" id="L59"> this.maxWeight = Natural.valueOf(maxWeight);</span> <span class="fc" id="L60"> this.weight = Natural.valueOf(0);</span> <span class="fc bfc" id="L61" title="All 2 branches covered."> for(Item i : inventory)</span> { <span class="fc" id="L63"> this.weight.add(i.getWeight());</span> <span class="fc" id="L64"> }</span> <span class="pc bpc" id="L65" title="1 of 2 branches missed."> if(this.weight.compareTo(this.maxWeight) == -1)</span> { <span class="nc" id="L67"> throw new UnsupportedOperationException("The player have too much to carry in theyr inventory");</span> } <span class="fc" id="L69"> this.maxHealthPoint = Natural.valueOf(maxHp);</span> <span class="fc" id="L70"> this.currentHealthPoints = Natural.valueOf(maxHp);</span> <span class="fc" id="L71"> }</span> public String getPlayerName() { <span class="fc" id="L75"> return this.playerName;</span> } public String getAvatarName() { <span class="fc" id="L80"> return this.avatarName;</span> } public Integer getMoney() { <span class="fc" id="L85"> return this.money.toInt();</span> } public int getLevel() { <span class="fc" id="L90"> return this.level.toInt();</span> } public int getMaxHealthPoints() { <span class="fc" id="L95"> return this.maxHealthPoint.toInt();</span> } public int getCurrentHealthPoints() { <span class="fc" id="L100"> return this.currentHealthPoints.toInt();</span> } public HashMap<Ability,Integer> getAbilities() { <span class="fc" id="L105"> HashMap<Ability,Integer> copy = new HashMap<Ability,Integer>(); //In doubt, deep copy</span> <span class="fc" id="L106"> this.abilities.forEach((key,value) -> {</span> <span class="fc" id="L107"> copy.put(key,value);}</span> ); <span class="fc" id="L109"> return copy;</span> } public ArrayList<Item> getInventory() { <span class="fc" id="L114"> return new ArrayList<Item>(this.inventory);</span> } public void heal(int hp) { <span class="fc bfc" id="L119" title="All 2 branches covered."> if(hp < 0 )</span> { <span class="fc" id="L121"> throw new IllegalArgumentException();</span> } <span class="fc" id="L123"> this.currentHealthPoints.add(Natural.valueOf(hp));</span> <span class="fc bfc" id="L124" title="All 2 branches covered."> if(this.currentHealthPoints.compareTo(this.maxHealthPoint) == -1)</span> { <span class="fc" id="L126"> this.currentHealthPoints = (Natural)this.maxHealthPoint.clone();</span> } <span class="fc" id="L128"> }</span> public void addItem(Item i) { <span class="nc bnc" id="L132" title="All 2 branches missed."> assert i != null : "Argument must be non null";</span> <span class="nc" id="L133"> Natural w = (Natural)this.weight.clone();</span> <span class="nc" id="L134"> w.add(i.getWeight());</span> <span class="nc bnc" id="L135" title="All 2 branches missed."> if(w.compareTo(this.maxWeight) == -1)</span> { <span class="nc" id="L137"> throw new UnsupportedOperationException("Player can't carry more.");</span> } <span class="nc" id="L141"> this.weight.add(i.getWeight());</span> <span class="nc" id="L142"> this.inventory.add(i);</span> <span class="nc" id="L143"> }</span> public void removeItem(Item i) { <span class="pc bpc" id="L147" title="1 of 2 branches missed."> assert i != null : "Argument must be non null";</span> <span class="pc bpc" id="L148" title="1 of 2 branches missed."> if(!this.inventory.contains(i))</span> { <span class="nc" id="L150"> throw new UnsupportedOperationException("Can't remove what player don't have");</span> } <span class="fc" id="L152"> this.weight.substract(i.getWeight());</span> <span class="fc" id="L153"> this.inventory.remove(i);</span> <span class="fc" id="L154"> }</span> public void buy(Item i) { <span class="pc bpc" id="L158" title="1 of 2 branches missed."> assert i != null : "Argument must be non null";</span> <span class="fc bfc" id="L159" title="All 2 branches covered."> if(this.money.compareTo(i.getValue()) == 1)</span> { <span class="fc" id="L161"> throw new UnsupportedOperationException("Player doesn't have enough money.");</span> } <span class="fc" id="L164"> Natural w = (Natural)this.weight.clone();</span> <span class="fc" id="L165"> w.add(i.getWeight());</span> <span class="pc bpc" id="L166" title="1 of 2 branches missed."> if(w.compareTo(this.maxWeight) == -1)</span> { <span class="nc" id="L168"> throw new UnsupportedOperationException("Player can't carry more.");</span> } <span class="fc" id="L172"> this.removeMoney(i.getValue().toInt());</span> <span class="fc" id="L173"> this.weight.add(i.getWeight());</span> <span class="fc" id="L174"> this.inventory.add(i);</span> <span class="fc" id="L175"> }</span> public void sell(Item i) { <span class="pc bpc" id="L179" title="1 of 2 branches missed."> assert i != null : "Argument must be non null";</span> <span class="fc bfc" id="L180" title="All 2 branches covered."> if(!this.inventory.contains(i))</span> { <span class="fc" id="L182"> throw new UnsupportedOperationException("Player can't sell what they don't own.");</span> } <span class="fc" id="L185"> this.inventory.remove(i);</span> <span class="fc" id="L186"> this.weight.substract(i.getWeight());</span> <span class="fc" id="L187"> this.addMoney(i.getValue().toInt());</span> <span class="fc" id="L188"> }</span> public void hurt(int damage) { <span class="fc bfc" id="L192" title="All 2 branches covered."> if(damage < 0 )</span> { <span class="fc" id="L194"> throw new IllegalArgumentException();</span> } <span class="fc" id="L196"> this.currentHealthPoints.substract(Natural.valueOf(damage));</span> <span class="fc" id="L197"> }</span> public int getXp() { <span class="fc" id="L202"> return this.xp.toInt();</span> } public Jobs getAvatarClass () { <span class="fc" id="L206"> return this.avatarClass;</span> } public void addXp(int xp) { <span class="fc" id="L210"> Natural ancientLevel = (Natural)this.level.clone();</span> <span class="fc" id="L211"> this.xp.add(Natural.valueOf(xp));</span> <span class="fc" id="L212"> int i = 0;</span> <span class="fc bfc" id="L213" title="All 4 branches covered."> while(i < xpForlevel.length && this.xp.compareTo(xpForlevel[i]) <= 0)</span> { <span class="fc" id="L215"> i++;</span> } <span class="fc" id="L218"> this.level = Natural.valueOf(i);</span> <span class="fc bfc" id="L220" title="All 2 branches covered."> if (!ancientLevel.equals(this.level)) {</span> // Player leveled-up! // Give a random object <span class="fc" id="L223"> Random random = new Random();</span> <span class="fc" id="L224"> this.inventory.add(Manager.objectList[random.nextInt(Manager.objectList.length)]);</span> // Add/upgrade abilities to player <span class="fc" id="L227"> avatarClass.getAbilityPerLevel().forEach((ability,value)-></span> <span class="fc" id="L228"> {this.abilities.put(ability,value[this.level.toInt()-1]);}</span> ); } <span class="fc" id="L231"> }</span> public void removeMoney(int amount) { <span class="fc" id="L235"> Natural toRemove = Natural.valueOf(amount);</span> <span class="fc bfc" id="L236" title="All 2 branches covered."> if (this.money.compareTo(toRemove) == 1) </span> { <span class="fc" id="L238"> throw new IllegalArgumentException("Player can't have a negative money!");</span> } <span class="fc" id="L240"> this.money.substract(toRemove);</span> <span class="fc" id="L241"> }</span> public void addMoney(int amount) { <span class="fc" id="L244"> Natural toAdd = Natural.valueOf(amount);</span> <span class="fc" id="L245"> this.money.add(toAdd);</span> <span class="fc" id="L246"> }</span> @Override public String toString() { <span class="fc" id="L251"> StringBuilder sb = new StringBuilder("Joueur ");</span> <span class="fc" id="L252"> sb.append(this.avatarName);</span> <span class="fc" id="L253"> sb.append(" joué par ");</span> <span class="fc" id="L254"> sb.append(this.playerName);</span> <span class="fc" id="L255"> sb.append("\nNiveau : ");</span> <span class="fc" id="L256"> sb.append(this.level.toInt());</span> <span class="fc" id="L257"> sb.append(" (XP totale : ");</span> <span class="fc" id="L258"> sb.append(this.xp);</span> <span class="fc" id="L259"> sb.append(")\n\nCapacités :");</span> <span class="fc" id="L261"> ArrayList<Ability> abilitiesKey = new ArrayList<Ability>(this.abilities.keySet());</span> <span class="fc" id="L262"> Collections.sort(abilitiesKey);</span> <span class="fc bfc" id="L263" title="All 2 branches covered."> for(Ability a : abilitiesKey)</span> { <span class="fc bfc" id="L265" title="All 2 branches covered."> if(this.abilities.get(a) != 0)</span> { <span class="fc" id="L267"> sb.append("\n " + a.toString() + " : " + this.abilities.get(a));</span> } <span class="fc" id="L269"> } </span> <span class="fc" id="L271"> sb.append("\n\nInventaire :");</span> <span class="fc" id="L272"> this.inventory.forEach(item -> {</span> <span class="nc" id="L273"> sb.append("\n " + item);</span> <span class="nc" id="L274"> });</span> <span class="fc" id="L275"> return sb.toString();</span> } public String toMarkDown() { <span class="nc" id="L280"> StringBuilder sb = new StringBuilder("#Joueur ");</span> <span class="nc" id="L281"> sb.append(this.avatarName);</span> <span class="nc" id="L282"> sb.append("#\n**Joué par ");</span> <span class="nc" id="L283"> sb.append(this.playerName);</span> <span class="nc" id="L284"> sb.append("**\n**Niveau : ");</span> <span class="nc" id="L285"> sb.append(this.level.toInt());</span> <span class="nc" id="L286"> sb.append("** (XP totale : ");</span> <span class="nc" id="L287"> sb.append(this.xp);</span> <span class="nc" id="L288"> sb.append(")\n\n##Capacités :##");</span> <span class="nc" id="L290"> ArrayList<Ability> abilitiesKey = new ArrayList<Ability>(this.abilities.keySet());</span> <span class="nc" id="L291"> Collections.sort(abilitiesKey);</span> <span class="nc bnc" id="L292" title="All 2 branches missed."> for(Ability a : abilitiesKey)</span> { <span class="nc bnc" id="L294" title="All 2 branches missed."> if(this.abilities.get(a) != 0)</span> { <span class="nc" id="L296"> sb.append("\n*" + a.toString() + " : " + this.abilities.get(a));</span> } <span class="nc" id="L298"> } </span> <span class="nc" id="L300"> sb.append("\n\n##Inventaire :##");</span> <span class="nc" id="L301"> this.inventory.forEach(item -> {</span> <span class="nc" id="L302"> sb.append("\n*" + item);</span> <span class="nc" id="L303"> });</span> <span class="nc" id="L304"> return sb.toString();</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.11.202310140853</span></div></body></html>