Portfolio Code | Clement Colmerauer
Repositories
Site
Kata refactoring
Code
Commits
Branches
Tags
Search
Tree:
f6f626b
Branches
Tags
master
Kata refactoring
build
reports
jacoco
test
html
re.forestier.edu.rpg
Player.java.html
enlever divide de Natural
Clement COLMERAUER
commited
f6f626b
at 2024-10-24 16:52:13
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 re.forestier.edu.lib.Natural; public class Player { <span class="fc" id="L9"> private static final Natural defaultMaxHp = Natural.valueOf(20);</span> <span class="fc" id="L10"> private static final Integer[] xpForlevel = {0,10,27,57,111}; //Level = i+1 </span> //TODO : add level private String playerName; private String avatarName; private String avatarClass; private Natural money; private Natural level; private Natural maxHealthPoint; private Natural currentHealthPoints; private Natural xp; public HashMap<String, Integer> abilities; //Ability = stat public ArrayList<String> inventory; public Player(String playerName, String avatar_name, String avatarClass, int money, ArrayList<String> inventory) { <span class="fc" id="L28"> this(playerName,avatar_name,avatarClass,money,inventory,Player.defaultMaxHp.toInt());</span> <span class="fc" id="L29"> }</span> private Player() { //Here to prevent the compilator to create default constructor } public Player(String playerName, String avatar_name, String avatarClass, int money, ArrayList<String> inventory, int maxHp) <span class="fc" id="L37"> {</span> <span class="fc bfc" id="L38" title="All 6 branches covered."> if (!avatarClass.equals("ARCHER") && !avatarClass.equals("ADVENTURER") && !avatarClass.equals("DWARF")) </span> { <span class="fc" id="L40"> return;</span> } <span class="fc" id="L43"> this.playerName = playerName;</span> <span class="fc" id="L44"> this.avatarName = avatar_name;</span> <span class="fc" id="L45"> this.avatarClass = avatarClass;</span> <span class="fc" id="L46"> this.money = Natural.valueOf(money);</span> <span class="fc" id="L47"> this.inventory = inventory;</span> <span class="fc" id="L48"> this.level = Natural.valueOf(1);</span> <span class="fc" id="L49"> this.xp = Natural.valueOf(0);</span> <span class="fc" id="L50"> this.abilities = UpdatePlayer.abilitiesPerTypeAndLevel().get(this.avatarClass).get(1);</span> <span class="fc" id="L51"> this.maxHealthPoint = Natural.valueOf(maxHp);</span> <span class="fc" id="L52"> this.currentHealthPoints = Natural.valueOf(maxHp);</span> <span class="fc" id="L53"> }</span> public String getPlayerName() { <span class="fc" id="L57"> return this.playerName;</span> } public String getAvatarName() { <span class="fc" id="L62"> return this.avatarName;</span> } public Integer getMoney() { <span class="fc" id="L67"> return this.money.toInt();</span> } public int getLevel() { <span class="fc" id="L72"> return this.level.toInt();</span> } public int getMaxHealthPoints() { <span class="fc" id="L77"> return this.maxHealthPoint.toInt();</span> } public int getCurrentHealthPoints() { <span class="fc" id="L82"> return this.currentHealthPoints.toInt();</span> } public void heal(int hp) { <span class="fc bfc" id="L87" title="All 2 branches covered."> if(hp < 0 )</span> { <span class="fc" id="L89"> throw new IllegalArgumentException();</span> } <span class="fc" id="L91"> this.currentHealthPoints.add(Natural.valueOf(hp));</span> <span class="fc bfc" id="L92" title="All 2 branches covered."> if(this.currentHealthPoints.compareTo(this.maxHealthPoint) == -1)</span> { <span class="fc" id="L94"> this.currentHealthPoints = (Natural)this.maxHealthPoint.clone();</span> } <span class="fc" id="L96"> }</span> public void hurt(int damage) { <span class="fc bfc" id="L100" title="All 2 branches covered."> if(damage < 0 )</span> { <span class="fc" id="L102"> throw new IllegalArgumentException();</span> } <span class="fc" id="L104"> this.currentHealthPoints.substract(Natural.valueOf(damage));</span> <span class="fc" id="L105"> }</span> public int getXp() { <span class="fc" id="L110"> return this.xp.toInt();</span> } public String getAvatarClass () { <span class="fc" id="L114"> return this.avatarClass;</span> } public void addXp(int xp) { <span class="fc" id="L118"> Natural ancientLevel = (Natural)this.level.clone();</span> <span class="fc" id="L119"> this.xp.add(Natural.valueOf(xp));</span> <span class="fc" id="L120"> int i = 0;</span> <span class="fc bfc" id="L121" title="All 4 branches covered."> while(i < xpForlevel.length && this.xp.toInt() >= xpForlevel[i])</span> { <span class="fc" id="L123"> i++;</span> } <span class="fc" id="L126"> this.level = Natural.valueOf(i);</span> <span class="fc bfc" id="L128" title="All 2 branches covered."> if (!ancientLevel.equals(this.level)) {</span> // Player leveled-up! // Give a random object ; <span class="fc" id="L132"> Random random = new Random();</span> <span class="fc" id="L133"> this.inventory.add(UpdatePlayer.objectList[random.nextInt(UpdatePlayer.objectList.length)]);</span> // Add/upgrade abilities to player <span class="fc" id="L136"> HashMap<String, Integer> abilities = UpdatePlayer.abilitiesPerTypeAndLevel().get(this.getAvatarClass()).get(this.level.toInt());</span> <span class="fc" id="L137"> abilities.forEach((ability, level) -> {</span> <span class="fc" id="L138"> this.abilities.put(ability, abilities.get(ability));</span> <span class="fc" id="L139"> });</span> } <span class="fc" id="L141"> }</span> public void removeMoney(int amount) { <span class="fc" id="L145"> Natural toRemove = Natural.valueOf(amount);</span> <span class="fc bfc" id="L146" title="All 2 branches covered."> if (this.money.compareTo(toRemove) == 1) </span> { <span class="fc" id="L148"> throw new IllegalArgumentException("Player can't have a negative money!");</span> } <span class="fc" id="L150"> this.money.substract(toRemove);</span> <span class="fc" id="L151"> }</span> public void addMoney(int amount) { <span class="fc" id="L154"> Natural toAdd = Natural.valueOf(amount);</span> <span class="fc" id="L155"> this.money.add(toAdd);</span> <span class="fc" id="L156"> }</span> @Override public String toString() { <span class="fc" id="L161"> StringBuilder sb = new StringBuilder("Joueur ");</span> <span class="fc" id="L162"> sb.append(this.avatarName);</span> <span class="fc" id="L163"> sb.append(" joué par ");</span> <span class="fc" id="L164"> sb.append(this.playerName);</span> <span class="fc" id="L165"> sb.append("\nNiveau : ");</span> <span class="fc" id="L166"> sb.append(this.level.toInt());</span> <span class="fc" id="L167"> sb.append(" (XP totale : ");</span> <span class="fc" id="L168"> sb.append(this.xp);</span> <span class="fc" id="L169"> sb.append(")\n\nCapacités :");</span> <span class="fc" id="L170"> this.abilities.forEach((name, level) -> {</span> <span class="fc" id="L171"> sb.append("\n " + name + " : " + level);</span> <span class="fc" id="L172"> });</span> <span class="fc" id="L173"> sb.append("\n\nInventaire :");</span> <span class="fc" id="L174"> this.inventory.forEach(item -> {</span> <span class="nc" id="L175"> sb.append("\n " + item);</span> <span class="nc" id="L176"> });</span> <span class="fc" id="L177"> 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>