Skip to content

Commit 59c1bce

Browse files
committed
ex5 concluido, gitignore adicionado
0 parents  commit 59c1bce

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.class

Jogo.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Random;
2+
3+
public class Jogo {
4+
public static void main(String[] args) throws Exception {
5+
Personagem cacador = new Personagem(10, 0, 0);
6+
Personagem guloso = new Personagem(2, 10, 0);
7+
8+
cacador.setNome("Caçador");
9+
guloso.setNome("Guloso");
10+
Random gerador = new Random();
11+
12+
while (true) {
13+
int oQueFazer = gerador.nextInt(6) + 1;
14+
15+
switch (oQueFazer) {
16+
case 1:
17+
cacador.cacar();
18+
cacador.estado();
19+
break;
20+
case 2:
21+
guloso.cacar();
22+
guloso.estado();
23+
break;
24+
case 3:
25+
cacador.comer();
26+
cacador.estado();
27+
break;
28+
case 4:
29+
guloso.comer();
30+
guloso.estado();
31+
break;
32+
case 5:
33+
cacador.dormir();
34+
cacador.estado();
35+
break;
36+
case 6:
37+
guloso.dormir();
38+
guloso.estado();
39+
break;
40+
}
41+
System.out.println("\n-------------------------------------------------\n");
42+
Thread.sleep(2000);
43+
}
44+
}
45+
}

Personagem.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
public class Personagem {
2+
3+
private String nome;
4+
private int energia = 10;
5+
private int fome = 0;
6+
private int sono = 0;
7+
final static int LIMITE_SUPERIOR = 10;
8+
9+
public Personagem(int energia, int fome, int sono) {
10+
if (energia >= 0 && energia <= 10)
11+
this.energia = energia;
12+
if (fome >= 0 && fome <= 10)
13+
this.fome = fome;
14+
if (sono >= 0 && sono <= 10)
15+
this.sono = sono;
16+
}
17+
18+
public Personagem(String nome, int energia, int fome, int sono) {
19+
this(energia, fome, sono);
20+
this.nome = nome;
21+
}
22+
23+
public void setNome(String nome) {
24+
this.nome = nome;
25+
}
26+
27+
public String getNome() {
28+
return this.nome;
29+
}
30+
31+
public int getEnergia() {
32+
return this.energia;
33+
}
34+
35+
public int getFome() {
36+
return this.fome;
37+
}
38+
39+
public int getSono() {
40+
return this.sono;
41+
}
42+
43+
void cacar() {
44+
if (this.energia >= 2) {
45+
System.out.printf("%s caçando...\n", nome);
46+
this.energia = this.energia - 2;
47+
} else {
48+
System.out.printf("%s sem energia para caçar...\n", nome);
49+
}
50+
this.fome = Math.min(this.fome + 1, LIMITE_SUPERIOR);
51+
this.sono = Math.min(this.sono + 1, LIMITE_SUPERIOR);
52+
}
53+
54+
void comer() {
55+
if (this.fome >= 1) {
56+
System.out.printf("%s comendo...\n", nome);
57+
this.fome -= 1;
58+
this.energia = this.energia + 1 <= LIMITE_SUPERIOR ? this.energia + 1 : this.energia;
59+
} else {
60+
System.out.printf("%s sem fome...\n", nome);
61+
}
62+
}
63+
64+
void dormir() {
65+
if (this.sono >= 1) {
66+
System.out.printf("%s dormindo...\n", nome);
67+
this.sono -= 1;
68+
this.energia = this.energia + 1 <= LIMITE_SUPERIOR ? this.energia + 1 : this.energia;
69+
} else {
70+
System.out.printf("%s sem sono...\n", nome);
71+
}
72+
}
73+
74+
void estado() {
75+
System.out.println("\n-------------------------------------------------");
76+
System.out.printf("ESTADO:\nPersonagem: %s\nEnergia: %d\nFome: %d\nSono: %d", this.getNome(), this.getEnergia(), this.getFome(), this.getSono());
77+
}
78+
}

0 commit comments

Comments
 (0)