$con) { foreach($con as $y => $value) { $i .= $value; } $i .= "\r\n"; } file_put_contents($filename,$i); } // Funktsioon mis joonistab mänguväljaku function drawField($playfieldArray) { foreach($playfieldArray as $key => $location) { echo "$key )"; foreach($location as $id => $s) { echo "$s"; } echo "
"; } } // Funktsioon mille abil saame teatud kohta accessida. function getPoint($x,$y,$playfieldArray) { return $playfieldArray[$x][$y]; } // Funktsioon mis määrab ära kas tegu on horisontaalse või vertikaalse laevaga // 1 = horisontaalne // 2 = vertikaalne // 3 = üksikRuut // -1 INVALID function getShipType($x,$y,$playfieldArray) { if(getPoint($x,$y,$playfieldArray) != "#") return -1; if(($i = getHorizontalPointY($x,$y,$playfieldArray)) && getPoint($x,$i+1,$playfieldArray) == "#") // meil on tegu horisontaalse laevaga return 1; elseif(($i = getVerticalPointX($x,$y,$playfieldArray)) && getPoint($i+1,$y,$playfieldArray) == "#") return 2; else return 3; } // -2 = Meil on juba laeva tükk horisontaalne mis on ainult 1 kohaline, järelikult on ta // 1) Ühe tüki laev // 2) Vertikaalne laev // -3 = Meil pole tegu üldselt laeva tükiga function getHorizontalPointY($x,$y,$playfieldArray) { if(getPoint($x,$y,$playfieldArray) != "#") return -3; if(getPoint($x,$y-1,$playfieldArray) != "#") return $y; for($y1 = $y; $y1 != 0 && getPoint($x,$y1,$playfieldArray) == "#"; --$y1); return $y1+1; } // function getVerticalPointX($x,$y,$playfieldArray) { if(getPoint($x,$y,$playfieldArray) != "#") return -3; if(getPoint($x-1,$y,$playfieldArray) != "#") return $x; for($x1 = $x; ($x1 != 0 && $y != 0) && getPoint($x1,$y,$playfieldArray) == "#"; --$x1); return $x1+1; } // function getShipLength($shipType, $x,$y,$playfieldArray) { if($shipType != -1) { if($shipType == 2) { // Vertikaalne laev $x = getVerticalPointX($x,$y,$playfieldArray); for($x1 = $x,$c = 0; getPoint($x1,$y,$playfieldArray) == "#"; $x1++,$c++); } elseif($shipType == 1) { // Horisontaalne laev $y = getHorizontalPointY($x,$y,$playfieldArray); for($y1 = $y,$c = 0; getPoint($x,$y1,$playfieldArray) == "#"; $y1++,$c++); } elseif($shipType == 3) $c = 1; return $c; } } // function countShips($ship_number,$covered_ships) { $i = 0; foreach($covered_ships as $x => $content) { foreach($content as $y => $val) if($val == $ship_number) $i++; } return $i; } // function getAvaiblityShipPosVertical($missing, $ar) { // Niisiis kontrollime kõik ruudud läbi kuhu saaks panna "Horisontaalse laeva" for($x = 0, $count = count($ar); $x < $count; $x++) { for($y = 0, $count2 = count($ar[0]); $y < $count2; $y++) { if(getPoint($x,$y,$ar) == ".") { // liigume vasakult paremale + ülevalt alla for($x1 = $x, $y1 = $y, $len = 0; getPoint($x1,$y1,$ar) != "#" && getPoint($x1,$y1+1,$ar) != "#" && getPoint($x1+1,$y1,$ar) == "."; $y1++,$len++); if($len == $missing || $len > $missing) { $shipPoint['x'] = $x; $shipPoint['y'] = $y; break 2; } } } } // END return isset($shipPoint) ? $shipPoint : -1; } function getAvaiblityShipPosHorizontal($missing,$ar) { // Niisiis kontrollime kõik ruudud läbi kuhu saaks panna "Horisontaalse laeva" for($x = 0, $count = count($ar); $x < $count; $x++) { for($y = 0, $count2 = count($ar[0]); $y < $count2; $y++) { if(getPoint($x,$y,$ar) == ".") { // liigume vasakult paremale for($x1 = $x, $y1 = $y, $len = 0; getPoint($x1,$y1,$ar) == "." && getPoint($x1,$y1-1,$ar) != "#" && getPoint($x1,$y1+1,$ar) != "#" && getPoint($x1+1,$y1,$ar) != "#"; $y1++,$len++); if($len == $missing || $len > $missing) { $shipPoint['x'] = $x; $shipPoint['y'] = $y; break 2; } } } } // END return isset($shipPoint) ? $shipPoint : -1; } $ar = convertStringFieldToArrayField(file_get_contents("laev.sis")); // $covered_ships $covered_ships[] = array(); for($x = 0,$count = count($ar); $x < $count; $x++) { for($y = 0, $countx2 = count($ar[0]); $y < $countx2; $y++) { if(getPoint($x,$y,$ar) == "#" && !isset($covered_ships[getVerticalPointX($x,$y,$ar)][getHorizontalPointY($x,$y,$ar)])) { $shipType = getShipType($x,$y,$ar); $shipType = getShipLength($shipType,$x,$y,$ar); if($shipType > 0) $covered_ships[getVerticalPointX($x,$y,$ar)][getHorizontalPointY($x,$y,$ar)] = $shipType; } } } // Loeme nüüd üle kõik ilusasti. $missing = 0; // EHK NONE if(countShips(1,$covered_ships) != 4) $missing = 1; elseif(countShips(2,$covered_ships) != 3) $missing = 2; elseif(countShips(3,$covered_ships) != 2) $missing = 3; elseif(countShips(4,$covered_ships) != 1) $missing = 4; else $missing = 0; // Meil on olemas $missing mis näitab mis laev meil kadunud on. // Järgmiseks tuleb veits haxida <3 $shipPoint = getAvaiblityShipPosHorizontal($missing, $ar); // if(is_array($shipPoint)) { // kirjutame faili siis for($a = 0; $a != $missing; $a++,$shipPoint['y']++) { $ar[$shipPoint['x']][$shipPoint['y']] = "#"; } } else { $shipPoint = getAvaiblityShipPosVertical($missing, $ar); if(is_array($shipPoint)) { for($a = 0; $a != $missing; $a++,$shipPoint['x']++) { $ar[$shipPoint['x']][$shipPoint['y']] = "#"; } } } saveField("laev.val",$ar); ?>