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