proj home

Files   Classes   Functions   Hierarchy  

mazegameD2solver01.cpp

Go to the documentation of this file.
00001 #include <cstdlib>
00002 
00003 #include <mazegameD2solver01.h>
00004 
00005 
00006 mazegameD2solver01::mazegameD2solver01(mazegameD2state01 & mg_)
00007   : mg(mg_), vi(mg.mz.vi)
00008 {
00009 }
00010 
00011 void mazegameD2solver01::reset()
00012 {
00013   mg.path.clear();
00014   mg.path.push_back(mg.gamestart);
00015 
00016   visited.clear();
00017   visited.insert(0);
00018   visited.insert(mg.gamestart);
00019 }
00020 
00021 boolc mazegameD2solver01::pathcontains(uintc k) const
00022 {
00023   assertreturnfalse(k!=0);
00024 
00025   for (uint i=0; i<mg.path.size(); ++i)
00026   {
00027     if (mg.path[i]==k)
00028       return true;
00029   }
00030  
00031   return false; 
00032 }
00033 
00034 boolc mazegameD2solver01::operator ! () const
00035 {
00036   if (pathcontains(mg.gamefinish))
00037     return false;
00038 
00039   return true;
00040 }
00041 
00042 void mazegameD2solver01::operator ++ ()
00043 {
00044   // Try moving forward.
00045 
00046   uint cpi=mg.currentpos();
00047   assert(cpi!=0);
00048 
00049   uint k2;
00050   uint gi[4];
00051   uint gisize=0;
00052   for (uint i=0; i<4; ++i)
00053   {
00054     k2 = vi[cpi].ni[i];
00055     if (k2==0)
00056       continue;
00057 
00058     if (visited.find(k2)!=visited.end())
00059       continue;
00060 
00061     gi[gisize] = k2;
00062     ++gisize;
00063   }
00064 
00065   if (gisize!=0)
00066   {
00067     // pick one and move forward
00068     k2 = gi[ rand() % gisize ];
00069     mg.path.push_back(k2);
00070     visited.insert(k2);
00071     return;
00072   } 
00073     
00074   // Must backtrack.
00075   assert(mg.path.size()>1);
00076   mg.path.pop_back();
00077 }
00078 

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