cgicc: cardgame.cpp Source File

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

# cardgame.cpp

Go to the documentation of this file.

    00001 /* -*-mode:c++; c-file-style: "gnu";-*- */
    00002 /*
    00003  *  $Id: cardgame.cpp,v 1.7 2009/01/03 17:26:43 sebdiaz Exp $
    00004  *
    00005  *  Copyright (C) 2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
    00006  *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
    00007  *
    00008  *  This library is free software; you can redistribute it and/or
    00009  *  modify it under the terms of the GNU Lesser General Public
    00010  *  License as published by the Free Software Foundation; either
    00011  *  version 3 of the License, or (at your option) any later version.
    00012  *
    00013  *  This library is distributed in the hope that it will be useful,
    00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00016  *  Lesser General Public License for more details.
    00017  *
    00018  *  You should have received a copy of the GNU Lesser General Public
    00019  *  License along with this library; if not, write to the Free Software
    00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
    00021  */
    00022
    00030 #include <iostream>
    00031 #include <vector>
    00032 #include <iterator>
    00033 #include <string>
    00034 #include <cstdlib>
    00035 #include <ctime>
    00036 #include <sstream>
    00037 #include <fstream>
    00038 #include <queue>
    00039 #include <algorithm>
    00040 #include "cgicc/CgiDefs.h"
    00041 #include "cgicc/Cgicc.h"
    00042 #include "cgicc/HTTPHTMLHeader.h"
    00043 #include "cgicc/HTMLClasses.h"
    00044 #include "cgicc/HTTPCookie.h"
    00045 #define COOKIE_NAME "ELPIELOJUEGO"
    00046 #define COOKIE_FILE_NAME "sessions.tmp"
    00047 #define GAME_FILE_NAME "games.tmp"
    00048 #define MAX_GAME 10
    00049
    00050 using namespace std;
    00051 using namespace cgicc;
    00052
    00053
    00054
    00061 struct datasplayer
    00062 {
    00067         vector <string > *cardsList;
    00068
    00072         string identifiant;
    00073
    00077         bool isPlaying;
    00078
    00082         int points;
    00083
    00087         string actualCard;
    00088 };
    00089
    00090
    00096 struct datasgame
    00097 {
    00102         vector <datasplayer *> *playersList;
    00103
    00107         vector<string> *playedCards;
    00108
    00112         vector<string> *piocheCards;
    00113
    00114 };
    00115
    00120 namespace CardGameTools
    00121 {
    00134         string convertStructToString( datasplayer *pPlayer)
    00135         {
    00136
    00137                 std::vector<string>::iterator itVectorData;
    00138                 stringstream buffer;
    00139                 buffer <<pPlayer->identifiant<<"::";
    00140                 for(itVectorData = pPlayer->cardsList->begin(); itVectorData != pPlayer->cardsList->end(); itVectorData++)
    00141                 {
    00142
    00143                         buffer << *itVectorData;
    00144                         if (itVectorData != pPlayer->cardsList->end())
    00145                         {
    00146                                 buffer <<"||";
    00147
    00148                         }
    00149                 }
    00150
    00151                 //Global Separator
    00152
    00153                 return buffer.str();
    00154         }
    00155
    00168         datasplayer * convertStringToStuct( string pPlayer)
    00169         {
    00170                 datasplayer *datas= new datasplayer;
    00171                 char *carList=(char *)pPlayer.c_str();
    00172                 int wordCounter=0;
    00173                 string word;
    00174                 stringstream actualWord;
    00175                 for (unsigned int i=0;i<pPlayer.size();i++)
    00176                 {
    00177                         if (i+1<pPlayer.size())
    00178                         if (carList[i]==':'&&carList[i+1]==':')
    00179                         {
    00180                                 word=actualWord.str();
    00181                                 //first , the player name
    00182                                 if (wordCounter==0)
    00183                                 {
    00184                                         //first is no data word
    00185                                 }
    00186                                 else
    00187                                 if (wordCounter==1)
    00188                                 {
    00189                                         datas->identifiant=word;
    00190                                 }else
    00191                                 {
    00192                                         datas->cardsList->push_back(word);
    00193                                 }
    00194                                 wordCounter++;
    00195                                 actualWord.clear();
    00196                                 i++;
    00197                         }else {
    00198                                 actualWord <<carList[i];
    00199                         }
    00200                 }
    00201
    00202
    00203                 return datas;
    00204         }
    00205
    00214         string getNUMCookie(std::vector< HTTPCookie > pCookieVector)
    00215         {
    00216
    00217                 if (pCookieVector.size()== 0)
    00218                 {
    00219
    00220                         return "";
    00221                 }
    00222                 std::vector<HTTPCookie>::iterator itVectorData;
    00223                 for(itVectorData = pCookieVector.begin(); itVectorData != pCookieVector.end(); itVectorData++)
    00224                 {
    00225
    00226                         HTTPCookie theCookie = *(itVectorData);
    00227
    00228                         if (theCookie.getName ().compare(COOKIE_NAME)==0)
    00229                         {
    00230                                 return theCookie.getValue ();
    00231                         }
    00232                 }
    00233
    00234
    00235                 return "";
    00236         }
    00237
    00245         string getValue(string pName)
    00246         {
    00247
    00248                 ifstream inFile;
    00249                 inFile.open(COOKIE_FILE_NAME);
    00250                 if (!inFile) {
    00251                         ofstream outFile(COOKIE_FILE_NAME, ios::out);
    00252                         outFile.put('\n');
    00253                         outFile.close();
    00254                         inFile.open(COOKIE_FILE_NAME);
    00255                 }
    00256
    00257                 // Lecture ligne a ligne
    00258                   while (inFile&&!inFile.eof ())
    00259                   {
    00260                         char ligne[32000];
    00261                         std::string s;
    00262
    00263                         inFile.getline (ligne, sizeof (ligne));
    00264                         s = ligne;
    00265
    00266                         if (s.find (pName)!= string::npos)
    00267                         {
    00268
    00269                                 s.replace(0,pName.size(),"");
    00270                                 inFile.close();
    00271                                 return s;
    00272                         }
    00273                   }
    00274                   inFile.close();
    00275                   return "";
    00276
    00277         }
    00278
    00286         string getFileGame(string pName)
    00287         {
    00288
    00289                 ifstream inFile;
    00290                 inFile.open(GAME_FILE_NAME);
    00291                 if (!inFile) {
    00292                         ofstream outFile(GAME_FILE_NAME, ios::out);
    00293                         outFile.put('\n');
    00294                         outFile.close();
    00295                         inFile.open(GAME_FILE_NAME);
    00296                 }
    00297                 // Lecture ligne a ligne
    00298                   while (inFile&&!inFile.eof ())
    00299                   {
    00300                         char ligne[32000];
    00301                         std::string s;
    00302
    00303                         inFile.getline (ligne, sizeof (ligne));
    00304                         s = ligne;
    00305
    00306                         if (s.find (pName)!= string::npos)
    00307                         {
    00308
    00309                                 inFile.close();
    00310                                 return s;
    00311                         }
    00312                   }
    00313                   inFile.close();
    00314                   return "";
    00315
    00316         }
    00317
    00324         datasgame *getGame(string pName)
    00325         {
    00326
    00327                 datasgame *dgame= new datasgame;
    00328                 dgame->playersList=new vector<datasplayer*>;
    00329
    00330
    00331                 string vGame=getFileGame(pName);
    00332                 if (vGame.compare("")==0)
    00333                 {
    00334                         return NULL;
    00335                 }
    00336
    00337
    00338
    00339                 char *carList=(char *)vGame.c_str();
    00340                 int wordCounter=0;
    00341                 string word;
    00342                 stringstream actualWord;
    00343                 int vNBPLayers=0;
    00344                 int playerCounter=0;
    00345                 int playerCounterElement=0;
    00346                 int vNBCards=0;
    00347                 int vCardsCounter=0;
    00348                 vector <string > *cardsList= new vector<string>;
    00349                 string identifiant;
    00350                 string actualCard;
    00351                 bool isPlaying=false;
    00352                 int points;
    00353
    00354                 int vNBCardsQueue1=0;
    00355                 int vCardsCounterQ1=0;
    00356                 int vNBCardsQueue2=0;
    00357                 int vCardsCounterQ2=0;
    00358                 bool vCountedCardsQ1;
    00359                 bool vCountedCardsQ2;
    00360                 vCountedCardsQ1=false;
    00361                 vCountedCardsQ2=false;
    00362                 vector <string > *queue1= new vector<string>;
    00363                 dgame->playedCards=queue1;
    00364
    00365                 vector <string > *queue2= new vector<string>;
    00366                 dgame->piocheCards=queue2;
    00367
    00368                 for (unsigned int i=0;i<vGame.size();i++)
    00369                 {
    00370                         if (i+1<vGame.size())
    00371                         if (carList[i]==':'&&carList[i+1]==':')
    00372                         {
    00373
    00374                                 word=actualWord.str();
    00375
    00376
    00377
    00378                                 //first , NB Players Value
    00379                                 if (wordCounter==0)
    00380                                 {
    00381
    00382                                         vNBPLayers=atoi(word.c_str());
    00383                                 }else
    00384                                 {
    00385                                         //Add of a player
    00386                                         if (playerCounter<vNBPLayers)
    00387                                         {
    00388
    00389                                                 //In first the name
    00390                                                 if (playerCounterElement==0)
    00391                                                 {
    00392
    00393                                                         identifiant=word;
    00394                                                 }
    00395                                                 if (playerCounterElement==1)
    00396                                                 {
    00397
    00398                                                         isPlaying=(word.compare("1")==0)?true:false;
    00399                                                 }
    00400                                                 if (playerCounterElement==2)
    00401                                                 {
    00402
    00403                                                         points=atoi(word.c_str());
    00404                                                 }
    00405                                                 if (playerCounterElement==3)
    00406                                                 {
    00407
    00408                                                         actualCard=word;
    00409                                                 }
    00410                                                 if (playerCounterElement==4)
    00411                                                 {
    00412
    00413                                                         vNBCards=atoi(word.c_str());
    00414                                                 }
    00415                                                 if (playerCounterElement>=5&&vCardsCounter<vNBCards)
    00416                                                 {
    00417
    00418                                                         cardsList->push_back(word);
    00419                                                         vCardsCounter++;
    00420                                                 }
    00421                                                 if (vCardsCounter==vNBCards&&playerCounterElement>=5)
    00422                                                 {
    00423
    00424                                                         datasplayer *vPlay= new datasplayer;
    00425
    00426                                                         vPlay->identifiant=identifiant;
    00427                                                         vPlay->points=points;
    00428                                                         vPlay->isPlaying=isPlaying;
    00429                                                         vPlay->cardsList=cardsList;
    00430                                                         vPlay->actualCard=actualCard;
    00431                                                         dgame->playersList->push_back(vPlay     );
    00432                                                         playerCounter++;
    00433                                                         vCardsCounter=0;
    00434                                                         playerCounterElement=0;
    00435
    00436                                                         cardsList=new vector <string >;
    00437
    00438
    00439                                                 }else{playerCounterElement++;   }
    00440
    00441
    00442                                         }
    00443                                         else
    00444                                         {//Saved queue
    00445
    00446                                                 if (vNBCardsQueue1==0&&!vCountedCardsQ1)
    00447                                                 {
    00448                                                         vNBCardsQueue1=atoi(word.c_str());
    00449                                                         vCountedCardsQ1=true;
    00450
    00451
    00452                                                 }else
    00453                                                 {
    00454
    00455                                                         if (vCardsCounterQ1<vNBCardsQueue1)
    00456                                                         {
    00457
    00458                                                                 queue1->push_back(word);
    00459
    00460                                                                 vCardsCounterQ1++;
    00461                                                         }else
    00462                                                         if (!vCountedCardsQ2&&vNBCardsQueue2==0&&vCardsCounterQ1==vNBCardsQueue1)
    00463                                                         {
    00464
    00465                                                                 vNBCardsQueue2=atoi(word.c_str());
    00466                                                                 vCountedCardsQ2=true;
    00467                                                                 //dgame->playedCards=queue1;
    00468
    00469
    00470                                                         }else
    00471                                                         if (vCardsCounterQ2<vNBCardsQueue2)
    00472                                                         {
    00473
    00474                                                                 queue2->push_back(word);
    00475
    00476                                                                 vCardsCounterQ2++;
    00477                                                         }
    00478                                                         if (vCardsCounterQ2==vNBCardsQueue2&&vCardsCounterQ2!=0)
    00479                                                         {
    00480
    00481                                                                 //dgame->piocheCards=queue2;
    00482
    00483                                                         }
    00484
    00485                                                 }
    00486                                         }
    00487                                 }
    00488
    00489
    00490                                 wordCounter++;
    00491                                 actualWord.str("");
    00492                                 actualWord.clear();
    00493                                 actualWord.flush();
    00494
    00495
    00496                                 i++;
    00497                         }else {
    00498
    00499                                 actualWord <<carList[i];
    00500                         }
    00501                 }
    00502                 return dgame;
    00503
    00504         }
    00505
    00512         void writeValue(string pName,string pValue)
    00513         {
    00514
    00515                 ofstream outFile(COOKIE_FILE_NAME, ios::out|ios::app);
    00516
    00517                 outFile << pName<<"::"<<pValue<<"\n";
    00518
    00519                 outFile.close();
    00520
    00521         }
    00522
    00529         void writeFileGame(string pName,string pValue)
    00530         {
    00531
    00532                 ifstream inFile;
    00533
    00534                 //Find Index of the game
    00535                 inFile.open(GAME_FILE_NAME,ios::in);
    00536                 if (!inFile) {
    00537
    00538                         ofstream outFile(GAME_FILE_NAME, ios::out|ios::app);
    00539
    00540
    00541                         outFile << "\n";
    00542
    00543                         outFile.close();
    00544                         inFile.open(GAME_FILE_NAME,ios::in);
    00545                 }
    00546                 //read of the file
    00547                 if (inFile) {
    00548                         stringstream buffer;
    00549                         bool haveWrited;
    00550                         haveWrited=false;
    00551                         while (inFile&&!inFile.eof ())
    00552                         {
    00553                                 char ligne[32000];
    00554                                 std::string s;
    00555                                 inFile.getline (ligne, sizeof (ligne));
    00556                                 s = ligne;
    00557
    00558                                 if (s.find (pName)!= string::npos)
    00559                                 {
    00560
    00561                                         buffer << pValue;
    00562
    00563                                         haveWrited=true;
    00564
    00565                                 }
    00566                                 else
    00567                                 {
    00568                                         buffer << s ;
    00569                                 }
    00570
    00571                         }
    00572                         inFile.close();
    00573                         ofstream outFile(GAME_FILE_NAME, ios::out|ios::trunc);
    00574
    00575
    00576                         outFile << buffer.str();
    00577                         if (!haveWrited)
    00578                         {
    00579                                 outFile << pValue<<"\n";
    00580                         }
    00581
    00582                         outFile.close();
    00583
    00584
    00585                 }
    00586
    00587
    00588
    00589         }
    00590
    00591
    00598         void writeGame(datasplayer *pPlayer,datasgame *pGame)
    00599         {
    00600
    00601                 stringstream buffer;
    00602
    00603                 datasplayer * itVectorData;
    00604                 buffer <<pGame->playersList->size()<<"::";
    00605                 for(unsigned int i=0;i<pGame->playersList->size(); i++)
    00606                 {
    00607
    00608                         itVectorData=pGame->playersList->at(i);
    00609
    00610
    00611                         std::vector<string>::iterator itVectorData2;
    00612                         buffer <<itVectorData->identifiant<<"::"<<((itVectorData->isPlaying)?"1":"0")<<"::"<<itVectorData->points<<"::"<<itVectorData->actualCard<<"::";
    00613                         //NBCards
    00614                         buffer <<itVectorData->cardsList->size()<<"::";
    00615                         for(itVectorData2 = itVectorData->cardsList->begin(); itVectorData2 != itVectorData->cardsList->end(); itVectorData2++)
    00616                         {
    00617                                 buffer <<*itVectorData2<<"::";
    00618
    00619                         }
    00620
    00621                 }
    00622
    00623                 //NBCards played
    00624                 buffer <<pGame->playedCards->size()<<"::";
    00625                 for (unsigned int i=0;i<pGame->playedCards->size();i++)
    00626                 {
    00627                         buffer <<pGame->playedCards->at(i)<<"::";
    00628
    00629                 }
    00630
    00631
    00632
    00633                 //NBCards in queue
    00634
    00635                 buffer <<pGame->piocheCards->size()<<"::";
    00636
    00637                 for (unsigned int i=0;i<pGame->piocheCards->size();i++)
    00638                 {
    00639                         buffer <<pGame->piocheCards->at(i)<<"::";
    00640
    00641
    00642                 }
    00643
    00644
    00645
    00646                 writeFileGame(pPlayer->identifiant,buffer.str());
    00647
    00648         }
    00649
    00655         string generateUnicCookie()
    00656         {
    00657
    00658                 srand ( time(NULL) );
    00659                 int nb_aleatoire;
    00660                 nb_aleatoire=(rand()%100)+1;
    00661
    00662
    00663                 stringstream buffer;
    00664                 buffer << nb_aleatoire<<"_"<<time(NULL);
    00665
    00666                 string vNum=buffer.str() ;
    00667
    00668                 return vNum;
    00669         }
    00670
    00676         int countGame()
    00677         {
    00678
    00679                 ifstream inFile;
    00680                 inFile.open(GAME_FILE_NAME);
    00681                 if (!inFile) {
    00682                         return 0;
    00683                 }
    00684                 int vNBLigne=0;
    00685
    00686                 // Lecture ligne a ligne
    00687                 while (inFile&&!inFile.eof ())
    00688                   {
    00689
    00690                         string tempo;
    00691                         inFile >> tempo;
    00692
    00693                         vNBLigne++;
    00694                   }
    00695
    00696                   inFile.close();
    00697
    00698
    00699                   return vNBLigne;
    00700
    00701         }
    00702
    00708         vector<string> *loadAndMixCards()
    00709         {
    00710                 vector<string> UnorderedPioche;
    00711                 UnorderedPioche.push_back("TA");
    00712                 UnorderedPioche.push_back("TZ");
    00713                 UnorderedPioche.push_back("T2");
    00714                 UnorderedPioche.push_back("T3");
    00715                 UnorderedPioche.push_back("T4");
    00716                 UnorderedPioche.push_back("T5");
    00717                 UnorderedPioche.push_back("T6");
    00718                 UnorderedPioche.push_back("T7");
    00719                 UnorderedPioche.push_back("T8");
    00720                 UnorderedPioche.push_back("T9");
    00721                 UnorderedPioche.push_back("TD");
    00722                 UnorderedPioche.push_back("TR");
    00723                 UnorderedPioche.push_back("TB");
    00724                 UnorderedPioche.push_back("CA");
    00725                 UnorderedPioche.push_back("CZ");
    00726                 UnorderedPioche.push_back("C2");
    00727                 UnorderedPioche.push_back("C3");
    00728                 UnorderedPioche.push_back("C4");
    00729                 UnorderedPioche.push_back("C5");
    00730                 UnorderedPioche.push_back("C6");
    00731                 UnorderedPioche.push_back("C7");
    00732                 UnorderedPioche.push_back("C8");
    00733                 UnorderedPioche.push_back("C9");
    00734                 UnorderedPioche.push_back("CD");
    00735                 UnorderedPioche.push_back("CR");
    00736                 UnorderedPioche.push_back("CB");
    00737                 UnorderedPioche.push_back("PA");
    00738                 UnorderedPioche.push_back("PZ");
    00739                 UnorderedPioche.push_back("P2");
    00740                 UnorderedPioche.push_back("P3");
    00741                 UnorderedPioche.push_back("P4");
    00742                 UnorderedPioche.push_back("P5");
    00743                 UnorderedPioche.push_back("P6");
    00744                 UnorderedPioche.push_back("P7");
    00745                 UnorderedPioche.push_back("P8");
    00746                 UnorderedPioche.push_back("P9");
    00747                 UnorderedPioche.push_back("PD");
    00748                 UnorderedPioche.push_back("PR");
    00749                 UnorderedPioche.push_back("PB");
    00750                 UnorderedPioche.push_back("HA");
    00751                 UnorderedPioche.push_back("HZ");
    00752                 UnorderedPioche.push_back("H2");
    00753                 UnorderedPioche.push_back("H3");
    00754                 UnorderedPioche.push_back("H4");
    00755                 UnorderedPioche.push_back("H5");
    00756                 UnorderedPioche.push_back("H6");
    00757                 UnorderedPioche.push_back("H7");
    00758                 UnorderedPioche.push_back("H8");
    00759                 UnorderedPioche.push_back("H9");
    00760                 UnorderedPioche.push_back("HD");
    00761                 UnorderedPioche.push_back("HR");
    00762                 UnorderedPioche.push_back("HB");
    00763                 srand ( time(NULL) );
    00764                 int nb_aleatoire;
    00765
    00766                 vector<string> *vRet= new vector<string>;
    00767                 while(UnorderedPioche.size()>0)
    00768                 {
    00769
    00770                         nb_aleatoire=(rand()%UnorderedPioche.size());
    00771                         vRet->push_back(UnorderedPioche[nb_aleatoire]);
    00772                         UnorderedPioche.erase((UnorderedPioche.begin())+nb_aleatoire);
    00773                 }
    00774                 return vRet;
    00775
    00776         }
    00777
    00784         int calculateCard(string pCard)
    00785         {
    00786
    00787                 if (pCard.compare("TA")==0) return 10;
    00788                 if (pCard.compare("TZ")==0) return 14;
    00789                 if (pCard.compare("T2")==0) return 2;
    00790                 if (pCard.compare("T3")==0) return 3;
    00791                 if (pCard.compare("T4")==0) return 4;
    00792                 if (pCard.compare("T5")==0) return 5;
    00793                 if (pCard.compare("T6")==0) return 6;
    00794                 if (pCard.compare("T7")==0) return 7;
    00795                 if (pCard.compare("T8")==0) return 8;
    00796                 if (pCard.compare("T9")==0) return 9;
    00797                 if (pCard.compare("TD")==0) return 12;
    00798                 if (pCard.compare("TR")==0) return 13;
    00799                 if (pCard.compare("TB")==0) return 11;
    00800                 if (pCard.compare("CA")==0) return 10;
    00801                 if (pCard.compare("CZ")==0) return 14;
    00802                 if (pCard.compare("C2")==0) return 2;
    00803                 if (pCard.compare("C3")==0) return 3;
    00804                 if (pCard.compare("C4")==0) return 4;
    00805                 if (pCard.compare("C5")==0) return 5;
    00806                 if (pCard.compare("C6")==0) return 6;
    00807                 if (pCard.compare("C7")==0) return 7;
    00808                 if (pCard.compare("C8")==0) return 8;
    00809                 if (pCard.compare("C9")==0) return 9;
    00810                 if (pCard.compare("CD")==0) return 12;
    00811                 if (pCard.compare("CR")==0) return 13;
    00812                 if (pCard.compare("CB")==0) return 11;
    00813                 if (pCard.compare("PA")==0) return 10;
    00814                 if (pCard.compare("PZ")==0) return 14;
    00815                 if (pCard.compare("P2")==0) return 2;
    00816                 if (pCard.compare("P3")==0) return 3;
    00817                 if (pCard.compare("P4")==0) return 4;
    00818                 if (pCard.compare("P5")==0) return 5;
    00819                 if (pCard.compare("P6")==0) return 6;
    00820                 if (pCard.compare("P7")==0) return 7;
    00821                 if (pCard.compare("P8")==0) return 8;
    00822                 if (pCard.compare("P9")==0) return 9;
    00823                 if (pCard.compare("PD")==0) return 12;
    00824                 if (pCard.compare("PR")==0) return 13;
    00825                 if (pCard.compare("PB")==0) return 11;
    00826                 if (pCard.compare("HA")==0) return 10;
    00827                 if (pCard.compare("HZ")==0) return 14;
    00828                 if (pCard.compare("H2")==0) return 2;
    00829                 if (pCard.compare("H3")==0) return 3;
    00830                 if (pCard.compare("H4")==0) return 4;
    00831                 if (pCard.compare("H5")==0) return 5;
    00832                 if (pCard.compare("H6")==0) return 6;
    00833                 if (pCard.compare("H7")==0) return 7;
    00834                 if (pCard.compare("H8")==0) return 8;
    00835                 if (pCard.compare("H9")==0) return 9;
    00836                 if (pCard.compare("HD")==0) return 12;
    00837                 if (pCard.compare("HR")==0) return 13;
    00838                 if (pCard.compare("HB")==0) return 11;
    00839                 return 0;
    00840
    00841         }
    00842
    00851         datasgame * createGame(datasplayer *vPlayer)
    00852         {
    00853
    00854                 datasplayer *p1=new datasplayer;
    00855                 datasplayer *p2=new datasplayer;
    00856                 datasplayer *p3=new datasplayer;
    00857                 datasplayer *p4=new datasplayer;
    00858                 p1->identifiant=vPlayer->identifiant;
    00859                 p1->actualCard="";
    00860                 p1->isPlaying=true;
    00861                 p1->points=0;
    00862                 p2->identifiant="FREE1";
    00863                 p2->actualCard="";
    00864                 p2->isPlaying=false;
    00865                 p2->points=0;
    00866                 p3->identifiant="FREE2";
    00867                 p3->actualCard="";
    00868                 p3->isPlaying=false;
    00869                 p3->points=0;
    00870                 p4->identifiant="FREE3";
    00871                 p4->actualCard="";
    00872                 p4->isPlaying=false;
    00873                 p4->points=0;
    00874
    00875                 datasgame *myGame= new datasgame;
    00876                 myGame->playersList=new vector <datasplayer *>;
    00877                 myGame->playersList->push_back(p1);
    00878                 myGame->playersList->push_back(p2);
    00879                 myGame->playersList->push_back(p3);
    00880                 myGame->playersList->push_back(p4);
    00881                 myGame->playedCards=new vector<string>;
    00882
    00883                 myGame->piocheCards=loadAndMixCards();
    00884
    00885                 int vNbCards=myGame->piocheCards->size();
    00886                 //distribution des cartes
    00887                 for (unsigned int i=0;i<myGame->playersList->size();i++)
    00888                 {
    00889
    00890                         myGame->playersList->at(i)->cardsList=new vector<string>;
    00891                         for (unsigned int j=0;j<vNbCards/myGame->playersList->size();j++)
    00892                         {
    00893
    00894                                 myGame->playersList->at(i)->cardsList->push_back(myGame->piocheCards->front());
    00895                                 myGame->piocheCards->erase(myGame->piocheCards->begin());
    00896                         }
    00897
    00898                 }
    00899
    00900
    00901                 writeGame(p1,myGame);
    00902                 return myGame;
    00903         }
    00904
    00910         void drawCards(vector <string> *cardList)
    00911         {
    00912
    00913
    00914                 for (unsigned int i=0;i<cardList->size();i++)
    00915                 {
    00916                         cout <<"<div style=\"position:absolute;top:50;left:"<<i*150+150<<"\" >";
    00917                         cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<cardList->at(i)<<".png\" alt=\"Carte ["<<i<<"]="<<cardList->at(i)<<"\" />"<<endl;
    00918                         cout <<"</div>";
    00919                 }
    00920                 cout <<"<br />";
    00921         }
    00922
    00931         void writeWinner(datasplayer *vPlayer,datasgame *pGame)
    00932         {
    00933                 unsigned int winner=0;
    00934                 int scoreOfTheWinner=0;
    00935                 unsigned int playerId=0;
    00936                 for (unsigned int i=0;i<pGame->playersList->size();i++)
    00937                 {
    00938                         if (vPlayer->identifiant.compare(pGame->playersList->at(i)->identifiant)==0)
    00939                         {
    00940                                 playerId=i;
    00941                         }
    00942                         if (scoreOfTheWinner<pGame->playersList->at(i)->points)
    00943                         {
    00944                                 winner=i;
    00945                                 scoreOfTheWinner=pGame->playersList->at(i)->points;
    00946                         }
    00947                 }
    00948
    00949                 cout <<"<div style=\"position:absolute;top:50;left:"<<150<<"\" >";
    00950                 if (playerId==winner)
    00951                 {
    00952                         cout <<"<h2>YOU WIN !</h2>"<<endl;
    00953                         cout <<"<b>Your score is : "<<scoreOfTheWinner<<"</b>"<<endl;
    00954                 }else
    00955                 {
    00956                         cout <<"<h2>YOU LOSS !</h2>"<<endl;
    00957                         cout <<"<b>The winner is : "<<pGame->playersList->at(winner)->identifiant<<"</b>"<<endl;
    00958                         cout <<"<b>The score is : "<<scoreOfTheWinner<<"</b>"<<endl;
    00959                 }
    00960                 cout <<"</div>";
    00961
    00962                 cout <<"<br />";
    00963         }
    00964
    00971         void drawInfos(datasplayer *vPlayer)
    00972         {
    00973                 cout <<"<div style=\"position:absolute;width:140;top:50;left:"<<0<<"\" >";
    00974                 cout <<"The latest Played Cards ";
    00975                 cout <<"</div>";
    00976                 cout <<"<div style=\"width:140;position:absolute;top:180;left:"<<0<<"\" >";
    00977                 cout <<"Actual Cards in the Game<br>You are the player :"<<vPlayer->identifiant;
    00978                 cout <<"</div>";
    00979                 cout <<"<div style=\"width:140;position:absolute;top:375;left:"<<0<<"\" >";
    00980                 cout <<"Your Cards, you can choose one card.";
    00981                 cout <<"</div>";
    00982         }
    00983
    00991         void drawPlayers(datasgame *pGame)
    00992         {
    00993
    00994                 bool vFirst=false;
    00995                 for (unsigned int i=0;i<pGame->playersList->size();i++)
    00996                 {
    00997                         bool afficheFirst=false;
    00998                         if (vFirst==false&&pGame->playersList->at(i)->actualCard.compare("")!=0)
    00999                         {
    01000                                 vFirst=true;
    01001                                 afficheFirst=true;
    01002                         }
    01003                         cout <<"<div style=\"outline-color:"<<((afficheFirst==false)?"black":"red")<<";outline-style:solid;outline-width:"<<((afficheFirst==false)?"1":"2")<<"px;position:absolute;top:180;left:"<<i*200+150<<"\" >";
    01004                         cout <<"Name :"<<pGame->playersList->at(i)->identifiant<<"<br>";
    01005                         cout <<"Score :"<<pGame->playersList->at(i)->points<<"<br>";
    01006
    01007                         cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<pGame->playersList->at(i)->actualCard<<".png\" alt=\" \" />"<<endl;
    01008                         if  (afficheFirst==true)
    01009                         {
    01010                                 cout <<"<br>The color to play";
    01011                         }
    01012                         cout <<"</div>";
    01013                 }
    01014                 cout <<"<br />";
    01015         }
    01016
    01024         void drawCardInPlay(datasgame *pGame)
    01025         {
    01026
    01027
    01028                 for (unsigned int i=0;i<pGame->playedCards->size();i++)
    01029                 {
    01030                         cout <<"<div style=\"position:absolute;top:100;left:"<<i*110<<"\" >";
    01031                         cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<pGame->playedCards->at(i)<<".png\" alt=\"Carte ["<<i<<"]="<<pGame->playedCards->at(i)<<"\" />"<<endl;
    01032                         cout <<"</div>";
    01033                 }
    01034                 cout <<"<br />";
    01035         }
    01036
    01044         void drawPlayerCards(datasplayer *vPlayer)
    01045         {
    01046                 std::sort (vPlayer->cardsList->begin(),vPlayer->cardsList->end());
    01047                 cout <<"<form name=\"cards\">";
    01048                 cout <<"<input type=\"hidden\" name=\"actionner\" value=\"\">";
    01049                 cout <<"<input type=\"hidden\" name=\"card\" value=\"\">";
    01050                 //affiche les cartes du joueurs
    01051                 for (unsigned int i=0;i<vPlayer->cardsList->size();i++)
    01052                 {
    01053                         cout <<"<div style=\"position:absolute;top:375;left:"<<i*20+150<<"\" >";
    01054                         if (vPlayer->isPlaying==true)
    01055                         {
    01056                                 cout <<"<a  href=\"javascript:document.forms.cards.actionner.value='playcard';document.forms.cards.card.value='"<<vPlayer->cardsList->at(i)<<"';document.forms.cards.submit();\">";
    01057                         }
    01058                         cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<vPlayer->cardsList->at(i)<<".png\" alt=\"Carte ["<<i<<"]="<<vPlayer->cardsList->at(i)<<"\" />"<<endl;
    01059                         if (vPlayer->isPlaying)
    01060                         {
    01061                                 cout <<"</a>";
    01062                         }
    01063                         cout <<"</div>";
    01064                 }
    01065                 cout <<"</form>";
    01066         }
    01067
    01080         void playACard(datasplayer *vPlayer,datasgame *readedGame,string *card)
    01081         {
    01082
    01083
    01084                 for (unsigned int i=0;i<readedGame->playersList->size();i++)
    01085                 {
    01086
    01087
    01088                         if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
    01089                         {
    01090
    01091                                 //vPlayer->
    01092
    01093                                 for (unsigned int j=0;j<readedGame->playersList->at(i)->cardsList->size();j++)
    01094                                 {
    01095
    01096
    01097                                         if(readedGame->playersList->at(i)->cardsList->at(j).compare(*card)==0)
    01098                                         {
    01099                                                 if (readedGame->piocheCards==NULL)
    01100                                                 {
    01101                                                         readedGame->piocheCards=new vector<string>;
    01102                                                 }
    01103                                                 //string carde=*card;
    01104                                                 //readedGame->piocheCards->push(carde);
    01105                                                 readedGame->playedCards->push_back(*card);
    01106                                                 readedGame->playersList->at(i)->actualCard=*card;
    01107                                                 readedGame->playersList->at(i)->cardsList->erase(readedGame->playersList->at(i)->cardsList->begin()+j);
    01108
    01109                                                 break;
    01110
    01111                                         }
    01112                                 }
    01113
    01114                                 break;
    01115                         }
    01116                 }
    01117
    01118         }
    01119
    01129         void turnPlayers(datasplayer *vPlayer,datasgame *readedGame)
    01130         {
    01131                 unsigned int IdTurn=0;
    01132                 vPlayer->isPlaying=false;
    01133                 //Find the player
    01134                 for (unsigned int i=0;i<readedGame->playersList->size();i++)
    01135                 {
    01136
    01137                         if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
    01138                         {
    01139                                 readedGame->playersList->at(i)->isPlaying=false;
    01140                                 IdTurn=i;
    01141                                 break;
    01142                         }
    01143                 }
    01144                 IdTurn++;
    01145                 if (IdTurn==readedGame->playersList->size())
    01146                 {
    01147                         IdTurn=0;
    01148                 }
    01149
    01150                 readedGame->playersList->at(IdTurn)->isPlaying=true;
    01151
    01152         }
    01153
    01164         bool testCard(datasplayer *vPlayer,datasgame *readedGame,string *card)
    01165         {
    01166
    01167                 //If first Card in the Bloxk
    01168                 if (readedGame->playedCards->size()==0)
    01169                 {
    01170                         return true;
    01171                 }
    01172                 //take the color of the first Card
    01173                 string color=readedGame->playedCards->front().substr(0,1);
    01174                 //Test if it's the same color
    01175                 if (color.compare(card->substr(0,1))==0)
    01176                 {
    01177                         return true;
    01178                 }
    01179
    01180                 //Test if the player has a good colored card in his game
    01181                 vPlayer->isPlaying=false;
    01182                 //Find the player
    01183                 for (unsigned int i=0;i<readedGame->playersList->size();i++)
    01184                 {
    01185
    01186                         if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
    01187                         {
    01188
    01189                                 for (unsigned int j=0;j<readedGame->playersList->at(i)->cardsList->size();j++)
    01190                                 {
    01191                                         if (color.compare(readedGame->playersList->at(i)->cardsList->at(j).substr(0,1))==0)
    01192                                                 return false;
    01193                                 }
    01194                                 return true;
    01195
    01196                         }
    01197                 }
    01198
    01199                 return true;
    01200
    01201         }
    01202
    01215         void IAPlay(datasgame *readedGame,int pId)
    01216         {
    01217
    01218                 if (readedGame->playersList->at(pId)->cardsList->size()==0)
    01219                 {
    01220                         return;
    01221                 }
    01222                 //If first Card in the Block
    01223                 if (readedGame->playedCards->size()==0)
    01224                 {
    01225                         playACard(readedGame->playersList->at(pId),readedGame,&readedGame->playersList->at(pId)->cardsList->front());
    01226                         turnPlayers(readedGame->playersList->at(pId),readedGame);
    01227                         return ;
    01228                 }
    01229                 //If not take the color of the first Card
    01230                 string color=readedGame->playedCards->front().substr(0,1);
    01231
    01232                 std::sort(readedGame->playersList->at(pId)->cardsList->begin(),readedGame->playersList->at(pId)->cardsList->end());
    01233
    01234                 //color Finded
    01235                 bool vColorOk=false;
    01236                 int vId=0;
    01237                 for (unsigned int i=0;i<readedGame->playersList->at(pId)->cardsList->size();i++)
    01238                 {
    01239                         unsigned int actualColor=color.compare(readedGame->playersList->at(pId)->cardsList->at(i).substr(0,1));
    01240
    01241
    01242                         //If the next color is not the same the card is the max
    01243                         if (actualColor==0&&vColorOk==true)
    01244                         {
    01245
    01246                                 vId=i;
    01247                         }
    01248                         if (actualColor!=0&&vColorOk==true)
    01249                         {
    01250
    01251                                 break;
    01252                         }
    01253
    01254                         //If Color Finded
    01255                         if (actualColor==0&&vColorOk==false)
    01256                         {
    01257
    01258                                 vColorOk=true;
    01259                                 vId=i;
    01260                         }
    01261                 }
    01262
    01263                 playACard(readedGame->playersList->at(pId),readedGame,&readedGame->playersList->at(pId)->cardsList->at(vId));
    01264                 turnPlayers(readedGame->playersList->at(pId),readedGame);
    01265         }
    01266
    01275         void gameRules(datasplayer *vPlayer, string *action, string * card)
    01276         {
    01277
    01278                 //search the player in a game
    01279
    01280                 datasgame *readedGame=getGame(vPlayer->identifiant);
    01281
    01282                 if (readedGame==NULL)
    01283                 {
    01284
    01285                         //It' isn't in game
    01286                         //Count the number of games
    01287                         if (countGame()>MAX_GAME)
    01288                         {
    01289                                 cout <<"THE PLAY TABLES ARE FULL!"<<endl;
    01290                                 return;
    01291                         }
    01292                         //Create a new Game for Four Player
    01293                         readedGame=createGame(vPlayer);
    01294
    01295                 }
    01296
    01297                 unsigned int thePlayer=0;
    01298                 //Find the player
    01299                 for (unsigned int i=0;i<readedGame->playersList->size();i++)
    01300                 {
    01301
    01302                         if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
    01303                         {
    01304                                 //vPlayer->
    01305                                 vPlayer->cardsList=readedGame->playersList->at(i)->cardsList;
    01306                                 vPlayer->isPlaying=readedGame->playersList->at(i)->isPlaying;
    01307                                 vPlayer->points=readedGame->playersList->at(i)->points;
    01308                                 thePlayer=i;
    01309                                 break;
    01310
    01311                         }
    01312                 }
    01313
    01314
    01315                 //Gestion des actions de l'utilisateur
    01316                 int vResComp=action->compare("playcard");
    01317
    01318                 if ( vResComp==0 && vPlayer->isPlaying==true)
    01319                 {
    01320                         if (testCard(vPlayer,readedGame,card)==true)
    01321                         {
    01322                                 playACard(vPlayer,readedGame,card);
    01323                                 turnPlayers(vPlayer,readedGame);
    01324                                 writeGame(vPlayer,readedGame);
    01325                         }
    01326                         else
    01327                         {
    01328                                 cout <<"<div style=\"position:absolute;top:50;left:"<<150<<"\" >";
    01329                                 cout <<"<b>You can not Play this card!</b><br>\n";
    01330                                 cout <<"</div>";
    01331                         }
    01332
    01333                 }
    01334
    01335                 //Tant que l'utilisateur n'a pas encore le droit de jouer on fait tourner l'IA
    01336                 unsigned int vNbTurns=0;
    01337                 while (vNbTurns<=readedGame->playersList->size()+1&&readedGame->playersList->at(thePlayer)->isPlaying==false)
    01338                 {
    01339                         vNbTurns++;
    01340                         for (unsigned int i=0;i<readedGame->playersList->size();i++)
    01341                         {
    01342                                 if (readedGame->playersList->size()<=readedGame->playedCards->size())
    01343                                 {
    01344
    01345                                         break;
    01346                                 }
    01347                                 if (i==thePlayer)
    01348                                 {
    01349                                         if (readedGame->playersList->at(thePlayer)->isPlaying==true)
    01350                                         {
    01351
    01352                                                 break;
    01353                                         }
    01354                                 }
    01355                                 else
    01356                                 {
    01357                                         if (readedGame->playersList->at(i)->isPlaying==true)
    01358                                         {
    01359
    01360                                                 IAPlay(readedGame,i);
    01361
    01362                                         }
    01363                                 }
    01364                         }
    01365
    01366                 }
    01367
    01368                 vector <string> *lastPlayedCard= new vector<string>;
    01369                 //A end is finish??
    01370                 if (readedGame->playersList->size()<=readedGame->playedCards->size())
    01371                 {
    01372                         //Find the winner
    01373                         //The Winner is
    01374                         int vWiner=0;
    01375                         int theMax=0;
    01376                         int total=0;
    01377                         for(unsigned int i=0;i<readedGame->playersList->size();i++)
    01378                         {
    01379                                 string plCard=readedGame->playersList->at(i)->actualCard;
    01380                                 readedGame->playersList->at(i)->isPlaying=false;
    01381                                 int cardValue=calculateCard(plCard);
    01382                                 readedGame->playersList->at(i)->actualCard="";
    01383                                 int compCard=plCard.substr(0,1).compare(readedGame->playedCards->front().substr(0,1));
    01384
    01385
    01386                                 if (theMax<cardValue&&compCard==0)
    01387                                 {
    01388                                         theMax=cardValue;
    01389                                         vWiner=i;
    01390
    01391                                 }
    01392
    01393                                 if (compCard==0)
    01394                                 {
    01395                                         total+=cardValue;
    01396                                 }
    01397                         }
    01398
    01399                         readedGame->playersList->at(vWiner)->isPlaying=true;
    01400                         readedGame->playersList->at(vWiner)->points+=total;
    01401
    01402                         //clear and add In Pioche
    01403
    01404                         while (!readedGame->playedCards->empty())
    01405                         {
    01406                                 readedGame->piocheCards->push_back(readedGame->playedCards->front());
    01407                                 lastPlayedCard->push_back(readedGame->playedCards->front());
    01408                                 readedGame->playedCards->erase(readedGame->playedCards->begin());
    01409                         }
    01410                 }
    01411                 if (readedGame->playersList->at(thePlayer)->isPlaying==true)
    01412                 {
    01413                         vPlayer->isPlaying=true;
    01414                 }
    01415                 if (readedGame->playersList->at(thePlayer)->cardsList->size()!=0)
    01416                 {
    01417                 //Tant que l'utilisateur n'a pas encore le droit de jouer on fait tourner l'IA
    01418                 vNbTurns=0;
    01419
    01420                 while (readedGame->playersList->at(thePlayer)->isPlaying==false)
    01421                 {
    01422                         vNbTurns++;
    01423                         for (unsigned int i=0;i<readedGame->playersList->size();i++)
    01424                         {
    01425                                 if (i==thePlayer)
    01426                                 {
    01427                                         if (readedGame->playersList->at(thePlayer)->isPlaying==true)
    01428
    01429                                         break;
    01430                                 }
    01431                                 else
    01432                                 {
    01433                                         if (readedGame->playersList->at(i)->isPlaying==true)
    01434                                         {
    01435                                                 IAPlay(readedGame,i);
    01436                                         }
    01437                                 }
    01438                         }
    01439
    01440                 }
    01441                 }
    01442                 if (readedGame->playersList->at(thePlayer)->isPlaying==true)
    01443                 {
    01444                         vPlayer->isPlaying=true;
    01445                 }
    01446                 try{
    01447                 writeGame(vPlayer,readedGame);
    01448                 if (readedGame->playersList->at(thePlayer)->cardsList->size()!=0)
    01449                 {
    01450                         drawCards(lastPlayedCard);
    01451                 }
    01452                 else
    01453                 {
    01454                         writeWinner(vPlayer,readedGame);
    01455                 }
    01456                 drawPlayers(readedGame);
    01457                 //drawCardInPlay(readedGame);
    01458                 drawPlayerCards(vPlayer);
    01459                 drawInfos(vPlayer);
    01460                 }
    01461                 catch(std::exception &error)
    01462                 {
    01463                         cout <<"Erreur:"<<error.what()<<"<br>\n";
    01464                 }
    01465
    01466         }
    01467 }
    01468
    01469 using namespace CardGameTools;
    01470
    01478 int
    01479 main(int argc,
    01480      char **argv)
    01481 {
    01482    try {
    01483       Cgicc cgi;
    01484
    01485        // Get the name and value of the cookie to set
    01486        const_form_iterator name = cgi.getElement("name");
    01487
    01488        const_form_iterator value = cgi.getElement("value");
    01489
    01490        const_form_iterator actionIn = cgi.getElement("actionner");
    01491        const_form_iterator playedCard = cgi.getElement("card");
    01492        string action;
    01493        string card;
    01494
    01495
    01496        if (actionIn!= cgi.getElements().end() &&actionIn->getValue().empty() == false)
    01497        {
    01498                 action=actionIn->getValue();
    01499
    01500        }
    01501        if (playedCard!= cgi.getElements().end() &&playedCard->getValue().empty() == false)
    01502        {
    01503                 card=playedCard->getValue();
    01504
    01505        }
    01506         string staticSession="";
    01507
    01508       //get a static session
    01509         if (argc>1)
    01510         {
    01511
    01512                 staticSession =argv[1];
    01513
    01514         }
    01515
    01516
    01517       // Send HTTP header
    01518
    01519       string vRet=getNUMCookie(cgi.getEnvironment().getCookieList());
    01520
    01521
    01522         if (vRet.compare("")==0&&staticSession.compare("")!=0)
    01523         {
    01524
    01525                 vRet=staticSession;
    01526         }
    01527
    01528         if (vRet.compare("")==0&&getValue(vRet).compare("")==0)
    01529         {
    01530
    01531                 vRet=generateUnicCookie();
    01532
    01533                 cout << HTTPHTMLHeader()
    01534         .setCookie(HTTPCookie(COOKIE_NAME, vRet));
    01535         }
    01536     else
    01537       cout << HTTPHTMLHeader();      // Set up the HTML document
    01538
    01539       cout << html() << head(title("Cgicc CardGame example")) << endl;
    01540       cout << body() << endl;
    01541
    01542       cout <<"<H1>Card Game</H1>";
    01543       cout <<"<div style=\"position:absolute;top:5;left:"<<250<<"\"><form name=\"start\"><input type=\"hidden\" name=\"actionner\" value=\"start\"><a href=\"javascript:document.forms.start.submit();\">Start a new Game</a></form></div>";
    01544     //if the are a cookie in the stock we parse data
    01545         datasplayer *vPlayer;
    01546         if (getValue(vRet).compare("")!=0)
    01547         {
    01548
    01549                 vPlayer=convertStringToStuct(getValue(vRet));
    01550
    01551         }else
    01552         {
    01553
    01554                 vPlayer= new datasplayer;
    01555                 srand ( time(NULL) );
    01556
    01557
    01558                 stringstream buffer;
    01559                 buffer << "P"<<(rand()%1000)+1<<"_"<<(rand()%1000)+1;
    01560                 vPlayer->identifiant=buffer.str();
    01561
    01562                 //We write a new empty value
    01563
    01564                 writeValue(vRet,convertStructToString(vPlayer));
    01565
    01566         }
    01567         if (action.compare("start")==0)
    01568         {
    01569                 writeFileGame(vPlayer->identifiant,"");
    01570         }
    01571
    01572         gameRules(vPlayer,&action,&card);
    01573
    01574       // Close the HTML document
    01575       cout << body() << html();
    01576
    01577    }
    01578    catch(exception& e) {
    01579       // handle any errors - omitted for brevity
    01580    }
    01581 }
    01582
    01583