45 #define COOKIE_NAME "ELPIELOJUEGO"
46 #define COOKIE_FILE_NAME "sessions.tmp"
47 #define GAME_FILE_NAME "games.tmp"
51 using namespace cgicc;
120 namespace CardGameTools
137 std::vector<string>::iterator itVectorData;
140 for(itVectorData = pPlayer->
cardsList->begin(); itVectorData != pPlayer->
cardsList->end(); itVectorData++)
143 buffer << *itVectorData;
144 if (itVectorData != pPlayer->
cardsList->end())
171 char *carList=(
char *)pPlayer.c_str();
174 stringstream actualWord;
175 for (
unsigned int i=0;i<pPlayer.size();i++)
177 if (i+1<pPlayer.size())
178 if (carList[i]==
':'&&carList[i+1]==
':')
180 word=actualWord.str();
198 actualWord <<carList[i];
217 if (pCookieVector.size()== 0)
222 std::vector<HTTPCookie>::iterator itVectorData;
223 for(itVectorData = pCookieVector.begin(); itVectorData != pCookieVector.end(); itVectorData++)
228 if (theCookie.
getName ().compare(COOKIE_NAME)==0)
249 inFile.open(COOKIE_FILE_NAME);
251 ofstream outFile(COOKIE_FILE_NAME, ios::out);
254 inFile.open(COOKIE_FILE_NAME);
258 while (inFile&&!inFile.eof ())
263 inFile.getline (ligne,
sizeof (ligne));
266 if (s.find (pName)!= string::npos)
269 s.replace(0,pName.size(),
"");
290 inFile.open(GAME_FILE_NAME);
292 ofstream outFile(GAME_FILE_NAME, ios::out);
295 inFile.open(GAME_FILE_NAME);
298 while (inFile&&!inFile.eof ())
303 inFile.getline (ligne,
sizeof (ligne));
306 if (s.find (pName)!= string::npos)
332 if (vGame.compare(
"")==0)
339 char *carList=(
char *)vGame.c_str();
342 stringstream actualWord;
345 int playerCounterElement=0;
348 vector <string > *cardsList=
new vector<string>;
351 bool isPlaying=
false;
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>;
365 vector <string > *queue2=
new vector<string>;
368 for (
unsigned int i=0;i<vGame.size();i++)
370 if (i+1<vGame.size())
371 if (carList[i]==
':'&&carList[i+1]==
':')
374 word=actualWord.str();
382 vNBPLayers=atoi(word.c_str());
386 if (playerCounter<vNBPLayers)
390 if (playerCounterElement==0)
395 if (playerCounterElement==1)
398 isPlaying=(word.compare(
"1")==0)?
true:
false;
400 if (playerCounterElement==2)
403 points=atoi(word.c_str());
405 if (playerCounterElement==3)
410 if (playerCounterElement==4)
413 vNBCards=atoi(word.c_str());
415 if (playerCounterElement>=5&&vCardsCounter<vNBCards)
418 cardsList->push_back(word);
421 if (vCardsCounter==vNBCards&&playerCounterElement>=5)
434 playerCounterElement=0;
436 cardsList=
new vector <string >;
439 }
else{playerCounterElement++; }
446 if (vNBCardsQueue1==0&&!vCountedCardsQ1)
448 vNBCardsQueue1=atoi(word.c_str());
449 vCountedCardsQ1=
true;
455 if (vCardsCounterQ1<vNBCardsQueue1)
458 queue1->push_back(word);
462 if (!vCountedCardsQ2&&vNBCardsQueue2==0&&vCardsCounterQ1==vNBCardsQueue1)
465 vNBCardsQueue2=atoi(word.c_str());
466 vCountedCardsQ2=
true;
471 if (vCardsCounterQ2<vNBCardsQueue2)
474 queue2->push_back(word);
478 if (vCardsCounterQ2==vNBCardsQueue2&&vCardsCounterQ2!=0)
499 actualWord <<carList[i];
515 ofstream outFile(COOKIE_FILE_NAME, ios::out|ios::app);
517 outFile << pName<<
"::"<<pValue<<
"\n";
535 inFile.open(GAME_FILE_NAME,ios::in);
538 ofstream outFile(GAME_FILE_NAME, ios::out|ios::app);
544 inFile.open(GAME_FILE_NAME,ios::in);
551 while (inFile&&!inFile.eof ())
555 inFile.getline (ligne,
sizeof (ligne));
558 if (s.find (pName)!= string::npos)
573 ofstream outFile(GAME_FILE_NAME, ios::out|ios::trunc);
576 outFile << buffer.str();
579 outFile << pValue<<
"\n";
605 for(
unsigned int i=0;i<pGame->
playersList->size(); i++)
611 std::vector<string>::iterator itVectorData2;
614 buffer <<itVectorData->
cardsList->size()<<
"::";
615 for(itVectorData2 = itVectorData->
cardsList->begin(); itVectorData2 != itVectorData->
cardsList->end(); itVectorData2++)
617 buffer <<*itVectorData2<<
"::";
625 for (
unsigned int i=0;i<pGame->
playedCards->size();i++)
637 for (
unsigned int i=0;i<pGame->
piocheCards->size();i++)
658 srand ( time(NULL) );
660 nb_aleatoire=(rand()%100)+1;
664 buffer << nb_aleatoire<<
"_"<<time(NULL);
666 string vNum=buffer.str() ;
680 inFile.open(GAME_FILE_NAME);
687 while (inFile&&!inFile.eof ())
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) );
766 vector<string> *vRet=
new vector<string>;
767 while(UnorderedPioche.size()>0)
770 nb_aleatoire=(rand()%UnorderedPioche.size());
771 vRet->push_back(UnorderedPioche[nb_aleatoire]);
772 UnorderedPioche.erase((UnorderedPioche.begin())+nb_aleatoire);
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;
887 for (
unsigned int i=0;i<myGame->
playersList->size();i++)
890 myGame->
playersList->at(i)->cardsList=
new vector<string>;
891 for (
unsigned int j=0;j<vNbCards/myGame->
playersList->size();j++)
914 for (
unsigned int i=0;i<cardList->size();i++)
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;
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++)
942 if (scoreOfTheWinner<pGame->playersList->at(i)->points)
945 scoreOfTheWinner=pGame->
playersList->at(i)->points;
949 cout <<
"<div style=\"position:absolute;top:50;left:"<<150<<
"\" >";
950 if (playerId==winner)
952 cout <<
"<h2>YOU WIN !</h2>"<<endl;
953 cout <<
"<b>Your score is : "<<scoreOfTheWinner<<
"</b>"<<endl;
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;
973 cout <<
"<div style=\"position:absolute;width:140;top:50;left:"<<0<<
"\" >";
974 cout <<
"The latest Played Cards ";
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;
979 cout <<
"<div style=\"width:140;position:absolute;top:375;left:"<<0<<
"\" >";
980 cout <<
"Your Cards, you can choose one card.";
995 for (
unsigned int i=0;i<pGame->
playersList->size();i++)
997 bool afficheFirst=
false;
998 if (vFirst==
false&&pGame->
playersList->at(i)->actualCard.compare(
"")!=0)
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>";
1007 cout <<
"<img border=\"0\" width=\"100\" src=\"images/"<<pGame->
playersList->at(i)->actualCard<<
".png\" alt=\" \" />"<<endl;
1008 if (afficheFirst==
true)
1010 cout <<
"<br>The color to play";
1028 for (
unsigned int i=0;i<pGame->
playedCards->size();i++)
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;
1047 cout <<
"<form name=\"cards\">";
1048 cout <<
"<input type=\"hidden\" name=\"actionner\" value=\"\">";
1049 cout <<
"<input type=\"hidden\" name=\"card\" value=\"\">";
1051 for (
unsigned int i=0;i<vPlayer->
cardsList->size();i++)
1053 cout <<
"<div style=\"position:absolute;top:375;left:"<<i*20+150<<
"\" >";
1056 cout <<
"<a href=\"javascript:document.forms.cards.actionner.value='playcard';document.forms.cards.card.value='"<<vPlayer->
cardsList->at(i)<<
"';document.forms.cards.submit();\">";
1058 cout <<
"<img border=\"0\" width=\"100\" src=\"images/"<<vPlayer->
cardsList->at(i)<<
".png\" alt=\"Carte ["<<i<<
"]="<<vPlayer->
cardsList->at(i)<<
"\" />"<<endl;
1084 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1093 for (
unsigned int j=0;j<readedGame->
playersList->at(i)->cardsList->size();j++)
1097 if(readedGame->
playersList->at(i)->cardsList->at(j).compare(*card)==0)
1131 unsigned int IdTurn=0;
1134 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1150 readedGame->
playersList->at(IdTurn)->isPlaying=
true;
1173 string color=readedGame->
playedCards->front().substr(0,1);
1175 if (color.compare(card->substr(0,1))==0)
1183 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1189 for (
unsigned int j=0;j<readedGame->
playersList->at(i)->cardsList->size();j++)
1191 if (color.compare(readedGame->
playersList->at(i)->cardsList->at(j).substr(0,1))==0)
1218 if (readedGame->
playersList->at(pId)->cardsList->size()==0)
1230 string color=readedGame->
playedCards->front().substr(0,1);
1232 std::sort(readedGame->
playersList->at(pId)->cardsList->begin(),readedGame->
playersList->at(pId)->cardsList->end());
1235 bool vColorOk=
false;
1237 for (
unsigned int i=0;i<readedGame->
playersList->at(pId)->cardsList->size();i++)
1239 unsigned int actualColor=color.compare(readedGame->
playersList->at(pId)->cardsList->at(i).substr(0,1));
1243 if (actualColor==0&&vColorOk==
true)
1248 if (actualColor!=0&&vColorOk==
true)
1255 if (actualColor==0&&vColorOk==
false)
1282 if (readedGame==NULL)
1289 cout <<
"THE PLAY TABLES ARE FULL!"<<endl;
1297 unsigned int thePlayer=0;
1299 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1316 int vResComp=action->compare(
"playcard");
1318 if ( vResComp==0 && vPlayer->
isPlaying==
true)
1320 if (
testCard(vPlayer,readedGame,card)==
true)
1328 cout <<
"<div style=\"position:absolute;top:50;left:"<<150<<
"\" >";
1329 cout <<
"<b>You can not Play this card!</b><br>\n";
1336 unsigned int vNbTurns=0;
1337 while (vNbTurns<=readedGame->playersList->size()+1&&readedGame->
playersList->at(thePlayer)->isPlaying==
false)
1340 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1349 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1357 if (readedGame->
playersList->at(i)->isPlaying==
true)
1368 vector <string> *lastPlayedCard=
new vector<string>;
1377 for(
unsigned int i=0;i<readedGame->
playersList->size();i++)
1379 string plCard=readedGame->
playersList->at(i)->actualCard;
1383 int compCard=plCard.substr(0,1).compare(readedGame->
playedCards->front().substr(0,1));
1386 if (theMax<cardValue&&compCard==0)
1399 readedGame->
playersList->at(vWiner)->isPlaying=
true;
1400 readedGame->
playersList->at(vWiner)->points+=total;
1407 lastPlayedCard->push_back(readedGame->
playedCards->front());
1411 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1415 if (readedGame->
playersList->at(thePlayer)->cardsList->size()!=0)
1420 while (readedGame->
playersList->at(thePlayer)->isPlaying==
false)
1423 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1427 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1433 if (readedGame->
playersList->at(i)->isPlaying==
true)
1442 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1448 if (readedGame->
playersList->at(thePlayer)->cardsList->size()!=0)
1461 catch(std::exception &error)
1463 cout <<
"Erreur:"<<error.what()<<
"<br>\n";
1469 using namespace CardGameTools;
1496 if (actionIn!= cgi.
getElements().end() &&actionIn->getValue().empty() ==
false)
1498 action=actionIn->getValue();
1501 if (playedCard!= cgi.
getElements().end() &&playedCard->getValue().empty() ==
false)
1503 card=playedCard->getValue();
1506 string staticSession=
"";
1512 staticSession =argv[1];
1522 if (vRet.compare(
"")==0&&staticSession.compare(
"")!=0)
1528 if (vRet.compare(
"")==0&&
getValue(vRet).compare(
"")==0)
1539 cout << html() << head(title(
"Cgicc CardGame example")) << endl;
1540 cout << body() << endl;
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>";
1555 srand ( time(NULL) );
1558 stringstream buffer;
1559 buffer <<
"P"<<(rand()%1000)+1<<
"_"<<(rand()%1000)+1;
1567 if (action.compare(
"start")==0)
1575 cout << body() << html();
1578 catch(exception& e) {