-
Notifications
You must be signed in to change notification settings - Fork 2
/
Fight.java
159 lines (142 loc) · 4.5 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Fight.java
* This class represents a turn-based fight.
* @author Emma Shumadine (primary), Lily Orth-Smith, Rachel Zhang
* */
import java.util.Vector;
import javafoundations.LinkedQueue;
public class Fight {
private LinkedQueue<GameCharacter> turns = new LinkedQueue<GameCharacter>();
private Vector<GameCharacter> enemies;
private Player player;
/**
* Creates a Fight object with the specified player and enemies. Automatically enqueues all turns.
* @param p the player
* @param e the enemies
* */
public Fight(Player p, Vector<GameCharacter> e) {
player = p;
enemies = e;
turns.enqueue(player);
for(int i = 0; i < enemies.size(); i++) {
turns.enqueue(enemies.get(i));
}
}
/**
* Plays one turn of the fight
* @param action the action for the character to take. Can be "attack" or "heal".
* @param targetIndex the index of the target in its respective data structure. -1 if target is the player.
* @return the damage/healing done (-1 if the attack missed).
* */
public int playOneRound(String action, int targetIndex) {
GameCharacter currentTurn = turns.dequeue();
GameCharacter target;
int result = 0;
if(currentTurn.getCurrentHP() != 0) {
if(targetIndex == -1 || enemies.contains(currentTurn)) {
target = player;
} else {
target = enemies.get(targetIndex);
}
if(enemies.contains(currentTurn) || action.equals("attack")) { //attacking or enemy turn
System.out.println("Attacking " + target.getName() + "...");
result = currentTurn.attack(target);
if(result != -1) {
System.out.println(result + " damage!");
} else {
System.out.println("Miss!");
}
} else { //player healing
System.out.println(target.getName());
Item toUse = player.getItem(targetIndex);
result = toUse.getHp();
player.useHealthItem(toUse);
}
turns.enqueue(currentTurn);
}
return result;
}
/**
* Runs the fight
* */
public void fight() {
while(player.getCurrentHP() != 0 && turns.size() != 1) {
System.out.println("Player: " + player.getCurrentHP());
String e = "Enemies: [";
for(int i = 0; i < enemies.size(); i++) {
GameCharacter en = enemies.get(i);
if(enemies.indexOf(en) != enemies.size() - 1) {
e += en.getName() + ": " + en.getCurrentHP() + ", ";
} else {
e += en.getName() + ": " + en.getCurrentHP() + "]";
}
}
System.out.println(e);
playOneRound("attack", 0);
}
if(player.getCurrentHP() == 0) {
System.out.println("Player: " + player.getCurrentHP());
String e = "Enemies: [";
for(int i = 0; i < enemies.size(); i++) {
GameCharacter en = enemies.get(i);
if(enemies.indexOf(en) != enemies.size() - 1) {
e += en.getName() + ": " + en.getCurrentHP() + ", ";
} else {
e += en.getName() + ": " + en.getCurrentHP() + "]";
}
}
System.out.println(e);
System.out.println("Game over");
} else {
System.out.println(player.getName() + " won!");
}
}
/**
* Respawns all enemies and adds all turns to the queue
* */
public void respawn() {
while(!turns.isEmpty()) {
turns.dequeue();
}
turns.enqueue(player);
for(int i = 0; i < enemies.size(); i++) {
GameCharacter current = enemies.get(i);
current.setHP(current.getMaxHP());
turns.enqueue(current);
}
}
/**
* Returns a vector containing all the enemies in the fight
* @return the enemies in the fight
*/
public Vector<GameCharacter> getEnemies() {
return enemies;
}
/**
* Determines if it is the player's turn currently
* @return true if it is the player's turn
*/
public boolean isPlayersTurn() {
if (turns.first().getName().equals("Player"))
return true;
else
return false;
}
/**
* Determines whether the fight has ended
* @return true if the player has died or all of the enemies have been defeated
*/
public boolean fightEnded() {
if(player.getCurrentHP() == 0 || turns.size() == 1)
return true;
else
return false;
}
public static void main(String[] args) {
Vector<GameCharacter> enemies = new Vector<GameCharacter>();
enemies.add(new GameCharacter(100, new Weapon("Sickle", "None", 0.65, "5d40"), "Sample"));
Player player = new Player();
Fight fight = new Fight(player, enemies);
fight.fight();
}
}