45#define COOKIE_NAME "ELPIELOJUEGO"
46#define COOKIE_FILE_NAME "sessions.tmp"
47#define GAME_FILE_NAME "games.tmp"
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())
179 if (carList[i]==
':'&&carList[i+1]==
':')
181 word=actualWord.str();
199 actualWord <<carList[i];
219 if (pCookieVector.size()== 0)
224 std::vector<HTTPCookie>::iterator itVectorData;
225 for(itVectorData = pCookieVector.begin(); itVectorData != pCookieVector.end(); itVectorData++)
230 if (theCookie.
getName ().compare(COOKIE_NAME)==0)
251 inFile.open(COOKIE_FILE_NAME);
253 ofstream outFile(COOKIE_FILE_NAME, ios::out);
256 inFile.open(COOKIE_FILE_NAME);
260 while (inFile&&!inFile.eof ())
265 inFile.getline (ligne,
sizeof (ligne));
268 if (s.find (pName)!= string::npos)
271 s.replace(0,pName.size(),
"");
292 inFile.open(GAME_FILE_NAME);
294 ofstream outFile(GAME_FILE_NAME, ios::out);
297 inFile.open(GAME_FILE_NAME);
300 while (inFile&&!inFile.eof ())
305 inFile.getline (ligne,
sizeof (ligne));
308 if (s.find (pName)!= string::npos)
334 if (vGame.compare(
"")==0)
341 char *carList=(
char *)vGame.c_str();
344 stringstream actualWord;
347 int playerCounterElement=0;
350 vector <string > *cardsList=
new vector<string>;
353 bool isPlaying=
false;
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>;
367 vector <string > *queue2=
new vector<string>;
370 for (
unsigned int i=0;i<vGame.size();i++)
372 if (i+1<vGame.size())
374 if (carList[i]==
':'&&carList[i+1]==
':')
377 word=actualWord.str();
385 vNBPLayers=atoi(word.c_str());
389 if (playerCounter<vNBPLayers)
393 if (playerCounterElement==0)
398 if (playerCounterElement==1)
401 isPlaying=(word.compare(
"1")==0)?
true:
false;
403 if (playerCounterElement==2)
406 points=atoi(word.c_str());
408 if (playerCounterElement==3)
413 if (playerCounterElement==4)
416 vNBCards=atoi(word.c_str());
418 if (playerCounterElement>=5&&vCardsCounter<vNBCards)
421 cardsList->push_back(word);
424 if (vCardsCounter==vNBCards&&playerCounterElement>=5)
437 playerCounterElement=0;
439 cardsList=
new vector <string >;
442 }
else{playerCounterElement++; }
449 if (vNBCardsQueue1==0&&!vCountedCardsQ1)
451 vNBCardsQueue1=atoi(word.c_str());
452 vCountedCardsQ1=
true;
458 if (vCardsCounterQ1<vNBCardsQueue1)
461 queue1->push_back(word);
465 if (!vCountedCardsQ2&&vNBCardsQueue2==0&&vCardsCounterQ1==vNBCardsQueue1)
468 vNBCardsQueue2=atoi(word.c_str());
469 vCountedCardsQ2=
true;
474 if (vCardsCounterQ2<vNBCardsQueue2)
477 queue2->push_back(word);
481 if (vCardsCounterQ2==vNBCardsQueue2&&vCardsCounterQ2!=0)
503 actualWord <<carList[i];
519 ofstream outFile(COOKIE_FILE_NAME, ios::out|ios::app);
521 outFile << pName<<
"::"<<pValue<<
"\n";
539 inFile.open(GAME_FILE_NAME,ios::in);
542 ofstream outFile(GAME_FILE_NAME, ios::out|ios::app);
548 inFile.open(GAME_FILE_NAME,ios::in);
555 while (inFile&&!inFile.eof ())
559 inFile.getline (ligne,
sizeof (ligne));
562 if (s.find (pName)!= string::npos)
577 ofstream outFile(GAME_FILE_NAME, ios::out|ios::trunc);
580 outFile << buffer.str();
583 outFile << pValue<<
"\n";
609 for(
unsigned int i=0;i<pGame->
playersList->size(); i++)
615 std::vector<string>::iterator itVectorData2;
618 buffer <<itVectorData->
cardsList->size()<<
"::";
619 for(itVectorData2 = itVectorData->
cardsList->begin(); itVectorData2 != itVectorData->
cardsList->end(); itVectorData2++)
621 buffer <<*itVectorData2<<
"::";
629 for (
unsigned int i=0;i<pGame->
playedCards->size();i++)
641 for (
unsigned int i=0;i<pGame->
piocheCards->size();i++)
662 srand ( time(NULL) );
664 nb_aleatoire=(rand()%100)+1;
668 buffer << nb_aleatoire<<
"_"<<time(NULL);
670 string vNum=buffer.str() ;
684 inFile.open(GAME_FILE_NAME);
691 while (inFile&&!inFile.eof ())
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) );
770 vector<string> *vRet=
new vector<string>;
771 while(UnorderedPioche.size()>0)
774 nb_aleatoire=(rand()%UnorderedPioche.size());
775 vRet->push_back(UnorderedPioche[nb_aleatoire]);
776 UnorderedPioche.erase((UnorderedPioche.begin())+nb_aleatoire);
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;
891 for (
unsigned int i=0;i<myGame->
playersList->size();i++)
894 myGame->
playersList->at(i)->cardsList=
new vector<string>;
895 for (
unsigned int j=0;j<vNbCards/myGame->
playersList->size();j++)
918 for (
unsigned int i=0;i<cardList->size();i++)
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;
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++)
946 if (scoreOfTheWinner<pGame->playersList->at(i)->points)
949 scoreOfTheWinner=pGame->
playersList->at(i)->points;
953 cout <<
"<div style=\"position:absolute;top:50;left:"<<150<<
"\" >";
954 if (playerId==winner)
956 cout <<
"<h2>YOU WIN !</h2>"<<endl;
957 cout <<
"<b>Your score is : "<<scoreOfTheWinner<<
"</b>"<<endl;
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;
977 cout <<
"<div style=\"position:absolute;width:140;top:50;left:"<<0<<
"\" >";
978 cout <<
"The latest Played Cards ";
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;
983 cout <<
"<div style=\"width:140;position:absolute;top:375;left:"<<0<<
"\" >";
984 cout <<
"Your Cards, you can choose one card.";
999 for (
unsigned int i=0;i<pGame->
playersList->size();i++)
1001 bool afficheFirst=
false;
1002 if (vFirst==
false&&pGame->
playersList->at(i)->actualCard.compare(
"")!=0)
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>";
1011 cout <<
"<img border=\"0\" width=\"100\" src=\"images/"<<pGame->
playersList->at(i)->actualCard<<
".png\" alt=\" \" />"<<endl;
1012 if (afficheFirst==
true)
1014 cout <<
"<br>The color to play";
1032 for (
unsigned int i=0;i<pGame->
playedCards->size();i++)
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;
1051 cout <<
"<form name=\"cards\">";
1052 cout <<
"<input type=\"hidden\" name=\"actionner\" value=\"\">";
1053 cout <<
"<input type=\"hidden\" name=\"card\" value=\"\">";
1055 for (
unsigned int i=0;i<vPlayer->
cardsList->size();i++)
1057 cout <<
"<div style=\"position:absolute;top:375;left:"<<i*20+150<<
"\" >";
1060 cout <<
"<a href=\"javascript:document.forms.cards.actionner.value='playcard';document.forms.cards.card.value='"<<vPlayer->
cardsList->at(i)<<
"';document.forms.cards.submit();\">";
1062 cout <<
"<img border=\"0\" width=\"100\" src=\"images/"<<vPlayer->
cardsList->at(i)<<
".png\" alt=\"Carte ["<<i<<
"]="<<vPlayer->
cardsList->at(i)<<
"\" />"<<endl;
1088 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1097 for (
unsigned int j=0;j<readedGame->
playersList->at(i)->cardsList->size();j++)
1101 if(readedGame->
playersList->at(i)->cardsList->at(j).compare(*card)==0)
1135 unsigned int IdTurn=0;
1138 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1154 readedGame->
playersList->at(IdTurn)->isPlaying=
true;
1177 string color=readedGame->
playedCards->front().substr(0,1);
1179 if (color.compare(card->substr(0,1))==0)
1187 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1193 for (
unsigned int j=0;j<readedGame->
playersList->at(i)->cardsList->size();j++)
1195 if (color.compare(readedGame->
playersList->at(i)->cardsList->at(j).substr(0,1))==0)
1222 if (readedGame->
playersList->at(pId)->cardsList->size()==0)
1234 string color=readedGame->
playedCards->front().substr(0,1);
1236 std::sort(readedGame->
playersList->at(pId)->cardsList->begin(),readedGame->
playersList->at(pId)->cardsList->end());
1239 bool vColorOk=
false;
1241 for (
unsigned int i=0;i<readedGame->
playersList->at(pId)->cardsList->size();i++)
1243 unsigned int actualColor=color.compare(readedGame->
playersList->at(pId)->cardsList->at(i).substr(0,1));
1247 if (actualColor==0&&vColorOk==
true)
1252 if (actualColor!=0&&vColorOk==
true)
1259 if (actualColor==0&&vColorOk==
false)
1286 if (readedGame==NULL)
1293 cout <<
"THE PLAY TABLES ARE FULL!"<<endl;
1301 unsigned int thePlayer=0;
1303 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1320 int vResComp=action->compare(
"playcard");
1322 if ( vResComp==0 && vPlayer->
isPlaying==
true)
1324 if (
testCard(vPlayer,readedGame,card)==
true)
1332 cout <<
"<div style=\"position:absolute;top:50;left:"<<150<<
"\" >";
1333 cout <<
"<b>You can not Play this card!</b><br>\n";
1340 unsigned int vNbTurns=0;
1341 while (vNbTurns<=readedGame->playersList->size()+1&&readedGame->
playersList->at(thePlayer)->isPlaying==
false)
1344 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1353 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1361 if (readedGame->
playersList->at(i)->isPlaying==
true)
1372 vector <string> *lastPlayedCard=
new vector<string>;
1381 for(
unsigned int i=0;i<readedGame->
playersList->size();i++)
1383 string plCard=readedGame->
playersList->at(i)->actualCard;
1387 int compCard=plCard.substr(0,1).compare(readedGame->
playedCards->front().substr(0,1));
1390 if (theMax<cardValue&&compCard==0)
1403 readedGame->
playersList->at(vWiner)->isPlaying=
true;
1404 readedGame->
playersList->at(vWiner)->points+=total;
1411 lastPlayedCard->push_back(readedGame->
playedCards->front());
1415 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1419 if (readedGame->
playersList->at(thePlayer)->cardsList->size()!=0)
1424 while (readedGame->
playersList->at(thePlayer)->isPlaying==
false)
1427 for (
unsigned int i=0;i<readedGame->
playersList->size();i++)
1431 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1437 if (readedGame->
playersList->at(i)->isPlaying==
true)
1446 if (readedGame->
playersList->at(thePlayer)->isPlaying==
true)
1452 if (readedGame->
playersList->at(thePlayer)->cardsList->size()!=0)
1465 catch(std::exception &error)
1467 cout <<
"Erreur:"<<error.what()<<
"<br>\n";
1497 if (actionIn!= cgi.
getElements().end() &&actionIn->getValue().empty() ==
false)
1499 action=actionIn->getValue();
1502 if (playedCard!= cgi.
getElements().end() &&playedCard->getValue().empty() ==
false)
1504 card=playedCard->getValue();
1507 string staticSession=
"";
1513 staticSession =argv[1];
1523 if (vRet.compare(
"")==0&&staticSession.compare(
"")!=0)
1529 if (vRet.compare(
"")==0&&
getValue(vRet).compare(
"")==0)
1540 cout << html() << head(title(
"Cgicc CardGame example")) << endl;
1541 cout << body() << endl;
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>";
1556 srand ( time(NULL) );
1559 stringstream buffer;
1560 buffer <<
"P"<<(rand()%1000)+1<<
"_"<<(rand()%1000)+1;
1568 if (action.compare(
"start")==0)
1576 cout << body() << html();
1579 catch(exception& e) {
Platform and operating system specific macro definitions.
The main header file for the GNU cgicc library.
The header file containing HTML output classes.
int main(int argc, char **argv)
The main function.
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.
const CgiEnvironment & getEnvironment() const
const std::vector< FormEntry > & getElements() const
Get all the submitted form elements, excluding files.
form_iterator getElement(const std::string &name)
Find a radio button in a radio group, or a selected list item.
std::string getValue() const
Get the value of this cookie.
std::string getName() const
Get the name of this cookie.
The namespace containing the cgicc library.
std::vector< FormEntry >::const_iterator const_form_iterator
A vector of const FormEntry objects.
A data model for the games.
vector< datasplayer * > * playersList
The players List.
vector< string > * piocheCards
The rest of the cards.
vector< string > * playedCards
The actual cards in the game.
A data model for the player.
string actualCard
The actual played card.
string identifiant
the identifiant of the player
vector< string > * cardsList
A list of the cards.
int points
the score of the player
bool isPlaying
If the player is playing the value is at true.