integration entier naturel et ajout surcharge opérateur (ne marche pas)
Clement COLMERAUER

Clement COLMERAUER commited on 2024-10-07 12:16:36
Showing 8 changed files, with 46 additions and 39 deletions.

... ...
@@ -2,18 +2,19 @@ package re.forestier.edu;
2 2
 import re.forestier.edu.rpg.Affichage;
3 3
 import re.forestier.edu.rpg.UpdatePlayer;
4 4
 import re.forestier.edu.rpg.Player;
5
+import re.forestier.edu.lib.Natural;
5 6
 
6 7
 import java.util.ArrayList;
7 8
 
8 9
 public class Main {
9 10
     public static void main(String[] args) {
10 11
         Player firstPlayer = new Player("Florian", "Ruzberg de Rivehaute", "DWARF", 200, new ArrayList<>());
11
-        firstPlayer.addMoney(400);
12
+        firstPlayer.addMoney(Natural.valueOf(400));
12 13
 
13
-        firstPlayer.addXp(15);
14
+        firstPlayer.addXp(Natural.valueOf(15));
14 15
         System.out.println(Affichage.afficherJoueur(firstPlayer));
15 16
         System.out.println("------------------");
16
-        firstPlayer.addXp(20);
17
+        firstPlayer.addXp(Natural.valueOf(20));
17 18
         System.out.println(Affichage.afficherJoueur(firstPlayer));
18 19
     }
19 20
 }
20 21
\ No newline at end of file
... ...
@@ -3,6 +3,7 @@ package re.forestier.edu.rpg;
3 3
 import java.util.Random;
4 4
 import java.util.ArrayList;
5 5
 import java.util.HashMap;
6
+import re.forestier.edu.lib.Natural;
6 7
 
