Browse the code
| Revision log Information on the revision | |
|---|---|
| Revision: | 263 (differences) |
| Author: | inouire |
| Log message: | client: niveau du bot dans la version |
| Change revision: | |
0
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Copyright 2009-2010 Edouard Garnier de Labareyre * * This file is part of B@ggle. * * B@ggle is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * B@ggle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with B@ggle. If not, see <http://www.gnu.org/licenses/>. */ package threads; import boggle.Solver; import client.Key; import client.Main; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Random; /** * * @author edouard */ public class BotServerConnection extends Thread{ private Solver solver; private String server; private int port; private String password; private Socket socket; private PrintWriter out = null; private BufferedReader in = null; private boolean wants_to_reset=false; public boolean playing=false; static Random r = new Random(); String grid_tmp; public BotServerConnection(String server, int port,String password){ this.server = server; this.port = port; this.password=password; } @Override public void run(){ socket = null; try { socket = new Socket(server, port); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (Exception e) { System.out.println("Erreur à la connexion avec le serveur."); return; } send(Key.NEW_PLAYER+":Robot de "+Main.server.getPlayerName()+":"+4); send(Key.VERSION+":bot level "+Main.mainFrame.getBotLevel()); this.solver=new Solver("fr"); if(password.length()>0){ send(password); } String packet; Key key; String message; try { while ((packet = in.readLine()) != null ) { if(packet.contains(":")){ try{ key = Key.valueOf(packet.substring(0, packet.indexOf(':'))); }catch(Exception e){ key = null; } if(key!=null){ message = packet.substring(packet.indexOf(':')+1).trim(); switch (key){ case READY: send_with_delay(Key.READY+":",1000); break; case RESET: if(!wants_to_reset){ send_with_delay(Key.RESET+":",1000); wants_to_reset=true; } break; case GRID: solver.solveGrid(message.trim()); break; case START: gameStart_hook(); wants_to_reset=false; break; case TIMEOUT: playing=false; break; case END_OF_RESULTS: playing=false; break; default: break; } } } } System.out.println("Déconnecté du serveur"); }catch (IOException ex) { System.out.println("Déconnecté du serveur"); } } public void closeAllConnections(){ send(Key.DISCONNECTED+":"); try { socket.close(); in.close(); out.close(); } catch (IOException ex) {} } public String getServerName(){ if(server.length()==0) return "localhost"; else return server; } public int getPort(){ return port; } private void gameStart_hook(){ this.playing=true; Thread t=new Thread(){ @Override public void run(){ int moy=7000; int div=4; int max_nb_letters=7; if(solver.solutions==null){ return; } int tot=solver.solutions.size(); switch(Main.mainFrame.getBotLevel()){ case 0: div=6; moy=9000; max_nb_letters=4; break; case 1: div=5; moy=7000; max_nb_letters=5; break; case 2: div=4; moy=5000; max_nb_letters=7; break; case 3: div=3; moy=3000; max_nb_letters=8; break; case 4: div=2; moy=2000; max_nb_letters=9; break; } int max=tot/div; System.out.println("Robot niveau "+Main.mainFrame.getBotLevel()+": "+tot+" -> "+max+ " (/"+div+")"); String w; for(int i=0;i<max;i++){ randomWait(moy); if(!playing){ return; } try{ w=solver.solutions.get(BotServerConnection.r.nextInt(tot)); if(w.length()<=max_nb_letters){ send(Key.WORD+":"+solver.solutions.get(BotServerConnection.r.nextInt(tot))); } }catch(Exception e){} } } }; t.start(); } public void randomWait(int moy){ int delay = moy/2 + BotServerConnection.r.nextInt(moy); try { sleep(delay); } catch (InterruptedException ex) { } return; } public void send(String message){ if(out != null){ out.println(message); } } public void send_with_delay(final String message,final int delay){ Thread t = new Thread(){ @Override public void run(){ try { sleep(delay); } catch (InterruptedException ex) { } send(message); return; } }; t.start(); } }

B@ggle