00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00140 if(first) {
00141 srand((unsigned)time(NULL));
00142 first=false;
00143 }
00144 unsigned int nb=(unsigned)rand();
00145
00146 unsigned int final=(unsigned int)(min+((float)rand()/RAND_MAX*(max)));
00147 if(final==0) return 1;
00148 return final;
00149 }