Skip to content

Commit 0f43888

Browse files
authored
Merge pull request mouredev#5676 from armenta-angel/main
mouredev#32 - Java
2 parents ebaae56 + d9915c9 commit 0f43888

File tree

2 files changed

+468
-0
lines changed

2 files changed

+468
-0
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
import static java.lang.Thread.sleep;
5+
6+
public class ArmentaAngel {
7+
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in); // Create a Scanner object
10+
11+
System.out.println("Enter Deadpool's life points:");
12+
Fighter deadpool = new Fighter(Hero.DEADPOOL, scanner.nextInt()) ;
13+
14+
System.out.println("Enter Wolverine's life points:");
15+
Fighter wolverine = new Fighter(Hero.WOLVERINE, scanner.nextInt()) ;
16+
17+
Game game = new Game(deadpool, wolverine);
18+
game.start();
19+
}
20+
}
21+
22+
enum Hero {
23+
24+
DEADPOOL("Deadpool", 10, 100, 25),
25+
WOLVERINE("Wolverine", 10, 120, 20);
26+
27+
final String name;
28+
final int minDamage;
29+
final int maxDamage;
30+
final int defendAttackPercentage;
31+
32+
Hero(String name, int minDamage, int maxDamage, int defendAttackPercentage) {
33+
this.name = name;
34+
this.minDamage = minDamage;
35+
this.maxDamage = maxDamage;
36+
this.defendAttackPercentage = defendAttackPercentage;
37+
}
38+
}
39+
40+
class Fighter {
41+
42+
public Hero hero;
43+
public int life;
44+
public String[] randomDefense = new String[100];
45+
46+
private final String SUPER_DEFENSE = "SUPER_DEFENSE";
47+
private final String USED = "USED";
48+
private final String NOT_USED = "NOT_USED";
49+
50+
private final Random rand = new Random();
51+
52+
public Fighter(Hero hero, int life) {
53+
this.hero = hero;
54+
this.life = life;
55+
56+
buildRandomDefense();
57+
}
58+
59+
public int attack() {
60+
return randomBetween(hero.minDamage, hero.maxDamage);
61+
}
62+
63+
public boolean defense() {
64+
int index;
65+
Boolean noDamage;
66+
67+
do {
68+
index = rand.nextInt(100);
69+
if (randomDefense[index].equals(SUPER_DEFENSE)) {
70+
noDamage = true;
71+
} else if (randomDefense[index].equals(NOT_USED)) {
72+
noDamage = false;
73+
} else {
74+
// Only can be USED
75+
noDamage = null;
76+
}
77+
} while (noDamage == null);
78+
randomDefense[index] = USED;
79+
80+
return noDamage;
81+
}
82+
83+
protected int randomBetween(int minInclusive, int maxInclusive) {
84+
return rand.nextInt(maxInclusive - minInclusive + 1) + minInclusive;
85+
}
86+
87+
private void buildRandomDefense() {
88+
// Place SUPER_DEFENSE positions
89+
int avoidAttackCount = hero.defendAttackPercentage;
90+
while (avoidAttackCount > 0) {
91+
int index = rand.nextInt(100);
92+
if (randomDefense[index] == null) {
93+
randomDefense[index] = SUPER_DEFENSE;
94+
avoidAttackCount--;
95+
}
96+
}
97+
98+
fillNullIndexes(randomDefense, NOT_USED);
99+
}
100+
101+
private void fillNullIndexes(String[] array, String filler) {
102+
for (int i = 0; i < array.length; i++) {
103+
if(array[i] == null) {
104+
array[i] = filler;
105+
}
106+
}
107+
}
108+
}
109+
110+
class Game {
111+
private final Fighter[] fighters = new Fighter[2];
112+
private int turnCount;
113+
protected int turnOf;
114+
private boolean fightEnds = false;
115+
116+
private final int TURN_TIME_IN_SECONDS = 1;
117+
118+
public Game(Fighter fighter1, Fighter fighter2) {
119+
fighters[0] = fighter1;
120+
fighters[1] = fighter2;
121+
}
122+
123+
public void start() {
124+
turnCount = 1;
125+
turnOf = chooseFirstFighterRandomly();
126+
127+
while (!fightEnds) {
128+
playTurn();
129+
}
130+
131+
fightBroadcastFightEnds();
132+
}
133+
134+
protected int chooseFirstFighterRandomly() {
135+
Random rand = new Random();
136+
return rand.nextInt(2); // Only can return 0 or 1
137+
}
138+
139+
protected void changeTurn() {
140+
turnOf = turnOf == 1 ? 0 : 1;
141+
}
142+
143+
protected int fighterOnDefenseIndex() {
144+
return turnOf == 1 ? 0 : 1;
145+
}
146+
147+
protected boolean fightEnds() {
148+
return fighters[0].life <= 0 || fighters[1].life <= 0;
149+
}
150+
151+
protected int getWinnerIndex() {
152+
return fighters[0].life > fighters[1].life ? 0 : 1;
153+
}
154+
155+
private void playTurn() {
156+
Fighter fighterOnAttack = fighters[turnOf];
157+
Fighter fighterOnDefense = fighters[fighterOnDefenseIndex()];
158+
159+
fightBroadcastTurnBegins();
160+
161+
dramaticPause();
162+
163+
int attackPower = fighterOnAttack.attack();
164+
boolean defended = fighterOnDefense.defense();
165+
boolean maximumPower = attackPower == fighterOnAttack.hero.maxDamage;
166+
167+
fightBroadcastTurnEnds(attackPower, maximumPower, defended);
168+
169+
if (!defended) {
170+
fighterOnDefense.life -= attackPower;
171+
}
172+
173+
if (!fightEnds()) {
174+
if (!maximumPower) {
175+
changeTurn();
176+
}
177+
turnCount++;
178+
} else {
179+
fightEnds = true;
180+
}
181+
}
182+
183+
private void dramaticPause() {
184+
try {
185+
sleep(TURN_TIME_IN_SECONDS * 1000);
186+
} catch (Exception e) {
187+
throw new RuntimeException(e);
188+
}
189+
}
190+
191+
private void fightBroadcastTurnBegins() {
192+
System.out.printf("# This is turn %d ##############################################################%n", turnCount);
193+
System.out.printf("# %s life is %d%n", fighters[0].hero.name, fighters[0].life);
194+
System.out.printf("# %s life is %d%n", fighters[1].hero.name, fighters[1].life);
195+
System.out.printf("# %s's turn to attack, %s's turn to defend%n", fighters[turnOf].hero.name, fighters[fighterOnDefenseIndex()].hero.name);
196+
System.out.printf("%n");
197+
}
198+
199+
private void fightBroadcastTurnEnds(int power, boolean maximumPower, boolean defended) {
200+
// Turn results
201+
System.out.printf("@ After turn %d%n", turnCount);
202+
if(maximumPower) {
203+
System.out.printf("@ %s attack is %d. It is a $$$ MAXIMUM POWER ATTACK $$$%n", fighters[turnOf].hero.name, power);
204+
System.out.printf("@ With this MAXIMUM POWER ATTACK, %s will strike again in the next turn%n", fighters[turnOf].hero.name);
205+
} else {
206+
System.out.printf("@ %s attack power is %d%n", fighters[turnOf].hero.name, power);
207+
}
208+
if(defended) {
209+
System.out.printf("@ %s dodges the attack with a --- SUPER DEFENSE ---%n", fighters[fighterOnDefenseIndex()].hero.name);
210+
} else {
211+
System.out.printf("@ %s receives an accurate hit and loses %d life points%n", fighters[fighterOnDefenseIndex()].hero.name, power);
212+
}
213+
System.out.printf("%n");
214+
}
215+
216+
private void fightBroadcastFightEnds() {
217+
// Fight results
218+
System.out.printf("################################################################################%n");
219+
System.out.printf("# #%n");
220+
System.out.printf("# %s is the WINNER after %d turns%n", fighters[getWinnerIndex()].hero.name, turnCount);
221+
System.out.printf("# #%n");
222+
System.out.printf("# %s loses with %d life points%n", fighters[fighterOnDefenseIndex()].hero.name, fighters[fighterOnDefenseIndex()].life);
223+
System.out.printf("# %s wins with %d life points%n", fighters[turnOf].hero.name, fighters[turnOf].life);
224+
System.out.printf("# #%n");
225+
System.out.printf("################################################################################%n");
226+
}
227+
}

0 commit comments

Comments
 (0)