-
Notifications
You must be signed in to change notification settings - Fork 0
/
JordleImplementacao.java
191 lines (155 loc) · 5.81 KB
/
JordleImplementacao.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.*;;
public class JordleImplementacao // (1)
extends UnicastRemoteObject implements Jordle {
static HashMap<Integer, GameStats> activeGames = new HashMap<Integer, GameStats>();
static private int maxGames = 100;
public JordleImplementacao() throws RemoteException {
super(); // (2)
}
static public void showActiveGames(){
System.out.println("Sessões ativas: " + activeGames.keySet().toString());
for (GameStats game : activeGames.values()) {
System.out.println(game);
}
}
public static String changeChar(String str, int idx, char newChar) {
if (idx < 0 || idx >= str.length()) {
throw new IllegalArgumentException("Posição inválida");
}
char[] chars = str.toCharArray(); // Convertendo a string para um array de caracteres
chars[idx] = newChar; // Alterando o caractere na posição especificada
return new String(chars); // Convertendo o array de caracteres de volta para uma string
}
private Integer newSession(){
int rand = new Random().nextInt(maxGames + 1);
while (activeGames.containsKey(rand)) {
rand = new Random().nextInt(maxGames + 1);
}
return rand;
}
private GameStats fetchGame(int sessionId, int gamesQty) throws RemoteException {
GameStats game;
int newSession;
if (sessionId == -1) {
newSession = this.newSession();
game = new GameStats(newSession, gamesQty);
activeGames.put(newSession, game);
System.out.println("Novo jogo iniciado com a sessão: " + Integer.toString(game.sessionId));
return game;
}
if (activeGames.containsKey(sessionId)) {
game = activeGames.get(sessionId);
System.out.println("Jogo recuperado com a sessão: " + Integer.toString(game.sessionId));
return game;
}
throw new RemoteException("Foi mal... A sessão " + Integer.toString(sessionId) + " não existe.");
}
public GameStats getGame(int sessionId, int gamesQty) throws RemoteException {
GameStats game = fetchGame(sessionId, gamesQty);
showActiveGames();
return game;
}
public GameStats newTry(GameStats game) throws RemoteException {
System.out.println("Nova tentativa na sessão " + Integer.toString(game.sessionId) + " com as palavras: " + Arrays.toString(game.wordsToTry));
int numThreads = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
ArrayList<Future<String>> futures = new ArrayList<>(game.gamesQty);
// Submete as tarefas de processamento para execução
for (int i = 0; i < game.gamesQty; ++i) {
final int idx = i;
Future<String> future = executor.submit(new Callable<String>() {
public String call() throws Exception {
return checkWord(game.words[idx], game.wordsToTry[idx]);
}
});
futures.add(idx, future);
}
// Aguarda a conclusão de todas as tarefas e armazena os resultados no array
for (int i = 0; i < game.gamesQty; ++i) {
try {
game.masks[i] = futures.get(i).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
executor.shutdown();
// Controla a vitória de cada jogo
for (int i = 0; i < game.gamesQty; ++i) game.winStates[i] = game.masks[i].equals("22222");
if (this.checkWins(game.winStates)) {
game.winner = true;
game.running = false;
activeGames.remove(game.sessionId);
}
// Controla tentativas do jogo
game.tries += 1;
if (game.tries >= 4) {
game.running = false;
activeGames.remove(game.sessionId);
}
// activeGames.remove(game.sessionId);
activeGames.put(game.sessionId, game);
showActiveGames();
return game;
}
private boolean checkWins(boolean[] winStates) {
for (boolean value : winStates) {
if (!value) {
return false;
}
}
return true;
}
static HashMap<Character, Integer> getCountCharMap(String word) {
char c;
HashMap<Character, Integer> countCharMap = new HashMap<Character, Integer>();
for (int i = 0; i < word.length(); ++i) {
c = word.charAt(i);
if (countCharMap.containsKey(c)) {
countCharMap.put(c, countCharMap.get(c) + 1);
} else {
countCharMap.put(c, 1);
}
}
return countCharMap;
}
static String checkWord(String word, String wordToTry) throws RemoteException{
String mask = "00000";
if (wordToTry.length() != 5) {
throw new RemoteException("A palavra " + wordToTry + " não tem 5 caracteres.");
}
// Controla a lógica da máscara
HashMap<Character, Integer> countCharMap = getCountCharMap(word);
// Verifica letras corretas no lugar correto
for (int i = 0; i < word.length(); ++i) {
char trueChar = word.charAt(i);
char tryChar = wordToTry.charAt(i);
if (trueChar == tryChar) {
mask = changeChar(mask, i, '2');
countCharMap.put(trueChar, countCharMap.get(trueChar) - 1);
}
}
System.out.println(word);
System.out.println(wordToTry);
System.out.println(mask);
// Verifica letras corretas no lugar errado
for (int i = 0; i < word.length(); ++i) {
char trueChar = word.charAt(i);
char tryChar = wordToTry.charAt(i);
char maskChar = mask.charAt(i);
if (countCharMap.containsKey(tryChar) && countCharMap.get(tryChar) > 0 && maskChar != '2') {
mask = changeChar(mask, i, '1');
countCharMap.put(trueChar, countCharMap.get(trueChar) - 1);
}
}
System.out.println(word);
System.out.println(wordToTry);
System.out.println(mask);
return mask;
}
}