-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTemplate.java
More file actions
90 lines (73 loc) · 2.36 KB
/
Template.java
File metadata and controls
90 lines (73 loc) · 2.36 KB
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
class Creature {
public int attack, health;
public Creature(int attack, int health) {
this.attack = attack;
this.health = health;
}
@Override
public String toString() {
return String.format("(%d/%d)", attack, health);
}
}
abstract class CardGame {
public Creature[] creatures;
public CardGame(Creature[] creatures) {
this.creatures = creatures;
}
// returns -1 if no clear winner (both alive or both dead)
public int combat(int creature1, int creature2) {
Creature first = creatures[creature1];
Creature second = creatures[creature2];
hit(first, second);
hit(second, first);
if (first.health > 0 && second.health <= 0) {
return 0;
}
if (first.health <= 0 && second.health > 0) {
return 1;
}
return -1;
}
// attacker hits other creature
protected abstract void hit(Creature attacker, Creature other);
}
class TemporaryCardDamageGame extends CardGame {
public TemporaryCardDamageGame(Creature... creatures) {
super(creatures);
}
protected void hit(Creature attacker, Creature other) {
if (attacker.attack >= other.health) {
other.health -= attacker.attack;
}
}
}
class PermanentCardDamageGame extends CardGame {
public PermanentCardDamageGame(Creature... creatures) {
super(creatures);
}
protected void hit(Creature attacker, Creature other) {
other.health -= attacker.attack;
}
}
class DemoTemplate {
public static void main(String[] args) {
Creature p1 = new Creature(10, 5);
Creature p2 = new Creature(2, 50);
CardGame permDamage = new PermanentCardDamageGame(p1, p2);
playGame(permDamage);
Creature p3 = new Creature(10, 5);
Creature p4 = new Creature(2, 50);
CardGame tempDamage = new TemporaryCardDamageGame(p3, p4);
playGame(tempDamage);
}
private static void playGame(CardGame game) {
int winner = -1;
for (int i = 0; i < 100; i++) {
winner = game.combat(0, 1);
if (winner != -1) {
break;
}
}
System.out.println(game.getClass().getSimpleName() + " winner = " + winner);
}
}