00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // 00015 00016 #include <e32math.h> 00017 00018 #include "CLifeEngine.h" 00019 00020 CLifeEngine::CLifeEngine(const TInt64& aSeed) 00021 : iSeed(aSeed) 00022 { 00023 Reset(); 00024 } 00025 00026 // Sets random initial cell states 00027 void CLifeEngine::Reset() 00028 { 00029 for (TInt y=0; y < DIM_Y_ARRAY; y++) 00030 for (TInt x=0; x < DIM_X_ARRAY; x++) 00031 iCellArray[x][y] = (Math::Rand(iSeed) > KMaxTInt/2) ? (TBool)ETrue : (TBool)EFalse; 00032 } 00033 00034 // Loops through cell array, storing a version altered according to the 00035 // game of life, in a temporary array 00036 void CLifeEngine::AddGeneration() 00037 { 00038 TInt numNeighbours; 00039 00040 for (TInt y=0; y < DIM_Y_ARRAY; y++) 00041 { 00042 for (TInt x=0; x < DIM_X_ARRAY; x++) 00043 { 00044 numNeighbours=NumNeighbors(x,y); 00045 00046 if (iCellArray[x][y]) 00047 // Filled cell 00048 { 00049 if ((numNeighbours == 2) || (numNeighbours == 3)) 00050 iTempCellArray[x][y]=ETrue; 00051 else 00052 iTempCellArray[x][y]=EFalse; 00053 } 00054 else 00055 // Empty cell 00056 { 00057 if (numNeighbours == 3) 00058 iTempCellArray[x][y]=ETrue; 00059 else 00060 iTempCellArray[x][y]=EFalse; 00061 } 00062 } 00063 } 00064 00065 for (TInt y2 =0; y2 < DIM_Y_ARRAY; y2++) 00066 for (TInt x2=0; x2 < DIM_X_ARRAY; x2++) 00067 iCellArray[x2][y2]=iTempCellArray[x2][y2]; 00068 } 00069 00070 00071 // Gets the number of adjacent cells to the specified cell 00072 TInt CLifeEngine::NumNeighbors(TInt x, TInt y) 00073 { 00074 TInt numNeighbors=0; 00075 TInt i =0; 00076 00077 // Get neighbors to the left 00078 if ((x-1) >= 0) 00079 for (i=y-1; i <= y+1; i++) 00080 if ((i >= 0) && (i < DIM_Y_ARRAY)) 00081 if (iCellArray[x-1][i]) 00082 numNeighbors++; 00083 00084 // Get neighbors to the right 00085 if (x+1 < DIM_X_ARRAY) 00086 for (i=y-1; i <= y+1; i++) 00087 if ((i >= 0) && (i < DIM_Y_ARRAY)) 00088 if (iCellArray[x+1][i]) 00089 numNeighbors++; 00090 00091 // Get neighbors straight above 00092 if ((y-1) >= 0) 00093 if (iCellArray[x][y-1]) 00094 numNeighbors++; 00095 00096 // Get neighbors straight below 00097 if ((y+1) < DIM_Y_ARRAY) 00098 if (iCellArray[x][y+1]) 00099 numNeighbors++; 00100 00101 return numNeighbors; 00102 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.