From 518619603ab3c49b7fdfcf19c439c1a30668164f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 2 Apr 2015 15:28:31 -0600 Subject: Everything works, it could use more stuff. --- src/worm.cpp | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/worm.cpp (limited to 'src/worm.cpp') diff --git a/src/worm.cpp b/src/worm.cpp new file mode 100644 index 0000000..31be54d --- /dev/null +++ b/src/worm.cpp @@ -0,0 +1,124 @@ +#include "worm.h" +#include "map.h" +#include "cell.h" + +#include + +Worm::Worm( int iId, const Position &rpStart, Map &rMap ) : + iId( iId ), + pCur( rpStart ), + rMap( rMap ), + iDist( 1 ) +{ + Cell &rCur = rMap[pCur]; + rCur.iDist = iDist; + rCur.iPath = iId; + + int iCount = 0; + int *iDirs = new int[2]; + if( pCur[0] == 0 ) + iDirs[iCount++] = 1; + else if( pCur[0] == rMap.getSize(0)-1 ) + iDirs[iCount++] = 2; + if( pCur[1] == 0 ) + iDirs[iCount++] = 4; + else if( pCur[1] == rMap.getSize(1)-1 ) + iDirs[iCount++] = 8; + + rCur.iWalls |= iDirs[rand()%iCount]; + delete[] iDirs; +} + +Worm::~Worm() +{ +} + +int opposite( int iDir ) +{ + if( iDir%2 == 1 ) + return iDir-1; + return iDir+1; +} + +bool Worm::timestep() +{ + int iDims = rMap.getDims(); + int iCount = 0; + Position *pDirs = new Position[iDims*2]; + int *iDirs = new int[iDims*2]; + + for(;;) + { + iCount = 0; + bool bFoundBack = false; + Position pBack; + for( int j = 0; j < iDims; j++ ) + { + int iSize = rMap.getSize( j ); + Position p1( pCur ); + p1[j]--; + if( p1[j] >= 0 ) + { + if( rMap[p1].iPath == 0 ) + { + iDirs[iCount] = j*2; + pDirs[iCount++] = p1; + } + else if( rMap[p1].iPath == iId && rMap[p1].iDist == iDist-1 ) + { + pBack = p1; + bFoundBack = true; + } + } + Position p2( pCur ); + p2[j]++; + if( p2[j] < iSize ) + { + if( rMap[p2].iPath == 0 ) + { + iDirs[iCount] = j*2+1; + pDirs[iCount++] = p2; + } + else if( rMap[p2].iPath == iId && rMap[p2].iDist == iDist-1 ) + { + pBack = p2; + bFoundBack = true; + } + } + } + + if( iCount > 0 ) + { + break; + } + else + { + if( bFoundBack ) + { + pCur = pBack; + iDist--; + } + else + { + delete[] pDirs; + delete[] iDirs; + return false; + } + } + } + + int iSel = rand()%iCount; + Cell &rCur = rMap[pCur]; + rCur.iWalls |= (1<