proj home

Files   Classes   Functions   Hierarchy  

mazegameD2state01.cpp

Go to the documentation of this file.
00001 #include <mazegameD2state01.h>
00002 #include <mazematrixD2createmaze.h>
00003 #include <stringconvert.h>
00004 #include <stringtagparser.h>
00005 
00006 void mazegameD2state01::game01default()
00007 {
00008   m=8;
00009   n=12;
00010 
00011   path.clear();
00012 
00013   gamestart=0;
00014   gamefinish=0;
00015 
00016   randomize=true;
00017 
00018   proper=true;
00019   deletewall=0.025; // 2.5%
00020 }
00021 
00022 
00023 mazegameD2state01::mazegameD2state01()
00024   : m(mz.dim[0]), n(mz.dim[1]) 
00025 {
00026   game01default();
00027 }
00028 
00029 
00030 void mazegameD2state01::game01()
00031 {
00032   assert(m!=0);
00033   assert(n!=0);
00034 
00035   uint mn=m*n;
00036   assert(mn!=0);
00037   if (mn==0)
00038     return;
00039 
00040   if (randomize)
00041     srand( (unsigned)time( NULL ) );
00042   
00043   gamestart = gamefinish = 0;
00044   for ( ; gamestart==gamefinish; )
00045   {
00046     gamestart = 1+(rand()%mn);
00047     gamefinish = 1+(rand()%mn);
00048   }
00049 
00050   path.clear();
00051   path.push_back(gamestart);
00052 
00053   mz.dimset(m,n);
00054   mazematrixD2createmaze<uint> mc(mz);
00055   mc.buildpropermaze(); 
00056 
00057   if (proper==false)
00058     mc.impropermaze01(deletewall);
00059 
00060   assert(valid());
00061 }
00062 
00063 boolc mazegameD2state01::valid() const
00064 {
00065 //cout << SHOW(m) << endl;
00066 //cout << SHOW(mz.dim[0]) << endl;
00067 
00068 //cout << SHOW(gamestart) << " " << SHOW(gamefinish) << " " << SHOW(mz.dim[2]) << endl;
00069 
00070   assertreturnfalse(mz.valid());
00071   //assertreturnfalse(m==mz.dim[0]);
00072   //assertreturnfalse(n==mz.dim[1]);
00073   assertreturnfalse(!((gamestart==0)||(gamestart>mz.dim[2])));
00074   assertreturnfalse(!((gamefinish==0)||(gamefinish>mz.dim[2])));
00075 
00076   return true;
00077 }
00078 
00079 stringc mazegameD2state01::settings() const
00080 {
00081   string options="";
00082   string s;
00083 
00084   options += ( "m=" + stringconvert::tostring(m) + " " );
00085   options += ( "n=" + stringconvert::tostring(n) + " " );
00086   options += ( "gamestart=" + stringconvert::tostring(gamestart) + " " );
00087   options += ( "gamefinish=" + stringconvert::tostring(gamefinish) + " " );
00088   options += ( "randomize=" + stringconvert::tostring(randomize) + " " );
00089   options += ( "proper=" + stringconvert::tostring(proper) + " " );
00090   options += ( "deletewall=" + stringconvert::tostring(deletewall) + " " );
00091 
00092   return options;
00093 }
00094 
00095 uintc mazegameD2state01::currentpos() const
00096 {
00097   assert(path.empty()==false);
00098   return path[path.size()-1];
00099 }
00100 
00101 boolc mazegameD2state01::currentmove(uintc dir)
00102 {
00103   assert(dir<4);
00104 
00105   bool res=false;
00106   uint k2;
00107 
00108   k2 = mz.vi[currentpos()].ni[dir];
00109   if (k2!=0)
00110     res=true;
00111 
00112 //  mz.move(res,k2,dir,currentpos());
00113 
00114   if (res==false)
00115     return false;
00116 
00117   if (path.size()>1)
00118   {
00119     if (path[path.size()-2]==k2)
00120     {
00121       path.pop_back(); 
00122       return true;
00123     }
00124   }
00125 
00126   path.push_back(k2);
00127   return true;
00128 }
00129 
00130 mazegameD2state01::operator stringc () const
00131 {
00132   string s1;
00133   s1 += "<mazegameD2state01>\n";
00134 
00135   s1 += stringtag(gamestart,"gamestart");
00136   s1 += stringtag(gamefinish,"gamefinish");
00137   s1 += stringtag(path.size(),"pathsize");
00138   s1 += "<path>\n";
00139   for (uint i=0; i<path.size(); ++i)
00140   {
00141     s1 += stringto(path[i]);
00142     s1 += " ";
00143   }
00144   s1 += "\n</path>\n";
00145   s1 += stringtag(randomize,"randomize");
00146   s1 += stringtag(proper,"proper");
00147   s1 += stringtag(deletewall,"deletewall");
00148 
00149   s1 += (stringc)mz;
00150   s1 += "</mazegameD2state01>\n";
00151 
00152   return s1;
00153 }
00154 
00155 
00156 void mazegameD2state01::serializeInverse(stringc & str)
00157 {
00158   string s2(stringtagparser(str).data("mazegameD2state01"));
00159   stringfrom(gamestart,stringtagparser(s2).data("gamestart"));
00160   stringfrom(gamefinish,stringtagparser(s2).data("gamefinish"));
00161   stringfrom(randomize,stringtagparser(s2).data("randomize"));
00162   stringfrom(proper,stringtagparser(s2).data("proper"));
00163   stringfrom(deletewall,stringtagparser(s2).data("deletewall"));
00164   
00165   path.clear();
00166   uint n;
00167   stringfrom(n,stringtagparser(s2).data("pathsize"));
00168   {
00169     uint k;
00170     stringstream ss(stringtagparser(s2).data("path"));
00171     for (uint i=0; i<n; ++i)
00172     {
00173       ss >> k;
00174       path.push_back(k); 
00175     }
00176   }
00177 
00178   mz.serializeInverse(s2);
00179 }
00180 
00181 

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