-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOJogo.java
More file actions
112 lines (85 loc) · 3.38 KB
/
OJogo.java
File metadata and controls
112 lines (85 loc) · 3.38 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Random;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
public class OJogo {
public final String[] categorias = {"Nome", "Animal", "Cor", "Verbo", "País", "Objeto"};
private char letraAtual;
private Random r = new Random();
private long tempoInicial;
private HashMap<String, Integer> pontuacao = new HashMap<String,Integer>();
private ArrayList<Row> palavrasAtuais;
public OJogo(){
/* Eu sou imune a essa piada, mas que vocês sofram aí. */
System.out.println("Que os jogos comecem.");
for(String categoria : categorias){
palavrasAtuais.add(new Row(categoria));
}
}
/* rola o d20 */
public void rerollLetra(){
char letraAnterior = this.letraAtual;
while(letraAnterior == this.letraAtual){
this.letraAtual = (char) (r.nextInt(26) + 'a');
}
}
/* O char da letra atual da rodada, para o servidor mandar aos clientes */
public char getLetra(){
return letraAtual;
}
/* O servidor adiciona as respostas de usuário que recebe */
public void addResposta(String user, String texto, String categoria){
for(Row linha : palavrasAtuais){
if(linha.nomeDaCategoria == categoria){
linha.inserirResposta(user, texto);
break;
}
}
}
public void calcularFrequencia(){
for(Row linha : palavrasAtuais){
Map<String, Integer> resultado = linha.calcularFrequencia();
for(String user : resultado.keySet()){
if(pontuacao.containsKey(user)){
//a freq. de repeticoes deve passar pelo protocoloDePontos
pontuacao.put(user, pontuacao.get(user) + protocoloDePontos(resultado.get(user)));
}else{
pontuacao.put(user, protocoloDePontos(resultado.get(user)));
}
}
}
}
public Integer protocoloDePontos(Integer entrada){
/* Retorna a pontuacao de acordo com a quantidade de repeticoes
Se quiser rebalancear o jogo, troque os valores aqui. */
if(entrada.equals(1)){return Integer.valueOf(25);}
else if(entrada.equals(2)){return Integer.valueOf(15);}
else if(entrada.equals(3)){return Integer.valueOf(10);}
else return Integer.valueOf(5);
}
//Para reciclar o objeto do Jogo.
public void limparRespostas(){
this.palavrasAtuais.clear();
}
public String terminarOJogo(){
Map.Entry<String,Integer> maxEntry = null;
for(Map.Entry<String, Integer> entry : pontuacao.entrySet()){
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0){
maxEntry = entry;
}
}
this.limparRespostas();
this.pontuacao.clear();
/* Vamos passar quem venceu à camada acima, para que o server possa anunciar. */
return "O vencedor foi: " + maxEntry.getKey();
}
//Precisamos de um timer que marque um tempo em que as respostas são aceitadas.
//Quando o tempo acaba, o protocolo deve rejeitar as respostas.
public void startTimer(){
this.tempoInicial = System.currentTimeMillis();
}
public long checarTimer(){
long deltaTempo = System.currentTimeMillis() - tempoInicial;
return deltaTempo;
}
}