examples/Graphics/WS/Direct/CLifeEngine.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include <e32math.h>
00032 
00033 #include "CLifeEngine.h"
00034 
00035 CLifeEngine::CLifeEngine(const TInt64& aSeed)
00036 : iSeed(aSeed)
00037         {
00038         Reset();
00039         }
00040 
00041 // Sets random initial cell states
00042 void CLifeEngine::Reset()
00043         {
00044         for (TInt y=0; y < DIM_Y_ARRAY; y++)
00045                 for (TInt x=0; x < DIM_X_ARRAY; x++)
00046                         iCellArray[x][y] = (Math::Rand(iSeed) > KMaxTInt/2) ? (TBool)ETrue : (TBool)EFalse;
00047         }
00048 
00049 // Loops through cell array, storing a version altered according to the 
00050 // game of life, in a temporary array
00051 void CLifeEngine::AddGeneration()
00052         {
00053         TInt numNeighbours;
00054         
00055         for (TInt y=0; y < DIM_Y_ARRAY; y++)
00056                 {
00057                 for (TInt x=0; x < DIM_X_ARRAY; x++)
00058                         {
00059                         numNeighbours=NumNeighbors(x,y);
00060                         
00061                         if (iCellArray[x][y])
00062                                 // Filled cell
00063                                 {
00064                                 if ((numNeighbours == 2) || (numNeighbours == 3))
00065                                         iTempCellArray[x][y]=ETrue;
00066                                 else
00067                                         iTempCellArray[x][y]=EFalse;
00068                                 }
00069                         else
00070                                 // Empty cell
00071                                 {
00072                                 if (numNeighbours == 3)
00073                                         iTempCellArray[x][y]=ETrue;
00074                                 else
00075                                         iTempCellArray[x][y]=EFalse;
00076                                 }
00077                         }
00078                 }
00079         
00080         for (TInt y2 =0; y2 < DIM_Y_ARRAY; y2++)
00081                 for (TInt x2=0; x2 < DIM_X_ARRAY; x2++)
00082                         iCellArray[x2][y2]=iTempCellArray[x2][y2];
00083         }
00084 
00085 
00086 // Gets the number of adjacent cells to the specified cell
00087 TInt CLifeEngine::NumNeighbors(TInt x, TInt y)
00088         {
00089         TInt numNeighbors=0;
00090         TInt i =0;
00091 
00092         // Get neighbors to the left
00093         if ((x-1) >= 0)
00094                 for (i=y-1; i <= y+1; i++)
00095                         if ((i >= 0) && (i < DIM_Y_ARRAY))
00096                                 if (iCellArray[x-1][i])
00097                                         numNeighbors++;
00098         
00099         // Get neighbors to the right
00100         if (x+1 < DIM_X_ARRAY)
00101                 for (i=y-1; i <= y+1; i++)
00102                         if ((i >= 0) && (i < DIM_Y_ARRAY))
00103                                 if (iCellArray[x+1][i])
00104                                         numNeighbors++;
00105         
00106         // Get neighbors straight above
00107         if ((y-1) >= 0)
00108                 if (iCellArray[x][y-1])
00109                         numNeighbors++;
00110         
00111         // Get neighbors straight below
00112         if ((y+1) < DIM_Y_ARRAY)
00113                 if (iCellArray[x][y+1])
00114                         numNeighbors++;
00115         
00116         return numNeighbors;
00117         }

Generated by  doxygen 1.6.2