Skip to content
Snippets Groups Projects
Commit c8a70585 authored by Camille Okubo's avatar Camille Okubo
Browse files

algo main défense

parent 55e87c9a
Branches
No related tags found
No related merge requests found
package bitFight;
public interface Action {
public ActionType getActionType();
public String getAction();
public String getName();
}
package bitFight;
public enum ActionType {
DEFENSE,
ATTACK;
}
......@@ -43,4 +43,8 @@ public enum Attack implements Action {
public static boolean attackInTime(LocalDateTime timeBeforeAttack, int attackTime) {
return (Duration.between(timeBeforeAttack, LocalDateTime.now()).toSeconds() > attackTime);
}
public ActionType getActionType(){
return ActionType.ATTACK;
}
}
\ No newline at end of file
......@@ -24,4 +24,8 @@ public enum Defense implements Action {
public String getAction() {
return "Defense";
}
public ActionType getActionType(){
return ActionType.DEFENSE;
}
}
......@@ -175,7 +175,7 @@ public class Level{
if(defense.getHeight() == attack.getHeight()){
character.currentEnergy += 60;
if(character.currentEnergy > character.maxEnergy) character.currentEnergy = character.maxEnergy;
System.out.println("The attack is blocked! 60 energy is gained");
System.out.println("The " + attack.getName() + " is blocked! 60 energy is gained");
} else {
character.currentEnergy += 30;
character.currentHealth -= attack.getDamage();
......
......@@ -52,10 +52,13 @@ class Main {
Display.goToUserInput();
// Récupère le nom de cahque attaque dans une ArrayList
ArrayList<Action> actionList = new ArrayList<>();
for (Attack a : Attack.values()) actionList.add(a);
for (Defense d : Defense.values()) actionList.add(d);
// Récupère le nom de chaque action dans une ArrayList
ArrayList<String> actionNames = new ArrayList<>();
for (Attack a : Attack.values()) actionNames.add(a.getName());
for (Defense d : Defense.values()) actionNames.add(d.getName());
for (Action a : actionList) actionNames.add(a.getName());
BufferedReader in= new BufferedReader( new InputStreamReader(System.in));
......@@ -83,6 +86,7 @@ class Main {
// Boucle tant que le joueur n'a pas taper de nom d'une attaque valide ou tape trop tard ou tape q
while ((!actionNames.contains(input.getInput()) || !Attack.attackInTime(timeBeforeAttack, attackTime)) && !input.getInput().equals("q")) {
//Si le joueur est trop lent, l'ennemi fait une attaque aléatoire
if(!Attack.attackInTime(timeBeforeAttack, attackTime)){
Display.clearDialogBox();
......@@ -101,8 +105,18 @@ class Main {
if(!player.isExhausted() && !input.getInput().equals("q") ){
Attack attack = Attack.valueOf(actionNames.get(actionNames.indexOf(input.getInput())).toUpperCase());
level.playerHasEnergy(attack);
ActionType type = null;
for(Action a : actionList) if(a.getName().equals(input.getInput())) type = a.getActionType();
if(type == ActionType.ATTACK) {
Attack attack = (Attack) actionList.get(actionList.indexOf(input.getInput()));
level.playerHasEnergy(attack);
} else
if(type == ActionType.DEFENSE) {
Defense defense = (Defense) actionList.get(actionList.indexOf(input.getInput()));
Attack randomEnemyAttack = Attack.values()[(int) rand.nextDouble()*Attack.values().length];
level.defends(player, defense, randomEnemyAttack);
}
} else {} //TODO ajouter display trop fatigué
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment