Reply to Pass by reference question

Your name:

Reply:


Posted by Adam on 10/10/06 01:32

I am creating a program to create sudoku games. I have an object that
is 1 row of a sudoku game. I store the initial state of the row before
I start figuring out the values to place in each cell, in case the
program runs into a problem and has to revert the row back to its
original state.

So first I store the initial State of the row, then I start to modify
the row. My problem is that once I start to change the original row,
it also changes the initial state that I saved. The only thing I can
think that is doing this is that instead of creating a copy of the
object's data in my initial state variable, it is just changing its
pointer to the original rows location.

I wrote a smaller test case to show what I am talking about. At the
end of the program, each cell in the initial state should be full of
numbers 1 to 9. Instead, it is a copy of the current state of the row.


Hopefully someone can clue me in on what I am missing? You can see the
output of the program here
http://mustang.millersville.edu/~arisser/sudoku/test.php

Thanks again for the help!!

<?php
###########################################################

class Cell {
public $c = array(1,2,3,4,5,6,7,8,9);

//_____________________________________________________

public function getCandidates() {
return $this->c;
}

//_____________________________________________________

public function pickRandomCandidate() {
return $this->c[rand() % count($this->c)];
}

//_____________________________________________________

public function setCandidate($a) {
$this->c = array($a);
}

//_____________________________________________________

public function removeCandidateFromCell($v) {
$this->c = array_values(array_diff($this->c,array($v)));
}
}

###########################################################

class Row {
public $cells = array();
public $initialState = array();

//_____________________________________________________

public function __construct() {
for ($i=0;$i<9;$i++)
$this->cells[$i] = new Cell();
}

//_____________________________________________________

public function getInitialState() {
return $this->initialState;
}

public function getCurrentState() {
return $this->cells;
}

//_____________________________________________________

public function setInitialState() {
$this->initialState = $this->getCurrentState();
}

//_____________________________________________________

public function printRow() {
echo "<strong>Printing Row State</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->cells as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";

echo"</table>";
}

//_____________________________________________________

public function printInitialState($rowNum = "") {
echo "<strong>Initial State of $rowNum</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->initialState as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";
echo"</table>";
}
//_____________________________________________________

public function resetRow($rowNum = "") {
echo "resetting row $rowNum <br />";
$this->cells = $this->getInitialState();
#$this->cells = $this->initialState;
}

//_____________________________________________________

public function removeCandidateFromRow($v) {
foreach($this->cells as $c => $val) {
$val->removeCandidateFromCell($v);
}
}
}

$myrow = new Row();

$myrow->setInitialState();

$candidate = $myrow->cells[0]->pickRandomCandidate();

$myrow ->removeCandidateFromRow($candidate);
$myrow->cells[0]->setCandidate($candidate);

$myrow->printRow();
$myrow->printInitialState();
?>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация