cardgame.cpp
Go to the documentation of this file.
1 /* -*-mode:c++; c-file-style: "gnu";-*- */
2 /*
3  * $Id: cardgame.cpp,v 1.7 2009/01/03 17:26:43 sebdiaz Exp $
4  *
5  * Copyright (C) 2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
6  * Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 3 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21  */
22 
30 #include <iostream>
31 #include <vector>
32 #include <iterator>
33 #include <string>
34 #include <cstdlib>
35 #include <ctime>
36 #include <sstream>
37 #include <fstream>
38 #include <queue>
39 #include <algorithm>
40 #include "cgicc/CgiDefs.h"
41 #include "cgicc/Cgicc.h"
42 #include "cgicc/HTTPHTMLHeader.h"
43 #include "cgicc/HTMLClasses.h"
44 #include "cgicc/HTTPCookie.h"
45 #define COOKIE_NAME "ELPIELOJUEGO"
46 #define COOKIE_FILE_NAME "sessions.tmp"
47 #define GAME_FILE_NAME "games.tmp"
48 #define MAX_GAME 10
49 
50 using namespace std;
51 using namespace cgicc;
52 
53 
54 
62 {
67  vector <string > *cardsList;
68 
72  string identifiant;
73 
77  bool isPlaying;
78 
82  int points;
83 
87  string actualCard;
88 };
89 
90 
96 struct datasgame
97 {
102  vector <datasplayer *> *playersList;
103 
107  vector<string> *playedCards;
108 
112  vector<string> *piocheCards;
113 
114 };
115 
120 namespace CardGameTools
121 {
135  {
136 
137  std::vector<string>::iterator itVectorData;
138  stringstream buffer;
139  buffer <<pPlayer->identifiant<<"::";
140  for(itVectorData = pPlayer->cardsList->begin(); itVectorData != pPlayer->cardsList->end(); itVectorData++)
141  {
142 
143  buffer << *itVectorData;
144  if (itVectorData != pPlayer->cardsList->end())
145  {
146  buffer <<"||";
147 
148  }
149  }
150 
151  //Global Separator
152 
153  return buffer.str();
154  }
155 
169  {
170  datasplayer *datas= new datasplayer;
171  char *carList=(char *)pPlayer.c_str();
172  int wordCounter=0;
173  string word;
174  stringstream actualWord;
175  for (unsigned int i=0;i<pPlayer.size();i++)
176  {
177  if (i+1<pPlayer.size())
178  if (carList[i]==':'&&carList[i+1]==':')
179  {
180  word=actualWord.str();
181  //first , the player name
182  if (wordCounter==0)
183  {
184  //first is no data word
185  }
186  else
187  if (wordCounter==1)
188  {
189  datas->identifiant=word;
190  }else
191  {
192  datas->cardsList->push_back(word);
193  }
194  wordCounter++;
195  actualWord.clear();
196  i++;
197  }else {
198  actualWord <<carList[i];
199  }
200  }
201 
202 
203  return datas;
204  }
205 
214  string getNUMCookie(std::vector< HTTPCookie > pCookieVector)
215  {
216 
217  if (pCookieVector.size()== 0)
218  {
219 
220  return "";
221  }
222  std::vector<HTTPCookie>::iterator itVectorData;
223  for(itVectorData = pCookieVector.begin(); itVectorData != pCookieVector.end(); itVectorData++)
224  {
225 
226  HTTPCookie theCookie = *(itVectorData);
227 
228  if (theCookie.getName ().compare(COOKIE_NAME)==0)
229  {
230  return theCookie.getValue ();
231  }
232  }
233 
234 
235  return "";
236  }
237 
245  string getValue(string pName)
246  {
247 
248  ifstream inFile;
249  inFile.open(COOKIE_FILE_NAME);
250  if (!inFile) {
251  ofstream outFile(COOKIE_FILE_NAME, ios::out);
252  outFile.put('\n');
253  outFile.close();
254  inFile.open(COOKIE_FILE_NAME);
255  }
256 
257  // Lecture ligne a ligne
258  while (inFile&&!inFile.eof ())
259  {
260  char ligne[32000];
261  std::string s;
262 
263  inFile.getline (ligne, sizeof (ligne));
264  s = ligne;
265 
266  if (s.find (pName)!= string::npos)
267  {
268 
269  s.replace(0,pName.size(),"");
270  inFile.close();
271  return s;
272  }
273  }
274  inFile.close();
275  return "";
276 
277  }
278 
286  string getFileGame(string pName)
287  {
288 
289  ifstream inFile;
290  inFile.open(GAME_FILE_NAME);
291  if (!inFile) {
292  ofstream outFile(GAME_FILE_NAME, ios::out);
293  outFile.put('\n');
294  outFile.close();
295  inFile.open(GAME_FILE_NAME);
296  }
297  // Lecture ligne a ligne
298  while (inFile&&!inFile.eof ())
299  {
300  char ligne[32000];
301  std::string s;
302 
303  inFile.getline (ligne, sizeof (ligne));
304  s = ligne;
305 
306  if (s.find (pName)!= string::npos)
307  {
308 
309  inFile.close();
310  return s;
311  }
312  }
313  inFile.close();
314  return "";
315 
316  }
317 
324  datasgame *getGame(string pName)
325  {
326 
327  datasgame *dgame= new datasgame;
328  dgame->playersList=new vector<datasplayer*>;
329 
330 
331  string vGame=getFileGame(pName);
332  if (vGame.compare("")==0)
333  {
334  return NULL;
335  }
336 
337 
338 
339  char *carList=(char *)vGame.c_str();
340  int wordCounter=0;
341  string word;
342  stringstream actualWord;
343  int vNBPLayers=0;
344  int playerCounter=0;
345  int playerCounterElement=0;
346  int vNBCards=0;
347  int vCardsCounter=0;
348  vector <string > *cardsList= new vector<string>;
349  string identifiant;
350  string actualCard;
351  bool isPlaying=false;
352  int points;
353 
354  int vNBCardsQueue1=0;
355  int vCardsCounterQ1=0;
356  int vNBCardsQueue2=0;
357  int vCardsCounterQ2=0;
358  bool vCountedCardsQ1;
359  bool vCountedCardsQ2;
360  vCountedCardsQ1=false;
361  vCountedCardsQ2=false;
362  vector <string > *queue1= new vector<string>;
363  dgame->playedCards=queue1;
364 
365  vector <string > *queue2= new vector<string>;
366  dgame->piocheCards=queue2;
367 
368  for (unsigned int i=0;i<vGame.size();i++)
369  {
370  if (i+1<vGame.size())
371  if (carList[i]==':'&&carList[i+1]==':')
372  {
373 
374  word=actualWord.str();
375 
376 
377 
378  //first , NB Players Value
379  if (wordCounter==0)
380  {
381 
382  vNBPLayers=atoi(word.c_str());
383  }else
384  {
385  //Add of a player
386  if (playerCounter<vNBPLayers)
387  {
388 
389  //In first the name
390  if (playerCounterElement==0)
391  {
392 
393  identifiant=word;
394  }
395  if (playerCounterElement==1)
396  {
397 
398  isPlaying=(word.compare("1")==0)?true:false;
399  }
400  if (playerCounterElement==2)
401  {
402 
403  points=atoi(word.c_str());
404  }
405  if (playerCounterElement==3)
406  {
407 
408  actualCard=word;
409  }
410  if (playerCounterElement==4)
411  {
412 
413  vNBCards=atoi(word.c_str());
414  }
415  if (playerCounterElement>=5&&vCardsCounter<vNBCards)
416  {
417 
418  cardsList->push_back(word);
419  vCardsCounter++;
420  }
421  if (vCardsCounter==vNBCards&&playerCounterElement>=5)
422  {
423 
424  datasplayer *vPlay= new datasplayer;
425 
426  vPlay->identifiant=identifiant;
427  vPlay->points=points;
428  vPlay->isPlaying=isPlaying;
429  vPlay->cardsList=cardsList;
430  vPlay->actualCard=actualCard;
431  dgame->playersList->push_back(vPlay );
432  playerCounter++;
433  vCardsCounter=0;
434  playerCounterElement=0;
435 
436  cardsList=new vector <string >;
437 
438 
439  }else{playerCounterElement++; }
440 
441 
442  }
443  else
444  {//Saved queue
445 
446  if (vNBCardsQueue1==0&&!vCountedCardsQ1)
447  {
448  vNBCardsQueue1=atoi(word.c_str());
449  vCountedCardsQ1=true;
450 
451 
452  }else
453  {
454 
455  if (vCardsCounterQ1<vNBCardsQueue1)
456  {
457 
458  queue1->push_back(word);
459 
460  vCardsCounterQ1++;
461  }else
462  if (!vCountedCardsQ2&&vNBCardsQueue2==0&&vCardsCounterQ1==vNBCardsQueue1)
463  {
464 
465  vNBCardsQueue2=atoi(word.c_str());
466  vCountedCardsQ2=true;
467  //dgame->playedCards=queue1;
468 
469 
470  }else
471  if (vCardsCounterQ2<vNBCardsQueue2)
472  {
473 
474  queue2->push_back(word);
475 
476  vCardsCounterQ2++;
477  }
478  if (vCardsCounterQ2==vNBCardsQueue2&&vCardsCounterQ2!=0)
479  {
480 
481  //dgame->piocheCards=queue2;
482 
483  }
484 
485  }
486  }
487  }
488 
489 
490  wordCounter++;
491  actualWord.str("");
492  actualWord.clear();
493  actualWord.flush();
494 
495 
496  i++;
497  }else {
498 
499  actualWord <<carList[i];
500  }
501  }
502  return dgame;
503 
504  }
505 
512  void writeValue(string pName,string pValue)
513  {
514 
515  ofstream outFile(COOKIE_FILE_NAME, ios::out|ios::app);
516 
517  outFile << pName<<"::"<<pValue<<"\n";
518 
519  outFile.close();
520 
521  }
522 
529  void writeFileGame(string pName,string pValue)
530  {
531 
532  ifstream inFile;
533 
534  //Find Index of the game
535  inFile.open(GAME_FILE_NAME,ios::in);
536  if (!inFile) {
537 
538  ofstream outFile(GAME_FILE_NAME, ios::out|ios::app);
539 
540 
541  outFile << "\n";
542 
543  outFile.close();
544  inFile.open(GAME_FILE_NAME,ios::in);
545  }
546  //read of the file
547  if (inFile) {
548  stringstream buffer;
549  bool haveWrited;
550  haveWrited=false;
551  while (inFile&&!inFile.eof ())
552  {
553  char ligne[32000];
554  std::string s;
555  inFile.getline (ligne, sizeof (ligne));
556  s = ligne;
557 
558  if (s.find (pName)!= string::npos)
559  {
560 
561  buffer << pValue;
562 
563  haveWrited=true;
564 
565  }
566  else
567  {
568  buffer << s ;
569  }
570 
571  }
572  inFile.close();
573  ofstream outFile(GAME_FILE_NAME, ios::out|ios::trunc);
574 
575 
576  outFile << buffer.str();
577  if (!haveWrited)
578  {
579  outFile << pValue<<"\n";
580  }
581 
582  outFile.close();
583 
584 
585  }
586 
587 
588 
589  }
590 
591 
598  void writeGame(datasplayer *pPlayer,datasgame *pGame)
599  {
600 
601  stringstream buffer;
602 
603  datasplayer * itVectorData;
604  buffer <<pGame->playersList->size()<<"::";
605  for(unsigned int i=0;i<pGame->playersList->size(); i++)
606  {
607 
608  itVectorData=pGame->playersList->at(i);
609 
610 
611  std::vector<string>::iterator itVectorData2;
612  buffer <<itVectorData->identifiant<<"::"<<((itVectorData->isPlaying)?"1":"0")<<"::"<<itVectorData->points<<"::"<<itVectorData->actualCard<<"::";
613  //NBCards
614  buffer <<itVectorData->cardsList->size()<<"::";
615  for(itVectorData2 = itVectorData->cardsList->begin(); itVectorData2 != itVectorData->cardsList->end(); itVectorData2++)
616  {
617  buffer <<*itVectorData2<<"::";
618 
619  }
620 
621  }
622 
623  //NBCards played
624  buffer <<pGame->playedCards->size()<<"::";
625  for (unsigned int i=0;i<pGame->playedCards->size();i++)
626  {
627  buffer <<pGame->playedCards->at(i)<<"::";
628 
629  }
630 
631 
632 
633  //NBCards in queue
634 
635  buffer <<pGame->piocheCards->size()<<"::";
636 
637  for (unsigned int i=0;i<pGame->piocheCards->size();i++)
638  {
639  buffer <<pGame->piocheCards->at(i)<<"::";
640 
641 
642  }
643 
644 
645 
646  writeFileGame(pPlayer->identifiant,buffer.str());
647 
648  }
649 
656  {
657 
658  srand ( time(NULL) );
659  int nb_aleatoire;
660  nb_aleatoire=(rand()%100)+1;
661 
662 
663  stringstream buffer;
664  buffer << nb_aleatoire<<"_"<<time(NULL);
665 
666  string vNum=buffer.str() ;
667 
668  return vNum;
669  }
670 
676  int countGame()
677  {
678 
679  ifstream inFile;
680  inFile.open(GAME_FILE_NAME);
681  if (!inFile) {
682  return 0;
683  }
684  int vNBLigne=0;
685 
686  // Lecture ligne a ligne
687  while (inFile&&!inFile.eof ())
688  {
689 
690  string tempo;
691  inFile >> tempo;
692 
693  vNBLigne++;
694  }
695 
696  inFile.close();
697 
698 
699  return vNBLigne;
700 
701  }
702 
708  vector<string> *loadAndMixCards()
709  {
710  vector<string> UnorderedPioche;
711  UnorderedPioche.push_back("TA");
712  UnorderedPioche.push_back("TZ");
713  UnorderedPioche.push_back("T2");
714  UnorderedPioche.push_back("T3");
715  UnorderedPioche.push_back("T4");
716  UnorderedPioche.push_back("T5");
717  UnorderedPioche.push_back("T6");
718  UnorderedPioche.push_back("T7");
719  UnorderedPioche.push_back("T8");
720  UnorderedPioche.push_back("T9");
721  UnorderedPioche.push_back("TD");
722  UnorderedPioche.push_back("TR");
723  UnorderedPioche.push_back("TB");
724  UnorderedPioche.push_back("CA");
725  UnorderedPioche.push_back("CZ");
726  UnorderedPioche.push_back("C2");
727  UnorderedPioche.push_back("C3");
728  UnorderedPioche.push_back("C4");
729  UnorderedPioche.push_back("C5");
730  UnorderedPioche.push_back("C6");
731  UnorderedPioche.push_back("C7");
732  UnorderedPioche.push_back("C8");
733  UnorderedPioche.push_back("C9");
734  UnorderedPioche.push_back("CD");
735  UnorderedPioche.push_back("CR");
736  UnorderedPioche.push_back("CB");
737  UnorderedPioche.push_back("PA");
738  UnorderedPioche.push_back("PZ");
739  UnorderedPioche.push_back("P2");
740  UnorderedPioche.push_back("P3");
741  UnorderedPioche.push_back("P4");
742  UnorderedPioche.push_back("P5");
743  UnorderedPioche.push_back("P6");
744  UnorderedPioche.push_back("P7");
745  UnorderedPioche.push_back("P8");
746  UnorderedPioche.push_back("P9");
747  UnorderedPioche.push_back("PD");
748  UnorderedPioche.push_back("PR");
749  UnorderedPioche.push_back("PB");
750  UnorderedPioche.push_back("HA");
751  UnorderedPioche.push_back("HZ");
752  UnorderedPioche.push_back("H2");
753  UnorderedPioche.push_back("H3");
754  UnorderedPioche.push_back("H4");
755  UnorderedPioche.push_back("H5");
756  UnorderedPioche.push_back("H6");
757  UnorderedPioche.push_back("H7");
758  UnorderedPioche.push_back("H8");
759  UnorderedPioche.push_back("H9");
760  UnorderedPioche.push_back("HD");
761  UnorderedPioche.push_back("HR");
762  UnorderedPioche.push_back("HB");
763  srand ( time(NULL) );
764  int nb_aleatoire;
765 
766  vector<string> *vRet= new vector<string>;
767  while(UnorderedPioche.size()>0)
768  {
769 
770  nb_aleatoire=(rand()%UnorderedPioche.size());
771  vRet->push_back(UnorderedPioche[nb_aleatoire]);
772  UnorderedPioche.erase((UnorderedPioche.begin())+nb_aleatoire);
773  }
774  return vRet;
775 
776  }
777 
784  int calculateCard(string pCard)
785  {
786 
787  if (pCard.compare("TA")==0) return 10;
788  if (pCard.compare("TZ")==0) return 14;
789  if (pCard.compare("T2")==0) return 2;
790  if (pCard.compare("T3")==0) return 3;
791  if (pCard.compare("T4")==0) return 4;
792  if (pCard.compare("T5")==0) return 5;
793  if (pCard.compare("T6")==0) return 6;
794  if (pCard.compare("T7")==0) return 7;
795  if (pCard.compare("T8")==0) return 8;
796  if (pCard.compare("T9")==0) return 9;
797  if (pCard.compare("TD")==0) return 12;
798  if (pCard.compare("TR")==0) return 13;
799  if (pCard.compare("TB")==0) return 11;
800  if (pCard.compare("CA")==0) return 10;
801  if (pCard.compare("CZ")==0) return 14;
802  if (pCard.compare("C2")==0) return 2;
803  if (pCard.compare("C3")==0) return 3;
804  if (pCard.compare("C4")==0) return 4;
805  if (pCard.compare("C5")==0) return 5;
806  if (pCard.compare("C6")==0) return 6;
807  if (pCard.compare("C7")==0) return 7;
808  if (pCard.compare("C8")==0) return 8;
809  if (pCard.compare("C9")==0) return 9;
810  if (pCard.compare("CD")==0) return 12;
811  if (pCard.compare("CR")==0) return 13;
812  if (pCard.compare("CB")==0) return 11;
813  if (pCard.compare("PA")==0) return 10;
814  if (pCard.compare("PZ")==0) return 14;
815  if (pCard.compare("P2")==0) return 2;
816  if (pCard.compare("P3")==0) return 3;
817  if (pCard.compare("P4")==0) return 4;
818  if (pCard.compare("P5")==0) return 5;
819  if (pCard.compare("P6")==0) return 6;
820  if (pCard.compare("P7")==0) return 7;
821  if (pCard.compare("P8")==0) return 8;
822  if (pCard.compare("P9")==0) return 9;
823  if (pCard.compare("PD")==0) return 12;
824  if (pCard.compare("PR")==0) return 13;
825  if (pCard.compare("PB")==0) return 11;
826  if (pCard.compare("HA")==0) return 10;
827  if (pCard.compare("HZ")==0) return 14;
828  if (pCard.compare("H2")==0) return 2;
829  if (pCard.compare("H3")==0) return 3;
830  if (pCard.compare("H4")==0) return 4;
831  if (pCard.compare("H5")==0) return 5;
832  if (pCard.compare("H6")==0) return 6;
833  if (pCard.compare("H7")==0) return 7;
834  if (pCard.compare("H8")==0) return 8;
835  if (pCard.compare("H9")==0) return 9;
836  if (pCard.compare("HD")==0) return 12;
837  if (pCard.compare("HR")==0) return 13;
838  if (pCard.compare("HB")==0) return 11;
839  return 0;
840 
841  }
842 
852  {
853 
854  datasplayer *p1=new datasplayer;
855  datasplayer *p2=new datasplayer;
856  datasplayer *p3=new datasplayer;
857  datasplayer *p4=new datasplayer;
858  p1->identifiant=vPlayer->identifiant;
859  p1->actualCard="";
860  p1->isPlaying=true;
861  p1->points=0;
862  p2->identifiant="FREE1";
863  p2->actualCard="";
864  p2->isPlaying=false;
865  p2->points=0;
866  p3->identifiant="FREE2";
867  p3->actualCard="";
868  p3->isPlaying=false;
869  p3->points=0;
870  p4->identifiant="FREE3";
871  p4->actualCard="";
872  p4->isPlaying=false;
873  p4->points=0;
874 
875  datasgame *myGame= new datasgame;
876  myGame->playersList=new vector <datasplayer *>;
877  myGame->playersList->push_back(p1);
878  myGame->playersList->push_back(p2);
879  myGame->playersList->push_back(p3);
880  myGame->playersList->push_back(p4);
881  myGame->playedCards=new vector<string>;
882 
883  myGame->piocheCards=loadAndMixCards();
884 
885  int vNbCards=myGame->piocheCards->size();
886  //distribution des cartes
887  for (unsigned int i=0;i<myGame->playersList->size();i++)
888  {
889 
890  myGame->playersList->at(i)->cardsList=new vector<string>;
891  for (unsigned int j=0;j<vNbCards/myGame->playersList->size();j++)
892  {
893 
894  myGame->playersList->at(i)->cardsList->push_back(myGame->piocheCards->front());
895  myGame->piocheCards->erase(myGame->piocheCards->begin());
896  }
897 
898  }
899 
900 
901  writeGame(p1,myGame);
902  return myGame;
903  }
904 
910  void drawCards(vector <string> *cardList)
911  {
912 
913 
914  for (unsigned int i=0;i<cardList->size();i++)
915  {
916  cout <<"<div style=\"position:absolute;top:50;left:"<<i*150+150<<"\" >";
917  cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<cardList->at(i)<<".png\" alt=\"Carte ["<<i<<"]="<<cardList->at(i)<<"\" />"<<endl;
918  cout <<"</div>";
919  }
920  cout <<"<br />";
921  }
922 
931  void writeWinner(datasplayer *vPlayer,datasgame *pGame)
932  {
933  unsigned int winner=0;
934  int scoreOfTheWinner=0;
935  unsigned int playerId=0;
936  for (unsigned int i=0;i<pGame->playersList->size();i++)
937  {
938  if (vPlayer->identifiant.compare(pGame->playersList->at(i)->identifiant)==0)
939  {
940  playerId=i;
941  }
942  if (scoreOfTheWinner<pGame->playersList->at(i)->points)
943  {
944  winner=i;
945  scoreOfTheWinner=pGame->playersList->at(i)->points;
946  }
947  }
948 
949  cout <<"<div style=\"position:absolute;top:50;left:"<<150<<"\" >";
950  if (playerId==winner)
951  {
952  cout <<"<h2>YOU WIN !</h2>"<<endl;
953  cout <<"<b>Your score is : "<<scoreOfTheWinner<<"</b>"<<endl;
954  }else
955  {
956  cout <<"<h2>YOU LOSS !</h2>"<<endl;
957  cout <<"<b>The winner is : "<<pGame->playersList->at(winner)->identifiant<<"</b>"<<endl;
958  cout <<"<b>The score is : "<<scoreOfTheWinner<<"</b>"<<endl;
959  }
960  cout <<"</div>";
961 
962  cout <<"<br />";
963  }
964 
971  void drawInfos(datasplayer *vPlayer)
972  {
973  cout <<"<div style=\"position:absolute;width:140;top:50;left:"<<0<<"\" >";
974  cout <<"The latest Played Cards ";
975  cout <<"</div>";
976  cout <<"<div style=\"width:140;position:absolute;top:180;left:"<<0<<"\" >";
977  cout <<"Actual Cards in the Game<br>You are the player :"<<vPlayer->identifiant;
978  cout <<"</div>";
979  cout <<"<div style=\"width:140;position:absolute;top:375;left:"<<0<<"\" >";
980  cout <<"Your Cards, you can choose one card.";
981  cout <<"</div>";
982  }
983 
991  void drawPlayers(datasgame *pGame)
992  {
993 
994  bool vFirst=false;
995  for (unsigned int i=0;i<pGame->playersList->size();i++)
996  {
997  bool afficheFirst=false;
998  if (vFirst==false&&pGame->playersList->at(i)->actualCard.compare("")!=0)
999  {
1000  vFirst=true;
1001  afficheFirst=true;
1002  }
1003  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<<"\" >";
1004  cout <<"Name :"<<pGame->playersList->at(i)->identifiant<<"<br>";
1005  cout <<"Score :"<<pGame->playersList->at(i)->points<<"<br>";
1006 
1007  cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<pGame->playersList->at(i)->actualCard<<".png\" alt=\" \" />"<<endl;
1008  if (afficheFirst==true)
1009  {
1010  cout <<"<br>The color to play";
1011  }
1012  cout <<"</div>";
1013  }
1014  cout <<"<br />";
1015  }
1016 
1025  {
1026 
1027 
1028  for (unsigned int i=0;i<pGame->playedCards->size();i++)
1029  {
1030  cout <<"<div style=\"position:absolute;top:100;left:"<<i*110<<"\" >";
1031  cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<pGame->playedCards->at(i)<<".png\" alt=\"Carte ["<<i<<"]="<<pGame->playedCards->at(i)<<"\" />"<<endl;
1032  cout <<"</div>";
1033  }
1034  cout <<"<br />";
1035  }
1036 
1045  {
1046  std::sort (vPlayer->cardsList->begin(),vPlayer->cardsList->end());
1047  cout <<"<form name=\"cards\">";
1048  cout <<"<input type=\"hidden\" name=\"actionner\" value=\"\">";
1049  cout <<"<input type=\"hidden\" name=\"card\" value=\"\">";
1050  //affiche les cartes du joueurs
1051  for (unsigned int i=0;i<vPlayer->cardsList->size();i++)
1052  {
1053  cout <<"<div style=\"position:absolute;top:375;left:"<<i*20+150<<"\" >";
1054  if (vPlayer->isPlaying==true)
1055  {
1056  cout <<"<a href=\"javascript:document.forms.cards.actionner.value='playcard';document.forms.cards.card.value='"<<vPlayer->cardsList->at(i)<<"';document.forms.cards.submit();\">";
1057  }
1058  cout <<"<img border=\"0\" width=\"100\" src=\"images/"<<vPlayer->cardsList->at(i)<<".png\" alt=\"Carte ["<<i<<"]="<<vPlayer->cardsList->at(i)<<"\" />"<<endl;
1059  if (vPlayer->isPlaying)
1060  {
1061  cout <<"</a>";
1062  }
1063  cout <<"</div>";
1064  }
1065  cout <<"</form>";
1066  }
1067 
1080  void playACard(datasplayer *vPlayer,datasgame *readedGame,string *card)
1081  {
1082 
1083 
1084  for (unsigned int i=0;i<readedGame->playersList->size();i++)
1085  {
1086 
1087 
1088  if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
1089  {
1090 
1091  //vPlayer->
1092 
1093  for (unsigned int j=0;j<readedGame->playersList->at(i)->cardsList->size();j++)
1094  {
1095 
1096 
1097  if(readedGame->playersList->at(i)->cardsList->at(j).compare(*card)==0)
1098  {
1099  if (readedGame->piocheCards==NULL)
1100  {
1101  readedGame->piocheCards=new vector<string>;
1102  }
1103  //string carde=*card;
1104  //readedGame->piocheCards->push(carde);
1105  readedGame->playedCards->push_back(*card);
1106  readedGame->playersList->at(i)->actualCard=*card;
1107  readedGame->playersList->at(i)->cardsList->erase(readedGame->playersList->at(i)->cardsList->begin()+j);
1108 
1109  break;
1110 
1111  }
1112  }
1113 
1114  break;
1115  }
1116  }
1117 
1118  }
1119 
1129  void turnPlayers(datasplayer *vPlayer,datasgame *readedGame)
1130  {
1131  unsigned int IdTurn=0;
1132  vPlayer->isPlaying=false;
1133  //Find the player
1134  for (unsigned int i=0;i<readedGame->playersList->size();i++)
1135  {
1136 
1137  if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
1138  {
1139  readedGame->playersList->at(i)->isPlaying=false;
1140  IdTurn=i;
1141  break;
1142  }
1143  }
1144  IdTurn++;
1145  if (IdTurn==readedGame->playersList->size())
1146  {
1147  IdTurn=0;
1148  }
1149 
1150  readedGame->playersList->at(IdTurn)->isPlaying=true;
1151 
1152  }
1153 
1164  bool testCard(datasplayer *vPlayer,datasgame *readedGame,string *card)
1165  {
1166 
1167  //If first Card in the Bloxk
1168  if (readedGame->playedCards->size()==0)
1169  {
1170  return true;
1171  }
1172  //take the color of the first Card
1173  string color=readedGame->playedCards->front().substr(0,1);
1174  //Test if it's the same color
1175  if (color.compare(card->substr(0,1))==0)
1176  {
1177  return true;
1178  }
1179 
1180  //Test if the player has a good colored card in his game
1181  vPlayer->isPlaying=false;
1182  //Find the player
1183  for (unsigned int i=0;i<readedGame->playersList->size();i++)
1184  {
1185 
1186  if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
1187  {
1188 
1189  for (unsigned int j=0;j<readedGame->playersList->at(i)->cardsList->size();j++)
1190  {
1191  if (color.compare(readedGame->playersList->at(i)->cardsList->at(j).substr(0,1))==0)
1192  return false;
1193  }
1194  return true;
1195 
1196  }
1197  }
1198 
1199  return true;
1200 
1201  }
1202 
1215  void IAPlay(datasgame *readedGame,int pId)
1216  {
1217 
1218  if (readedGame->playersList->at(pId)->cardsList->size()==0)
1219  {
1220  return;
1221  }
1222  //If first Card in the Block
1223  if (readedGame->playedCards->size()==0)
1224  {
1225  playACard(readedGame->playersList->at(pId),readedGame,&readedGame->playersList->at(pId)->cardsList->front());
1226  turnPlayers(readedGame->playersList->at(pId),readedGame);
1227  return ;
1228  }
1229  //If not take the color of the first Card
1230  string color=readedGame->playedCards->front().substr(0,1);
1231 
1232  std::sort(readedGame->playersList->at(pId)->cardsList->begin(),readedGame->playersList->at(pId)->cardsList->end());
1233 
1234  //color Finded
1235  bool vColorOk=false;
1236  int vId=0;
1237  for (unsigned int i=0;i<readedGame->playersList->at(pId)->cardsList->size();i++)
1238  {
1239  unsigned int actualColor=color.compare(readedGame->playersList->at(pId)->cardsList->at(i).substr(0,1));
1240 
1241 
1242  //If the next color is not the same the card is the max
1243  if (actualColor==0&&vColorOk==true)
1244  {
1245 
1246  vId=i;
1247  }
1248  if (actualColor!=0&&vColorOk==true)
1249  {
1250 
1251  break;
1252  }
1253 
1254  //If Color Finded
1255  if (actualColor==0&&vColorOk==false)
1256  {
1257 
1258  vColorOk=true;
1259  vId=i;
1260  }
1261  }
1262 
1263  playACard(readedGame->playersList->at(pId),readedGame,&readedGame->playersList->at(pId)->cardsList->at(vId));
1264  turnPlayers(readedGame->playersList->at(pId),readedGame);
1265  }
1266 
1275  void gameRules(datasplayer *vPlayer, string *action, string * card)
1276  {
1277 
1278  //search the player in a game
1279 
1280  datasgame *readedGame=getGame(vPlayer->identifiant);
1281 
1282  if (readedGame==NULL)
1283  {
1284 
1285  //It' isn't in game
1286  //Count the number of games
1287  if (countGame()>MAX_GAME)
1288  {
1289  cout <<"THE PLAY TABLES ARE FULL!"<<endl;
1290  return;
1291  }
1292  //Create a new Game for Four Player
1293  readedGame=createGame(vPlayer);
1294 
1295  }
1296 
1297  unsigned int thePlayer=0;
1298  //Find the player
1299  for (unsigned int i=0;i<readedGame->playersList->size();i++)
1300  {
1301 
1302  if (readedGame->playersList->at(i)->identifiant.compare(vPlayer->identifiant)==0)
1303  {
1304  //vPlayer->
1305  vPlayer->cardsList=readedGame->playersList->at(i)->cardsList;
1306  vPlayer->isPlaying=readedGame->playersList->at(i)->isPlaying;
1307  vPlayer->points=readedGame->playersList->at(i)->points;
1308  thePlayer=i;
1309  break;
1310 
1311  }
1312  }
1313 
1314 
1315  //Gestion des actions de l'utilisateur
1316  int vResComp=action->compare("playcard");
1317 
1318  if ( vResComp==0 && vPlayer->isPlaying==true)
1319  {
1320  if (testCard(vPlayer,readedGame,card)==true)
1321  {
1322  playACard(vPlayer,readedGame,card);
1323  turnPlayers(vPlayer,readedGame);
1324  writeGame(vPlayer,readedGame);
1325  }
1326  else
1327  {
1328  cout <<"<div style=\"position:absolute;top:50;left:"<<150<<"\" >";
1329  cout <<"<b>You can not Play this card!</b><br>\n";
1330  cout <<"</div>";
1331  }
1332 
1333  }
1334 
1335  //Tant que l'utilisateur n'a pas encore le droit de jouer on fait tourner l'IA
1336  unsigned int vNbTurns=0;
1337  while (vNbTurns<=readedGame->playersList->size()+1&&readedGame->playersList->at(thePlayer)->isPlaying==false)
1338  {
1339  vNbTurns++;
1340  for (unsigned int i=0;i<readedGame->playersList->size();i++)
1341  {
1342  if (readedGame->playersList->size()<=readedGame->playedCards->size())
1343  {
1344 
1345  break;
1346  }
1347  if (i==thePlayer)
1348  {
1349  if (readedGame->playersList->at(thePlayer)->isPlaying==true)
1350  {
1351 
1352  break;
1353  }
1354  }
1355  else
1356  {
1357  if (readedGame->playersList->at(i)->isPlaying==true)
1358  {
1359 
1360  IAPlay(readedGame,i);
1361 
1362  }
1363  }
1364  }
1365 
1366  }
1367 
1368  vector <string> *lastPlayedCard= new vector<string>;
1369  //A end is finish??
1370  if (readedGame->playersList->size()<=readedGame->playedCards->size())
1371  {
1372  //Find the winner
1373  //The Winner is
1374  int vWiner=0;
1375  int theMax=0;
1376  int total=0;
1377  for(unsigned int i=0;i<readedGame->playersList->size();i++)
1378  {
1379  string plCard=readedGame->playersList->at(i)->actualCard;
1380  readedGame->playersList->at(i)->isPlaying=false;
1381  int cardValue=calculateCard(plCard);
1382  readedGame->playersList->at(i)->actualCard="";
1383  int compCard=plCard.substr(0,1).compare(readedGame->playedCards->front().substr(0,1));
1384 
1385 
1386  if (theMax<cardValue&&compCard==0)
1387  {
1388  theMax=cardValue;
1389  vWiner=i;
1390 
1391  }
1392 
1393  if (compCard==0)
1394  {
1395  total+=cardValue;
1396  }
1397  }
1398 
1399  readedGame->playersList->at(vWiner)->isPlaying=true;
1400  readedGame->playersList->at(vWiner)->points+=total;
1401 
1402  //clear and add In Pioche
1403 
1404  while (!readedGame->playedCards->empty())
1405  {
1406  readedGame->piocheCards->push_back(readedGame->playedCards->front());
1407  lastPlayedCard->push_back(readedGame->playedCards->front());
1408  readedGame->playedCards->erase(readedGame->playedCards->begin());
1409  }
1410  }
1411  if (readedGame->playersList->at(thePlayer)->isPlaying==true)
1412  {
1413  vPlayer->isPlaying=true;
1414  }
1415  if (readedGame->playersList->at(thePlayer)->cardsList->size()!=0)
1416  {
1417  //Tant que l'utilisateur n'a pas encore le droit de jouer on fait tourner l'IA
1418  vNbTurns=0;
1419 
1420  while (readedGame->playersList->at(thePlayer)->isPlaying==false)
1421  {
1422  vNbTurns++;
1423  for (unsigned int i=0;i<readedGame->playersList->size();i++)
1424  {
1425  if (i==thePlayer)
1426  {
1427  if (readedGame->playersList->at(thePlayer)->isPlaying==true)
1428 
1429  break;
1430  }
1431  else
1432  {
1433  if (readedGame->playersList->at(i)->isPlaying==true)
1434  {
1435  IAPlay(readedGame,i);
1436  }
1437  }
1438  }
1439 
1440  }
1441  }
1442  if (readedGame->playersList->at(thePlayer)->isPlaying==true)
1443  {
1444  vPlayer->isPlaying=true;
1445  }
1446  try{
1447  writeGame(vPlayer,readedGame);
1448  if (readedGame->playersList->at(thePlayer)->cardsList->size()!=0)
1449  {
1450  drawCards(lastPlayedCard);
1451  }
1452  else
1453  {
1454  writeWinner(vPlayer,readedGame);
1455  }
1456  drawPlayers(readedGame);
1457  //drawCardInPlay(readedGame);
1458  drawPlayerCards(vPlayer);
1459  drawInfos(vPlayer);
1460  }
1461  catch(std::exception &error)
1462  {
1463  cout <<"Erreur:"<<error.what()<<"<br>\n";
1464  }
1465 
1466  }
1467 }
1468 
1469 using namespace CardGameTools;
1470 
1478 int
1479 main(int argc,
1480  char **argv)
1481 {
1482  try {
1483  Cgicc cgi;
1484 
1485  // Get the name and value of the cookie to set
1486  const_form_iterator name = cgi.getElement("name");
1487 
1488  const_form_iterator value = cgi.getElement("value");
1489 
1490  const_form_iterator actionIn = cgi.getElement("actionner");
1491  const_form_iterator playedCard = cgi.getElement("card");
1492  string action;
1493  string card;
1494 
1495 
1496  if (actionIn!= cgi.getElements().end() &&actionIn->getValue().empty() == false)
1497  {
1498  action=actionIn->getValue();
1499 
1500  }
1501  if (playedCard!= cgi.getElements().end() &&playedCard->getValue().empty() == false)
1502  {
1503  card=playedCard->getValue();
1504 
1505  }
1506  string staticSession="";
1507 
1508  //get a static session
1509  if (argc>1)
1510  {
1511 
1512  staticSession =argv[1];
1513 
1514  }
1515 
1516 
1517  // Send HTTP header
1518 
1519  string vRet=getNUMCookie(cgi.getEnvironment().getCookieList());
1520 
1521 
1522  if (vRet.compare("")==0&&staticSession.compare("")!=0)
1523  {
1524 
1525  vRet=staticSession;
1526  }
1527 
1528  if (vRet.compare("")==0&&getValue(vRet).compare("")==0)
1529  {
1530 
1531  vRet=generateUnicCookie();
1532 
1533  cout << HTTPHTMLHeader()
1534  .setCookie(HTTPCookie(COOKIE_NAME, vRet));
1535  }
1536  else
1537  cout << HTTPHTMLHeader(); // Set up the HTML document
1538 
1539  cout << html() << head(title("Cgicc CardGame example")) << endl;
1540  cout << body() << endl;
1541 
1542  cout <<"<H1>Card Game</H1>";
1543  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>";
1544  //if the are a cookie in the stock we parse data
1545  datasplayer *vPlayer;
1546  if (getValue(vRet).compare("")!=0)
1547  {
1548 
1549  vPlayer=convertStringToStuct(getValue(vRet));
1550 
1551  }else
1552  {
1553 
1554  vPlayer= new datasplayer;
1555  srand ( time(NULL) );
1556 
1557 
1558  stringstream buffer;
1559  buffer << "P"<<(rand()%1000)+1<<"_"<<(rand()%1000)+1;
1560  vPlayer->identifiant=buffer.str();
1561 
1562  //We write a new empty value
1563 
1564  writeValue(vRet,convertStructToString(vPlayer));
1565 
1566  }
1567  if (action.compare("start")==0)
1568  {
1569  writeFileGame(vPlayer->identifiant,"");
1570  }
1571 
1572  gameRules(vPlayer,&action,&card);
1573 
1574  // Close the HTML document
1575  cout << body() << html();
1576 
1577  }
1578  catch(exception& e) {
1579  // handle any errors - omitted for brevity
1580  }
1581 }
1582 
1583 
Platform and operating system specific macro definitions.
The main header file for the GNU cgicc library.
The header file containing HTML output classes.
An HTTP Cookie.
Shortcut to HTTPContentHeader for text/html.
int main(int argc, char **argv)
The main function.
Definition: cardgame.cpp:1479
const std::vector< HTTPCookie > & getCookieList() const
Get a vector containing the HTTP cookies associated with this query.
The main class of the GNU cgicc library.
Definition: Cgicc.h:103
const CgiEnvironment & getEnvironment() const
Definition: Cgicc.h:394
const std::vector< FormEntry > & getElements() const
Get all the submitted form elements, excluding files.
Definition: Cgicc.h:348
form_iterator getElement(const std::string &name)
Find a radio button in a radio group, or a selected list item.
An HTTP cookie.
Definition: HTTPCookie.h:59
std::string getValue() const
Get the value of this cookie.
Definition: HTTPCookie.h:220
std::string getName() const
Get the name of this cookie.
Definition: HTTPCookie.h:211
Shortcut to HTTPContentHeader for text/html.
HTTPHeader & setCookie(const HTTPCookie &cookie)
Set a cookie to go out with this HTTPResponseHeader.
Definition: HTTPHeader.h:88
Contain all the functions to the coding game.
datasgame * getGame(string pName)
Convert the data to the datasgame struct.
Definition: cardgame.cpp:324
void drawPlayers(datasgame *pGame)
Draw players game informations.
Definition: cardgame.cpp:991
void writeValue(string pName, string pValue)
Write data in the cookie file.
Definition: cardgame.cpp:512
string generateUnicCookie()
generate an unique id
Definition: cardgame.cpp:655
void writeFileGame(string pName, string pValue)
Write data in the game file.
Definition: cardgame.cpp:529
void writeWinner(datasplayer *vPlayer, datasgame *pGame)
Draw the winner informations.
Definition: cardgame.cpp:931
int countGame()
Read the game file and count the number of games.
Definition: cardgame.cpp:676
void turnPlayers(datasplayer *vPlayer, datasgame *readedGame)
Change the hand in the game.
Definition: cardgame.cpp:1129
void writeGame(datasplayer *pPlayer, datasgame *pGame)
Write datasgame struct in the game file.
Definition: cardgame.cpp:598
string convertStructToString(datasplayer *pPlayer)
Generate a data in a single line from the data of the player.
Definition: cardgame.cpp:134
void drawInfos(datasplayer *vPlayer)
Draw player informations.
Definition: cardgame.cpp:971
vector< string > * loadAndMixCards()
Generate a mixed cards list.
Definition: cardgame.cpp:708
string getFileGame(string pName)
Get all game information from the name of the player.
Definition: cardgame.cpp:286
void playACard(datasplayer *vPlayer, datasgame *readedGame, string *card)
The algorithm when a card is played.
Definition: cardgame.cpp:1080
void drawCards(vector< string > *cardList)
Draw the cards list.
Definition: cardgame.cpp:910
datasgame * createGame(datasplayer *vPlayer)
Create a game from the data of the player.
Definition: cardgame.cpp:851
datasplayer * convertStringToStuct(string pPlayer)
Generate a datasgame struct from a single line
Definition: cardgame.cpp:168
void IAPlay(datasgame *readedGame, int pId)
AI algorithme for one AI player.
Definition: cardgame.cpp:1215
void drawCardInPlay(datasgame *pGame)
Draw the cards in the table.
Definition: cardgame.cpp:1024
bool testCard(datasplayer *vPlayer, datasgame *readedGame, string *card)
Test if the card is playable.
Definition: cardgame.cpp:1164
void gameRules(datasplayer *vPlayer, string *action, string *card)
The rules Algorithm.
Definition: cardgame.cpp:1275
void drawPlayerCards(datasplayer *vPlayer)
Draw the cards of the player.
Definition: cardgame.cpp:1044
string getNUMCookie(std::vector< HTTPCookie > pCookieVector)
Get the cookie of the game from the list of the cookies
Definition: cardgame.cpp:214
string getValue(string pName)
Get all personnal information from the cookie's Id.
Definition: cardgame.cpp:245
int calculateCard(string pCard)
Return the value of the card.
Definition: cardgame.cpp:784
The namespace containing the cgicc library.
Definition: Cgicc.h:52
std::vector< FormEntry >::const_iterator const_form_iterator
A vector of const FormEntry objects.
Definition: Cgicc.h:68
A data model for the games.
Definition: cardgame.cpp:97
vector< datasplayer * > * playersList
The players List.
Definition: cardgame.cpp:102
vector< string > * piocheCards
The rest of the cards.
Definition: cardgame.cpp:112
vector< string > * playedCards
The actual cards in the game.
Definition: cardgame.cpp:107
A data model for the player.
Definition: cardgame.cpp:62
string actualCard
The actual played card.
Definition: cardgame.cpp:87
string identifiant
the identifiant of the player
Definition: cardgame.cpp:72
vector< string > * cardsList
A list of the cards.
Definition: cardgame.cpp:67
int points
the score of the player
Definition: cardgame.cpp:82
bool isPlaying
If the player is playing the value is at true.
Definition: cardgame.cpp:77

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 Wed Sep 26 2018 00:00:00 for cgicc by doxygen 1.9.1