Skip to content

Commit d61f389

Browse files
Incluindo exercício jogo nos Repositórios.
0 parents  commit d61f389

10 files changed

+253
-0
lines changed

Diff for: .classpath

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
5+
<attributes>
6+
<attribute name="module" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

Diff for: .project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Game</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

Diff for: .settings/org.eclipse.core.resources.prefs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8

Diff for: .settings/org.eclipse.jdt.core.prefs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17

Diff for: bin/model/Calcular.class

2.76 KB
Binary file not shown.

Diff for: bin/view/Game.class

2.24 KB
Binary file not shown.

Diff for: bin/view/test.class

593 Bytes
Binary file not shown.

Diff for: src/model/Calcular.java

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package model;
2+
3+
import java.util.Random;
4+
5+
public class Calcular {
6+
private int dificuldade;
7+
private int valor1;
8+
private int valor2;
9+
private int operacao;
10+
private int resultado;
11+
12+
13+
public Calcular(int dificuldade) {
14+
15+
Random rand = new Random();
16+
17+
this.operacao = rand.nextInt(3); // 0 - somar, 1 - diminuir, 2 - multiplicar
18+
this.dificuldade = dificuldade;
19+
20+
if(dificuldade == 1) {
21+
//fácil
22+
this.valor1 = rand.nextInt(10); // 0 a 9
23+
this.valor2 = rand.nextInt(10); // 0 a 9
24+
} else if (dificuldade == 2) {
25+
//médio
26+
this.valor1 = rand.nextInt(100); // 0 a 99
27+
this.valor2 = rand.nextInt(100); // 0 a 99
28+
} else if (dificuldade == 3) {
29+
// difícil
30+
this.valor1 = rand.nextInt(100); // 0 a 999
31+
this.valor2 = rand.nextInt(100); // 0 a 999
32+
} else if (dificuldade == 4) {
33+
// Insano
34+
this.valor1 = rand.nextInt(1000); // 0 a 9999
35+
this.valor2 = rand.nextInt(1000); // 0 a 9999
36+
} else {
37+
// Ultra
38+
this.valor1 = rand.nextInt(10000); // 0 a 99999
39+
this.valor2 = rand.nextInt(10000); // 0 a 99999
40+
}
41+
}
42+
43+
public int getDificuldade() {
44+
return dificuldade;
45+
}
46+
public int getValor1() {
47+
return valor1;
48+
}
49+
public int getValor2() {
50+
return valor2;
51+
}
52+
public int getOperacao() {
53+
return operacao;
54+
}
55+
public int getResultado() {
56+
return resultado;
57+
}
58+
59+
public String toString() {
60+
String op;
61+
if(this.getOperacao() == 0) {
62+
op = "Somar";
63+
}else if(this.getOperacao() == 1) {
64+
op = "Diminuir";
65+
}else if(this.getOperacao() == 2) {
66+
op = "Multiplicar";
67+
}else{
68+
op = "Operação desconhecida";
69+
}
70+
return "Valor 1: " + this.getValor1() +
71+
"\nValor 2: " + this.getValor2() +
72+
"\nDificuldade: " +this.getDificuldade() +
73+
"\nOperação: " + op;
74+
}
75+
76+
public boolean somar(int resposta) {
77+
this.resultado = this.getValor1() + this.getValor2();
78+
boolean certo = false;
79+
80+
if(resposta == this.getResultado()) {
81+
System.out.println("Resposta correta");
82+
certo = true;
83+
} else {
84+
System.out.println("Resposta errada");
85+
}
86+
System.out.println(this.getValor1() + " + " + this.getValor2() + " = " + this.getResultado());
87+
return certo;
88+
}
89+
90+
public boolean diminuir(int resposta) {
91+
this.resultado = this.getValor1() - this.getValor2();
92+
boolean certo = false;
93+
94+
if(resposta == this.getResultado()) {
95+
System.out.println("Resposta correta");
96+
certo = true;
97+
} else {
98+
System.out.println("Resposta errada");
99+
}
100+
System.out.println(this.getValor1() + " - " + this.getValor2() + " = " + this.getResultado());
101+
return certo;
102+
}
103+
104+
public boolean multiplicar(int resposta) {
105+
this.resultado = this.getValor1() * this.getValor2();
106+
boolean certo = false;
107+
108+
if(resposta == this.getResultado()) {
109+
System.out.println("Resposta correta");
110+
certo = true;
111+
} else {
112+
System.out.println("Resposta errada");
113+
}
114+
System.out.println(this.getValor1() + " * " + this.getValor2() + " = " + this.getResultado());
115+
return certo;
116+
}
117+
}

Diff for: src/view/Game.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
*
3+
*/
4+
/**
5+
* @author rafael
6+
*
7+
*/
8+
package view;
9+
10+
import java.util.Scanner;
11+
12+
import model.Calcular;
13+
14+
public class Game {
15+
16+
static Scanner teclado = new Scanner(System.in);
17+
static int pontos = 0;
18+
static Calcular calc;
19+
20+
public static void main(String[] args) {
21+
Game.jogar();
22+
23+
}
24+
25+
public static void jogar() {
26+
System.out.println("Informe o níve de Dificuldade: [1, 2, 3, 4]");
27+
int dificuldade = Game.teclado.nextInt();
28+
29+
Game.calc = new Calcular(dificuldade);
30+
31+
// Somar
32+
if (calc.getOperacao() == 0) {
33+
System.out.println(calc.getValor1() + " + " + calc.getValor2());
34+
int resposta = Game.teclado.nextInt();
35+
36+
if (calc.somar(resposta)) {
37+
// Ganha 1 ponto
38+
Game.pontos += 1;
39+
System.out.println("Você tem " + Game.pontos + " ponto(s).");
40+
}
41+
}
42+
// Diminuir
43+
else if (calc.getOperacao() == 1) {
44+
System.out.println(calc.getValor1() + " - " + calc.getValor2());
45+
int resposta = Game.teclado.nextInt();
46+
47+
if (calc.diminuir(resposta)) {
48+
// Ganha 1 ponto
49+
Game.pontos += 1;
50+
System.out.println("Você tem " + Game.pontos + " ponto(s).");
51+
}
52+
}
53+
// Multiplicar
54+
else if (calc.getOperacao() == 2) {
55+
System.out.println(calc.getValor1() + " * " + calc.getValor2());
56+
int resposta = Game.teclado.nextInt();
57+
58+
if (calc.multiplicar(resposta)) {
59+
// Ganha 1 ponto
60+
Game.pontos += 1;
61+
System.out.println("Você tem " + Game.pontos + " ponto(s).");
62+
} else {
63+
System.out.println("A operação " + calc.getOperacao() + "não é conhecida.");
64+
}
65+
}
66+
67+
System.out.println("Deseja continuar? [1 - Sim, 0 - Não]: ");
68+
int continuar = Game.teclado.nextInt();
69+
70+
if (continuar == 1) {
71+
Game.jogar();
72+
73+
} else {
74+
System.out.println("Você fez " + Game.pontos + " ponto(s).");
75+
System.out.println("Até a pŕoxima.");
76+
System.exit(0);
77+
}
78+
}
79+
}

Diff for: src/view/test.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package view;
2+
3+
import model.Calcular;
4+
5+
public class test {
6+
7+
public static void main(String[] args) {
8+
Calcular calc = new Calcular(1);
9+
10+
System.out.println(calc);
11+
12+
}
13+
14+
}

0 commit comments

Comments
 (0)