proj home

Files   Classes   Functions   Hierarchy  

playerGame.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 using namespace std;
00004 
00005 #include <deckpontoon.h>
00006 
00007 #include <playerGame.h>
00008 
00009 
00010 playerGame::playerGame()
00011 {
00012   dealerWins=0;
00013   playerWins=0;
00014 
00015   cards = new deckpontoon();
00016   deal = new dealer(*cards);
00017 
00018   alive=false;
00019 }
00020 
00021 playerGame::~playerGame()
00022 {
00023   delete deal;
00024   deal=0;
00025 
00026   delete cards;
00027   cards=0;
00028 }
00029 
00030 
00031 void playerGame::notalive_dealCards()
00032 {
00033   assert(alive==false);
00034 
00035   player.reset();
00036   player.add(cards->draw());
00037   deal->hnd.reset();
00038   deal->hnd.add(cards->draw());
00039 
00040   bet=1;
00041 
00042   alive=true;
00043 }
00044 
00045 
00046 void playerGame::alive_hold()
00047 {
00048   assert(alive==true);
00049 
00050   assert(player.isbusted()==false);
00051 
00052   deal->play();
00053 
00054   alive=false;
00055 
00056   if (deal->hnd.isbusted())
00057   {
00058     playerWins += bet;
00059     return;
00060   }
00061 
00062   if (deal->hnd.value > player.value)
00063   {
00064     dealerWins += bet;
00065     return;
00066   }
00067 
00068   if (deal->hnd.value < player.value)
00069   {
00070     playerWins += bet;
00071     return;
00072   }
00073 
00074   // Draw game then do nothing.
00075 }
00076 
00077 
00078 void playerGame::alive_play()
00079 {
00080   assert(alive==true);
00081 
00082   player.add(cards->draw());
00083   if (player.isbusted())
00084   {
00085     alive=false; 
00086     dealerWins += bet;
00087     return;
00088   }
00089 
00090   if (player.calculate()==21)
00091   {
00092     alive=false; 
00093     playerWins += bet;
00094     return;
00095   }
00096 
00097   
00098 }
00099 
00100 
00101 void playerGame::alive_doubleAndPlay()
00102 {
00103   assert(alive==true);
00104 
00105   bet *= 2;
00106   alive_play();
00107 }
00108 
00109 
00110 playerGameSimulate::playerGameSimulate( uintc _nsamples )
00111   : nsamples(_nsamples)
00112 {
00113 }
00114 
00115 
00116 void playerGameSimulate::calculateHit
00117 (
00118   double & win,
00119   double & loss,
00120   uintc dealer0,
00121   uintc player0
00122 ) 
00123 {
00124   dealerWins = 0;
00125   playerWins = 0;
00126   bet = 1;
00127 
00128   hand & dh(deal->hnd);
00129 
00130   if (dealer0==1)
00131   {
00132     for (uint i=0; i<nsamples; ++i)
00133     {
00134       player.value = player0;
00135       dh.value = 0;
00136       dh.hasAce = true;
00137       alive = true;
00138 
00139       alive_play();
00140       if (alive)
00141         alive_hold();
00142     }
00143   }
00144   else
00145   {
00146     for (uint i=0; i<nsamples; ++i)
00147     {
00148       player.value = player0;
00149       dh.value = dealer0;
00150       dh.hasAce = false;
00151       alive = true;
00152 
00153       alive_play();
00154       if (alive)
00155         alive_hold();
00156     }
00157   }
00158 
00159   loss = (double)dealerWins / (double) nsamples;
00160   win = (double)playerWins / (double) nsamples;
00161 }
00162 
00163 
00164 
00165 
00166 void playerGameSimulate::calculate
00167 (
00168   double & win,
00169   double & loss,
00170   uintc dealer0,
00171   uintc player0
00172 ) 
00173 {
00174   dealerWins = 0;
00175   playerWins = 0;
00176   bet = 1;
00177 
00178   hand & dh(deal->hnd);
00179 
00180   if (dealer0==1)
00181   {
00182     for (uint i=0; i<nsamples; ++i)
00183     {
00184       player.value = player0;
00185       dh.value = 0;
00186       dh.hasAce = true;
00187       alive = true;
00188       alive_hold();
00189     }
00190   }
00191   else
00192   {
00193     for (uint i=0; i<nsamples; ++i)
00194     {
00195       player.value = player0;
00196       dh.value = dealer0;
00197       dh.hasAce = false;
00198       alive = true;
00199       alive_hold();
00200     }
00201   }
00202 
00203   loss = (double)dealerWins / (double) nsamples;
00204   win = (double)playerWins / (double) nsamples;
00205 }
00206 
00207 
00208 void playerGameSimulate::tableCalculateHitRowPrint(uintc dealer0)
00209 {
00210   double win;
00211   double loss;
00212 
00213   //cout << "(dealer0,player)=<dealer win,player win>" << endl;
00214   cout << "(dealer0,player)=&lt;dealer win,player win&gt;" << endl;
00215 
00216   for (uint i=7; i<19; ++i)
00217   {
00218     calculateHit(win,loss,dealer0,i);
00219     //cout << "(" << dealer0 << "," << i << ")=<" << loss << "," << win << ">" << endl;
00220     cout << "(" << dealer0 << "," << i << ")=&lt;" << loss << "," << win << "&gt;" << endl;
00221   }
00222 }
00223 
00224 void playerGameSimulate::tableCalculateHitPrint()
00225 {
00226   for (uint k=1; k<=10; ++k)
00227     tableCalculateHitRowPrint(k);
00228 }
00229 
00230 
00231 
00232 
00233 void playerGameSimulate::tableCalculateRowPrint(uintc dealer0)
00234 {
00235   double win;
00236   double loss;
00237 
00238   //cout << "(dealer0,player)=<dealer win,player win>" << endl;
00239   cout << "(dealer0,player)=&lt;dealer win,player win&gt;" << endl;
00240 
00241   for (uint i=12; i<21; ++i)
00242   {
00243     calculate(win,loss,dealer0,i);
00244     //cout << "(" << dealer0 << "," << i << ")=<" << loss << "," << win << ">" << endl;
00245     cout << "(" << dealer0 << "," << i << ")=&lt;" << loss << "," << win << "&gt;" << endl;
00246   }
00247 }
00248 
00249 void playerGameSimulate::tableCalculatePrint()
00250 {
00251   for (uint k=1; k<=10; ++k)
00252     tableCalculateRowPrint(k);
00253 }
00254 
00255 

Generated on Fri Mar 4 00:49:30 2011 for Chelton Evans Source by  doxygen 1.5.8