So basically this is an algorithm I made for a battleships game. I’m having problems with the while loop, I want to have the code inside the do while loop keep repeating, if the generated values already exist in a multidimensional array, and it looks like it actually works, because it generates correct values, but I keep getting the error “UNDEFINED OFFSET”, why? And yes, I know it’s ugly code, but I’m really just interested in the error, because the program works fine otherwise
$_SESSION["ladjica1"] = array("", "", "", "", "");
$_SESSION["ladjica2"] = array("", "", "", "");
$_SESSION["ladjica3"] = array("", "", "");
$_SESSION["ladjica4"] = array("", "", "");
$_SESSION["ladjica5"] = array("", "");
$_SESSION["ladjica"] = array(
$_SESSION["ladjica1"],
$_SESSION["ladjica2"],
$_SESSION["ladjica3"],
$_SESSION["ladjica4"],
$_SESSION["ladjica5"]
);
function multi_array_iskanje($search_for, $search_in) {
foreach ($search_in as $element) {
if ( ($element === $search_for) || (is_array($element) && multi_array_iskanje($search_for, $element)) ){
return true;
}
}
return false;
}
function generirajLadjo($dolzina, $sirina, $indeksladje) {
do {
$ladjicaROW = rand(1,10)*10-1;
$ladjicaCOL = rand(1,10);
$zacetniPolozaj = $ladjicaROW + $ladjicaCOL;
for($sir = 0; $sir < $sirina; $sir++) {
$HaliV = rand(0,1);
if($HaliV == 1) {
if($ladjicaCOL > $dolzina) {
for($x = 0; $x < $dolzina; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj - $x;
}
} else {
for($x = 0; $x < $dolzina; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj + $x;
}
}
} else {
if(($ladjicaROW > ($dolzina*10)) && $ladjicaROW % 10 != 0) {
for($x = 0; $x < $dolzina; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj - ($x*10);
}
} else {
for($x = 0; $x < 5; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj + ($x*10);
}
}
}
}
} while(multi_array_iskanje($_SESSION["ladjica".strval($indeksladje)][$x], $_SESSION["ladjica"])==true);
}
generirajLadjo(5, 1, 1);
generirajLadjo(4, 1, 2);
generirajLadjo(3, 1, 3);
generirajLadjo(3, 1, 4);
generirajLadjo(2, 1, 5);
Advertisement
Answer
Because a for loop will increment the counter and then test it your do-while limiting statement was using $x = 5
and that blew
$_SESSION["ladjica".strval($indeksladje)][$x]
beyond its bounds.
All I did was $x--;
as the last thing before completing the loop and bingo. No erors.
$_SESSION["ladjica1"] = array("", "", "", "", "");
$_SESSION["ladjica2"] = array("", "", "", "");
$_SESSION["ladjica3"] = array("", "", "");
$_SESSION["ladjica4"] = array("", "", "");
$_SESSION["ladjica5"] = array("", "");
$_SESSION["ladjica"] = array(
$_SESSION["ladjica1"],
$_SESSION["ladjica2"],
$_SESSION["ladjica3"],
$_SESSION["ladjica4"],
$_SESSION["ladjica5"]
);
function multi_array_iskanje($search_for, $search_in)
{
foreach ($search_in as $element) {
if ( ($element === $search_for) || (is_array($element) && multi_array_iskanje($search_for, $element)) ){
return true;
}
}
return false;
}
function generirajLadjo($dolzina, $sirina, $indeksladje)
{
do {
$ladjicaROW = rand(1,10)*10-1;
$ladjicaCOL = rand(1,10);
$zacetniPolozaj = $ladjicaROW + $ladjicaCOL;
for($sir = 0; $sir < $sirina; $sir++) {
$HaliV = rand(0,1);
if($HaliV == 1) {
if($ladjicaCOL > $dolzina) {
for($x = 0; $x < $dolzina; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj - $x;
}
} else {
for($x = 0; $x < $dolzina; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj + $x;
}
}
} else {
if(($ladjicaROW > ($dolzina*10)) && $ladjicaROW % 10 != 0) {
for($x = 0; $x < $dolzina; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj - ($x*10);
}
} else {
for($x = 0; $x < 5; $x++) {
$_SESSION["ladjica".strval($indeksladje)][$x] = $zacetniPolozaj + ($x*10);
}
}
}
}
$x--;
} while(multi_array_iskanje($_SESSION["ladjica".strval($indeksladje)][$x], $_SESSION["ladjica"])==true);
}
generirajLadjo(5, 1, 1);
generirajLadjo(4, 1, 2);
generirajLadjo(3, 1, 3);
generirajLadjo(3, 1, 4);
generirajLadjo(2, 1, 5);
Its a little dodgy controlling an outer loop with the result of something else going on in the loop IMO