ClientSJ.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Pierre Schmitt, Ludovic Giacomello, Nhi Ly      *
00003  *                                                                         *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "ClientSJ.h"
00022 
00023 ClientSJ::ClientSJ(string adresse, unsigned int port, string login){ 
00024         mVerrou=new Mutex;
00025         mSocket=new SocketTCP(adresse,port);
00026         Traitement(login);
00027 }
00028 
00029 ClientSJ::~ClientSJ(){
00030         delete mSocket; 
00031         delete mVerrou;
00032         delete mAG;
00033 }
00034 
00035 void ClientSJ::Traitement(string login){
00036         SendHello();
00037         SendLogin(login);
00038         SendRole();
00039         TraitementDeplacement();
00040 }
00041 
00042 void ClientSJ::SendHello(){
00043         cout<<"Initialisation du protocole avec le serveur"<<endl;
00044         cout<<endl<<">> HELLO"<<endl;
00045         mSocket->Envoyer("HELLO");
00046         cout<<"ClientSJ: <<: "<<mSocket->Ecouter()<<endl;
00047 }
00048 
00049 void ClientSJ::SendLogin(string login){
00050         cout<<"Envoi du login au serveur"<<endl;
00051         while(login==""){
00052                 cout<<"Login: "; cin>>login; cout<<endl;
00053         }
00054         string reponse="LOGIN "; reponse+=login;
00055         mSocket->Envoyer(reponse);
00056         cout<<mSocket->Ecouter()<<endl;
00057 }
00058 
00059 void ClientSJ::SendRole(){
00060         cout<<"Attente des places disponibles"<<endl;
00061         string place=mSocket->Ecouter();
00062         istringstream iss(place);
00063         string cmd,code, reponse;
00064         getline(iss,cmd,' '); 
00065         if(cmd=="BUSY"){
00066                 getline(iss,code,' ');
00067                 cout<<"Le serveur nous propose les rôles suivant:"<<endl;
00068                 if(code[0]=='0') cout<<"\t1) Fantome 0"<<endl;
00069                 if(code[1]=='0') cout<<"\t2) Fantome 1"<<endl;
00070                 if(code[2]=='0') cout<<"\t3) Fantome 2"<<endl;
00071                 if(code[3]=='0') cout<<"\t4) Pacman"<<endl;
00072                 bool stop=true;
00073                 while(1){
00074                         unsigned int choix; string role;
00075                         cout<<"Role: "; cin>>choix;
00076                         switch(choix){
00077                                 case 4:{
00078                                         role="PACMAN"; break; 
00079                                 }
00080                                 case 1:{
00081                                         role="F1"; break; 
00082                                 }
00083                                 case 2:{
00084                                         role="F2"; break; 
00085                                 }
00086                                 case 3:{
00087                                         role="F3"; break; 
00088                                 }
00089                                 default: stop=false;
00090                         }
00091                         reponse="GET "; reponse+=role;
00092                         mSocket->Envoyer(reponse);
00093                         if(mSocket->Ecouter()!="ERROR GET ROLE") break;
00094                 }
00095         }
00096 
00097 }
00098 
00099 
00100 void ClientSJ::TraitementDeplacement(){
00101         cout<<"On attend les autres joueurs..."<<endl;
00102         if(mSocket->Ecouter()=="READY"){
00103                 cout<<"Le serveur est prêt à commencer la partie..."<<endl;
00104                 mAG=new AffichageGrille(mSocket, mVerrou);
00105                 mAG->Demarrer();
00106                 while(!mAG->GameOver()){
00107                         char dep=cin.get();
00108                         mVerrou->Lock();
00109                         switch(dep){
00110                                 case '8': {
00111                                         mSocket->Envoyer("HAUT");
00112                                         break;
00113                                 }
00114                                 case '2' : {
00115                                         mSocket->Envoyer("BAS");
00116                                         break;
00117                                 }
00118                                 case '4' : {
00119                                         mSocket->Envoyer("GAUCHE");
00120                                         break;
00121                                 }
00122                                 case '6' : {
00123                                         mSocket->Envoyer("DROITE");
00124                                         break;
00125                                 }
00126                                 default: mSocket->Envoyer("BIDON");
00127                         }
00128                         if(mSocket->Ecouter()=="GAME OVER") {
00129                                 printf( "\n\e[30;47m\e[33mGAME OVER\e[m\e[m\n\n");
00130                                 break;
00131                         }
00132                         mVerrou->Unlock();
00133                 }
00134         }
00135 }
00136 
00137 unsigned int ClientSJ::UintAleat(unsigned int min, unsigned int max){
00138         static bool first=true;
00139         //On initialise le générateur en fonction de l'horloge
00140         if(first) {
00141                 srand((unsigned)time(NULL));
00142                 first=false;
00143         }
00144         unsigned int nb=(unsigned)rand();
00145         //on tire un nombre au hasard et on le place sur le bon intervalle.
00146         unsigned int final=(unsigned int)(min+((float)rand()/RAND_MAX*(max)));
00147         if(final==0) return 1;
00148         return final;
00149 }

Generated on Wed Jan 2 14:01:41 2008 for Pacman by  doxygen 1.5.1