Another solution for undefined offset(Conway's game of Life)
Posted: Tue Aug 16, 2011 9:09 am
I am a beginner in PHP coding and currently I am attempting to code Conway's game of life which can be found in viewtopic.php?f=1&t=131031. I'm not sure if I should open a new thread regarding the same topic but I don't wanna steal someone's thread so I apologise if I created a pointless post. Currently, I am facing some issues with undefined offset errors which resulted from my code accessing values that doesn't exist.
Here is my entire code that I've came up with so far:
As you can see from
If $currentboard[$i][$j] == $currentboard[0,0],the code will search for $currentboard[-1][-1] that does not exist, resulting in an undefined offset.
I do know how to solve the undefined offset issue, but it would require specific codes for values at the borders of the board, which will be rather tedious to code.
I've tried but still the code tries to access $currentboard[-1][-1].
I hope someone can help me with this and please give me opinions about my codes. Any help would be gladly appreciated!!! Thanks in advance!

Here is my entire code that I've came up with so far:
Code: Select all
<?php
$initialBoard = generateFirstConfiguration();
printConfiguration($initialBoard);
$initialBoardClone = $initialBoard;
$occurrences = rand(5,20);
$countLive = countinitialLive($initialBoard);
$countdead = 256 - $countLive;
print "Total number of initial live cells: " . $countLive . "<br/>";
print "Total number of initial dead cells: " . $countdead . "<br/>";
print "Number of times simulation should run: " . $occurrences . "<br/>";
function printConfiguration($initialBoard)
{
for ($i = 0; $i <16; $i++)
{
for($j = 0; $j <16; $j++)
{
print $initialBoard[$i][$j];
}
print "<br/>";
}
}
function generateFirstConfiguration()
{
for ($i = 0; $i < 16; $i++)
{
for($j = 0; $j < 16; $j++)
{
$initialBoard[$i][$j] = rand(0,1);
}
}
return $initialBoard;
}
function countinitialLive($initialBoard)
{
$countLive = 0;
for ($i = 0; $i < 16; $i++)
{
for($j = 0; $j < 16; $j++)
{
if ($initialBoard[$i][$j] == 1)
$countLive++;
}
} return $countLive++;
}
function generateNextConfiguration($initialBoard, $currentBoard)
{
global $initialBoard;
global $occurrences;
global $initialBoardClone;
global $surroundingValues;
global $currentBoard;
$countOne = 0;
$countZero = 0;
for ($i = 0; $i < 16; $i++)
{
for($j = 0; $j < 16; $j++)
{
if($initialBoard = $initialBoardClone)
$currentBoard = $initialBoard;
for($i = 1; $i < 15; $i++)
{
for($j = 1; $j < 15; $j++)
{
for ($a = 0; $a < count($surroundingValues); $a++)
{
$surroundingValues = array(
$currentBoard[$i-1][$j-1],$currentBoard[$i-1][$j],
$currentBoard[$i-1][$j+1],$currentBoard[$i][$j-1],
$currentBoard[$i][$j+1],$currentBoard[$i+1][$j-1],
$currentBoard[$i+1][$j],$currentBoard[$i+1][$j+1]);
if($surroundingValues[$a] <= 1)
$countOne += 1;
else
$countZero += 1;
}
if ($countOne > 3 && $surroundingValues[$a] = 0 || $countOne > 3 && $surroundingValues[$a] = 1)
$currentBoard[$i][$j] = 0;
elseif ($countOne == 3 && $surroundingValues[$a] = 0 || $countOne == 3 && $surroundingValues[$a] = 1)
$currentBoard[$i][$j] = 1;
elseif ($countOne == 2 && $surroundingValues[$a] = 2)
$currentBoard[$i][$j] = 0;
elseif ($countOne == 2 && $surroundingValues[$a] = 1)
$currentBoard[$i][$j] = 1;
else
$currentBoard[$i][$j] = 0;
print $currentBoard[$i][$j];
}
print $currentBoard[$i][$j];
}
}
print "<br/>";
}
}
?>
}Code: Select all
$surroundingValues = array(
$currentBoard[$i-1][$j-1],$currentBoard[$i-1][$j],
$currentBoard[$i-1][$j+1],$currentBoard[$i][$j-1],
$currentBoard[$i][$j+1],$currentBoard[$i+1][$j-1],
$currentBoard[$i+1][$j],$currentBoard[$i+1][$j+1]);
I do know how to solve the undefined offset issue, but it would require specific codes for values at the borders of the board, which will be rather tedious to code.
I've tried
Code: Select all
if $surroundingValues[$a] != 0 || $surroundingValues[$a] != 1I hope someone can help me with this and please give me opinions about my codes. Any help would be gladly appreciated!!! Thanks in advance!