cardgame.cpp

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

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 2004 Stephen F. Booth
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front Cover Texts, and with no Back-Cover Texts.
Documentation generated Mon Feb 10 2020 13:57:40 for cgicc by doxygen 1.7.3