-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFight.java
90 lines (73 loc) · 3.13 KB
/
Fight.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Random;
/**
* Manages the interactions between the player and a monster
* by initializing turn-based combat with RNG mechanics.
*/
public class Fight
{
private static Random random = new Random();
public static String fight(Player currentPlayer, Room currentRoom) {
Monster currentMonster = currentRoom.getMonster();
System.out.println(currentMonster.getName().toUpperCase() + " IS COMING!!!!");
currentMonster.printMonster();
currentPlayer.checkStatus();
currentMonster.checkStatus();
while (currentMonster.getCurrentHealth() > 0) {
String toFlee = UI.promptLine("Do you want to FIGHT [F] or RUN [R]?").trim().toUpperCase();
while (!toFlee.equals("F") && !toFlee.equals("R")) {
System.out.println("Not a valid command. Enter F for fight or R for run.");
toFlee = UI.promptLine("Do you want to FIGHT [F] or RUN [R]?").trim().toUpperCase();
System.out.println("-----------------------");
}
if (toFlee.equals("R")) {
if (random.nextBoolean()) {
System.out.println("You successfully fled from the monster, but the monster's health regenerated. You're back in the hallway.");
return "fled";
} else {
System.out.println("You slipped and fell, and the monster got a second attack!");
if (damageTaken(currentPlayer, currentMonster)) {
return "dead";
}
if (currentMonster.getCurrentHealth() <= 0){
System.out.println("------------------fight over-------------------------");
System.out.println(currentMonster.getName() + " was defeated! Good job, " + currentPlayer.getName() + ".");
System.out.println("-----------------------------------------------------");
return "won";
}
}
}
if (toFlee.equals("F")) {
if (damageTaken(currentPlayer, currentMonster)) {
return "dead";
}
}
if (currentPlayer.getHealth() < 5) {
System.out.println("Your health is really low.");
}
}
System.out.println("------------------fight over-------------------------");
System.out.println(currentMonster.getName() + " was defeated! Good job, " + currentPlayer.getName() + ".");
System.out.println("-----------------------------------------------------");
currentRoom.killMonster();
if (!currentRoom.getName().equals("exit")){
currentPlayer.checkStatus();
}
return "won";
}
private static boolean damageTaken(Player currentPlayer, Monster currentMonster){
int damageGiven = random.nextInt(currentPlayer.getCurrentWeapon().getAttribute());
int damageSustained = random.nextInt(currentMonster.getCurrentStrength());
currentMonster.setCurrentHealth(currentMonster.getCurrentHealth() - damageGiven);
currentPlayer.incrementHealth(-damageSustained);
if (currentPlayer.getHealth() <= 0)
{
System.out.println("You're dead!");
return true;
}
System.out.println("-----------------------");
System.out.println("You took " + damageSustained + " damage and the monster took " + damageGiven + " damage.");
System.out.println("Your health is now " + currentPlayer.getHealth() + " and the monster's health is now " + currentMonster.getCurrentHealth());
System.out.println("-----------------------");
return false;
}
}