7 8
 public class Player {
8 9
     private static final Integer[] xpForlevel = {0,10,27,57,111}; //Level = i+1 
... ...
@@ -12,13 +13,12 @@ public class Player {
12 13
     private String Avatar_name;
13 14
     private String AvatarClass;
14 15
 
15
-    private Integer money;
16
-    private Float __real_money__;
16
+    private Natural money;
17 17
 
18
-    private int level;
19
-    private int healthpoints;
20
-    private int currenthealthpoints;
21
-    private int xp;
18
+    private Natural level;
19
+    private Natural healthpoints;
20
+    private Natural currenthealthpoints;
21
+    private Natural xp;
22 22
 
23 23
     public HashMap<String, Integer> abilities; //Ability = stat
24 24
     public ArrayList<String> inventory;
... ...
@@ -32,10 +32,10 @@ public class Player {
32 32
         this.playerName = playerName;
33 33
         Avatar_name = avatar_name;
34 34
         AvatarClass = avatarClass;
35
-        this.money = Integer.valueOf(money);
35
+        this.money = Natural.valueOf(money);
36 36
         this.inventory = inventory;
37
-        this.level = 1;
38
-        this.xp = 0;
37
+        this.level = Natural.valueOf(1);
38
+        this.xp = Natural.valueOf(0);
39 39
         this.abilities = UpdatePlayer.abilitiesPerTypeAndLevel().get(AvatarClass).get(1);
40 40
     }
41 41
 
... ...
@@ -49,42 +49,42 @@ public class Player {
49 49
         return this.Avatar_name;
50 50
     }
51 51
             
52
-    public Integer getMoney()
52
+    public Natural getMoney()
53 53
     {
54 54
         return this.money;
55 55
     }
56 56
 
57
-    public int getLevel()
57
+    public Natural getLevel()
58 58
     {
59 59
         return this.level;
60 60
     }
61 61
 
62
-    public int getHealthPoints()
62
+    public Natural getHealthPoints()
63 63
     {
64 64
         return this.healthpoints;
65 65
     }
66 66
 
67
-    public void setHealthPoints(int hp)
67
+    public void setHealthPoints(Natural hp)
68 68
     {
69
-        this.healthpoints = hp;
69
+        this.healthpoints = hp.clone();
70 70
     }
71 71
 
72
-    public int getCurrentHealthPoints()
72
+    public Natural getCurrentHealthPoints()
73 73
     {
74
-        return this.currenthealthpoints;
74
+        return this.currenthealthpoints.clone();
75 75
     }
76 76
 
77 77
     public void setCurrentHealthPoints(int hp)
78 78
     {
79
-        this.currenthealthpoints = hp;
79
+        this.currenthealthpoints = hp.clone();
80 80
     }
81 81
 
82
-    public int getXp()
82
+    public Natural getXp()
83 83
     {
84 84
         return this.xp;
85 85
     }
86 86
 
87
-    public void setXp(int xp)
87
+    public void setXp(Natural xp)
88 88
     {
89 89
         this.xp = xp;
90 90
     }
... ...
@@ -93,18 +93,18 @@ public class Player {
93 93
         return AvatarClass;
94 94
     }
95 95
 
96
-    public void addXp(int xp) {
97
-        int ancientLevel = this.level;
98
-        this.xp += xp;
96
+    public void addXp(Natural xp) {
97
+        Natural ancientLevel = this.level;
98
+        this.xp.add(xp);
99 99
         int i = 0;
100 100
         while(i < xpForlevel.length && this.xp >= xpForlevel[i])
101 101
         {
102 102
             i++;
103 103
         }
104 104
 
105
-        this.level = i;
105
+        this.level = Natural.valueof(i);
106 106
 
107
-        if (this.level != ancientLevel) {
107
+        if (this.level.compareTo(ancientLevel) == -1) {
108 108
             // Player leveled-up!
109 109
             // Give a random object
110 110
             ;
... ...
@@ -119,14 +119,16 @@ public class Player {
119 119
         }
120 120
     }
121 121
 
122
-    public void removeMoney(int amount) throws IllegalArgumentException {
122
+    public void removeMoney(Natural amount)
123
+    {
123 124
         if (money - amount < 0) {
124 125
             throw new IllegalArgumentException("Player can't have a negative money!");
125 126
         }
126 127
 
127
-        money = Integer.parseInt(money.toString()) - amount;
128
+        money.remove(amount);
128 129
     }
129
-    public void addMoney(int amount) { 
130
-        money = money + amount;
130
+
131
+    public void addMoney(Natural amount) { 
132
+        money.add(amount);
131 133
     }
132 134
 }
133 135
\ No newline at end of file
... ...
@@ -2,6 +2,7 @@ package re.forestier.edu.rpg;
2 2
 
3 3
 import java.util.HashMap;
4 4
 import java.util.Random;
5
+import re.forestier.edu.lib.Natural;
5 6
 
6 7
 public class UpdatePlayer {
7 8
 
... ...
@@ -100,31 +101,31 @@ public class UpdatePlayer {
100 101
 
101 102
     // majFinDeTour met à jour les points de vie
102 103
     public static void majFinDeTour(Player player) {
103
-        if(player.getCurrentHealthPoints() == 0) {
104
+        if(player.getCurrentHealthPoints().equals(Natural.ZERO)) {
104 105
             System.out.println("Le joueur est KO !");
105 106
             return;
106 107
         }
107 108
 
108
-        if(player.getCurrentHealthPoints() < player.getHealthPoints()/2) {
109
+        if(player.getCurrentHealthPoints().compareTo(Natural.divide(player.getHealthPoints(),2)) == 1 ) {
109 110
             if(!player.getAvatarClass().equals("ADVENTURER")) {
110 111
                 if(player.getAvatarClass().equals("DWARF")) {
111 112
                     if(player.inventory.contains("Holy Elixir")) {
112
-                        player.setCurrentHealthPoints(player.getCurrentHealthPoints()+1);
113
+                        player.setCurrentHealthPoints(Natural.add(player.getCurrentHealthPoints(),1));
113 114
                     }
114
-                    player.setCurrentHealthPoints(player.getCurrentHealthPoints()+1);
115
+                    player.setCurrentHealthPoints(Natural.add(player.getCurrentHealthPoints(),1));
115 116
                 }
116 117
 
117 118
 
118 119
                 if(player.getAvatarClass().equals("ARCHER")) {
119
-                    player.setCurrentHealthPoints(player.getCurrentHealthPoints()+1);
120
+                    player.setCurrentHealthPoints(Natural.add(player.getCurrentHealthPoints(),1));
120 121
                     if(player.inventory.contains("Magic Bow")) {
121
-                        player.setCurrentHealthPoints(player.getCurrentHealthPoints()+player.getCurrentHealthPoints()/8-1);
122
+                        player.setCurrentHealthPoints(Natural.add(player.getCurrentHealthPoints(), Natural.substract(Natural.divide(player.getCurrentHealthPoints(),8),1),player.getCurrentHealthPoints))
122 123
                     }
123 124
                 }
124 125
             } else {
125 126
                 player.setCurrentHealthPoints(player.getCurrentHealthPoints()+2);
126
-                if(player.getLevel() < 3) {
127
-                    player.setCurrentHealthPoints(player.getCurrentHealthPoints()-1);
127
+                if(player.getLevel().compareTo(Natural.valueOf(3)) == 1) {
128
+                    player.setCurrentHealthPoints(player.getCurrentHealthPoints().substract(1));
128 129
                 }
129 130
             }
130 131
         } else 
... ...
@@ -229,6 +229,9 @@ public class UnitTests {
229 229
         assertEquals(n.substract(Natural.valueOf(2)),Natural.valueOf(0));
230 230
         assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(0).substract(Natural.valueOf(4)));
231 231
 
232
+        assertEquals(Natural.valueOf(4).divide(Natural.valueOf(2)),Natural.valueOf(2));
233
+        assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(0).divide(null));
234
+
232 235
         assertEquals(n.toString(),"2");
233 236
         assertTrue(n.equals(Natural.valueOf(2)));
234 237
         assertTrue(n.equals(n));
235 238