websiteWebsite
baggle B@ggle
Jeu de boggle multi-joueurs en réseau - A free online multiplayer boggle game

 

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:
 /* 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();
    }
}