forked from mbohlaender/gameOfLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.php
105 lines (86 loc) · 1.77 KB
/
simulator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
*
*
* @version $Id$
* @copyright 2011
*/
class Simulator{
private $gamefield;
private $gamefieldArrays;
function __construct($_gamefield){
$this->gamefield = $_gamefield;
}
function countDeadOrAliveCells($_row,$_column)
{
$tempArray = $this->gamefield->neighborCells($_row,$_column);
$statisticArray;
$tempAlive=0;
$tempDead=0;
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
if($tempArray[$i][$j]=="A")
{
$tempAlive++;
}
if($tempArray[$i][$j]==" ")
{
$tempDead++;
}
}
}
$statisticArray[0]=$tempAlive;
$statisticArray[1]=$tempDead;
return $statisticArray;
}
function checkDeadOrAlive($_row,$_column)
{
$tempArray = $this->gamefield->gameFieldArray();
if($tempArray[$_row][$_column]=="A")
{
return "alive";
}
if($tempArray[$_row][$_column]==" ")
{
return "dead";
}
}
function simulation(){
$changings;
$counter=0;
$this->gamefieldArrays = $this->gamefield->gameFieldArray();
for($i=0;$i<$this->gamefield->RowsOfGameField();$i++)
{
for($j=0;$j<$this->gamefield->ColumnsOfGameField();$j++)
{
$deadOrAlive=$this->checkDeadOrAlive($i,$j);
$neighbors = $this->countDeadOrAliveCells($i,$j);
if($deadOrAlive=="dead" && $neighbors[0]==3)
{
$changings[$counter]=array($i,$j,"A");
$counter++;
}
elseif($deadOrAlive=="alive" && $neighbors[0]<2)
{
$changings[$counter]=array($i,$j," ");
$counter++;
}
elseif($deadOrAlive=="alive" && $neighbors[0]>3)
{
$changings[$counter]=array($i,$j," ");
$counter++;
}
}
}
for($k=0;$k<$counter;$k++)
{
$temp1=$changings[$k][0];
$temp2=$changings[$k][1];
$temp3=$changings[$k][2];
$this->gamefield->setGamefieldCell($temp1,$temp2,$temp3);
}
}
}
?>