forked from mbohlaender/gameOfLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamefield.php
166 lines (145 loc) · 2.61 KB
/
gamefield.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
*
*
* @version $Id$
* @copyright 2011
* @author Marcel BohlŠnder
*/
class GameField
{
private $gameFieldArray;
function __construct($_gameFieldArray=array(
array(" "," ","A"," "," "),
array("A","A"," "," "," "),
array(" "," ","A","A"," "),
array(" ","A"," "," "," "),
array(" "," "," "," "," ")
))
{
$this->gameFieldArray=$_gameFieldArray;
}
/**
* gameField::ColumnsOfGameField()
*
* @return int number of columns
*/
public function columnsOfGameField()
{
return count($this->gameFieldArray[0]);
}
public function setGamefield($_row,$_column)
{
if($_row < 10)
{
$_row=10;
}
if($_row>20)
{
$_row=20;
}
if($_column < 10)
{
$_column=10;
}
if($_column>40)
{
$_column=40;
}
for($i=0;$i<$_row;$i++)
{
for($j=0;$j<$_column;$j++)
{
$this->gameFieldArray[$i][$j]=' ';
}
}
}
public function setGamefieldCell($_row,$_column,$_doa)
{
$this->gameFieldArray[$_row][$_column]=$_doa;
}
public function countA()
{
for($k=0;$k<40;$k++)
{
for($j=0;$j<20;$j++)
{
if ($this->gameFieldArray[$k][$j] == "A") echo "A".$k.'-'.$j;
}
}
echo "\n";
}
/**
* gameField::RowsOfGameField()
*
* @return int number of rows
*/
public function rowsOfGameField()
{
return count($this->gameFieldArray);
}
public function checkDeadOrAlive($_row,$_column)
{
$tempArray = $this->gameFieldArray();
if($tempArray[$_row][$_column]=="A")
{
return "alive";
}
if($tempArray[$_row][$_column]==" ")
{
return "dead";
}
}
public function readFormsOutOfTxtInArray()
{
$rowArray = file ("form/hour.txt",FILE_IGNORE_NEW_LINES);
for($i=0;$i<count($rowArray);$i++)
{
$tempString = $rowArray[$i];
$coords =explode(" ",$tempString,2);
$this->setGamefieldCell($coords[0],$coords[1],"A");
}
}
public function gameFieldArray()
{
return $this->gameFieldArray;
}
/**
* Gives in an array the coordinates from the neighbor cells back
*
* @param int $_row
* @param int $_column
* @return array $neighborHoodPlaces
*/
public function neighborCells($_row,$_column)
{
$tempRow;
$tempColumn;
$neighborHoodPlaces = array();
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
$x=$i-1+$_row;
$y=$j-1+$_column;
if (isset($this->gameFieldArray[$x][$y]))
{ //wert aus gamefield übernehmen
if($x==$_row && $y==$_column)
{
$neighborHoodPlaces[$i][$j]="C";
}
else
{
$neighborHoodPlaces[$i][$j]=$this->gameFieldArray[$x][$y];
}
}
else
{ //n muss rein
$neighborHoodPlaces[$i][$j]="N";
}
}
}
return $neighborHoodPlaces;
}
}
?>