-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.java
More file actions
89 lines (58 loc) · 2.48 KB
/
Game.java
File metadata and controls
89 lines (58 loc) · 2.48 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
package factory;
import java.util.Random;
import java.util.Scanner;
/**
* Dummy Example of Factory Pattern :)
* Replace the Mario Class with Factory pattern
* @author Khalid Elshafie <abolkog@gmail.com>
* @created 08/03/2018.
*/
public class Game {
static Random random = new Random();
public static void main(String[] args) {
Mario luigi = new Mario("luigi");
Scanner scanner = new Scanner(System.in);
System.out.printf("Game On.%nYou are playing using[%s]. Jump Attach is [%d] and Mushroom is [%d]%n", luigi.getName(), luigi.getJumpAttack(), luigi.getMushroomAttack());
int counter = 0;
while (luigi.getHealth() > 0) {
int id = EnemyFactory.BIRD;
int random = getRandom(0, 20);
if (random >= 0 && random < 5) id = EnemyFactory.BIRD;
if (random >= 5 && random < 15) id = EnemyFactory.TURTLE;
if (random >= 15 && random < 20) id = EnemyFactory.DINOSAUR;
Enemy enemy = EnemyFactory.createEnemy(id);
enemy.showUp();
System.out.println("\n");
play(luigi, enemy, scanner);
counter++;
if (counter >= 4) break;
}
scanner.close();
System.out.println("You Won!!!! Tat Tart Tart Ta");
}
public static void play(Mario hero, Enemy enemy, Scanner scanner) {
while (enemy.getHealth() > 0 || hero.getHealth() > 0) {
System.out.print("What do you want to do? [1=Jump Attack/2=Mushroom Attack]");
int answer = scanner.nextInt();
int heroAttack = 0;
if (answer == 1) heroAttack = hero.getJumpAttack();
if (answer == 2) heroAttack = hero.getMushroomAttack();
enemy.takeDamage(heroAttack);
if (enemy.getHealth() <= 0) {
System.out.printf("%s died%n", enemy.getName());
break;
}
System.out.printf("%s is attaching ...%n", enemy.getName());
hero.setHealth(hero.getHealth() - enemy.attack());
System.out.printf("%s's Health is [%d]%n%n", hero.getName(), hero.getHealth());
if (hero.getHealth() <= 0) {
System.out.printf("%s is Dead. Health [%d]%n", hero.getName(), hero.getHealth());
System.out.println("Game Over");
System.exit(0);
}
}
}
public static int getRandom(int min, int max) {
return random.nextInt((max - min) + 1) + min;
}
}