如何将 table 的坐标插入 PHP 中的数组

How to insert coordinates of a table into an array in PHP

我在PHP制作游戏战舰,我需要制作它让计算机随机选择5个坐标(x,y),并且它们必须在一条直线上(列或行) ).如何使坐标在一条直线上以及如何将坐标连接到 table 个单元格?

这是我目前的情况:

<?php

session_start();


$i=0;
$start=1;
$st=65;
$l=0;
$polje=array();
$x=(mt_rand(0, 10));
$y=(mt_rand(0, 10));

for($q=0; $q<5; $q++){
$polje[$l]=array($x, $y);
}


echo "<table id='table'>\n";


while ($i<11){
    $ascii=chr($st);
    echo "<tr>";
    for($k=0; $k<11; $k++, $start++){
        if($k==0){
            echo "<td>$i</td>";
        }
        else if($i==0){
            echo "<td class='td2'>$ascii</td>";
            $ascii++;
        }
        else{
            echo "<td class='td1'> </td>";
        }

    }
    echo "</tr>";
    $i++;
}




?>

这就是 table 的样子:

我喜欢这种挑战。 当然,有很多方法,更好的方法可以使用 OOP 来做到这一点,但您可以理解要点。 通过代码阅读注释,如果您有任何疑问,请随时提出。 我做了一些处理,避免船只相互重叠,随机部署。

const BOARD_SIZE = 10; //10x10 board
const SUBMARINE = 1;
const DESTROYER = 2;
const BATTLESHIP = 3;
const AIRCRAFTCARRIER = 4;

$ships_available = [];
//populate with some data based on game rule
array_push($ships_available, SUBMARINE);
array_push($ships_available, SUBMARINE);
array_push($ships_available, DESTROYER);
array_push($ships_available, DESTROYER);
array_push($ships_available, DESTROYER);
array_push($ships_available, BATTLESHIP);
array_push($ships_available, BATTLESHIP);
array_push($ships_available, AIRCRAFTCARRIER);

$board = [];

while(count($ships_available) > 0) {
    deployShip($board, $ships_available);
}

$lastColumnLetter = chr(ord('A')+ BOARD_SIZE-1);

//print table
echo "<table border='1' align='center'>";
echo "<tr><td></td><td>".implode('</td><td>',range(1, BOARD_SIZE))."</td></tr>";
for($i = 'A'; $i <= $lastColumnLetter; $i++) {
    echo "<tr><td>".$i."</td>";
    for($j=0; $j < BOARD_SIZE; $j++) {
        echo "<td align='center'>";
        echo $board[$i][$j] ?: '&nbsp;&nbsp;&nbsp;&nbsp;';
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";
exit;

function array_merge_custom($array1, $array2) {
    foreach($array2 as $key => $value) {
        foreach($value as $k => $v) {
            $array1[$key][$k] = $v;
        }
    }
    return $array1;
}

function deployShip(&$board, &$ships_available) {
    $randomShipKey = array_rand($ships_available);
    $ship = $ships_available[$randomShipKey];
    $beginCoordinates = getRandomCoordinates();
    $coordinates = getShipCoordinates($board, $beginCoordinates, $ship);

    if(!$coordinates) {
        return false;
    }

    unset($ships_available[$randomShipKey]);
    $board = array_merge_custom($board,$coordinates);
}

function getRowNumberByLetter($letter) {
    return array_search($letter, range('A','Z'));
}

function getRowLetterByNumber($number) {
    $letters = range('A','Z');
    return $letters[$number];
}

function getRandomCoordinates() {
    return ['row' => chr(mt_rand(ord('A'), (ord('A') + BOARD_SIZE-1))), 'col' => mt_rand(0,BOARD_SIZE -1)];
}

function getShipCoordinates($board, $beginCoordinates, $ship) {
    if(isset($board[$beginCoordinates['row']][$beginCoordinates['col']])) {
        return false; //anchor position already taken
    }
    if($ship == 1) {
        $return[$beginCoordinates['row']][$beginCoordinates['col']] = 1;
        return $return;
    }

    $shipArraySize = $ship -1;
    $directions = ['left', 'right', 'up', 'down'];
    $tries = 10;
    while($tries > 0) {
        $tries--;
        $direction = $directions[array_rand($directions)];
        $return = [];
        switch($direction) {
            case 'left':
                if(($beginCoordinates['col'] - $shipArraySize) < 0) { //check if can go left
                    break;
                }
                for($colBegin = ($beginCoordinates['col'] - $shipArraySize), $colEnd = $beginCoordinates['col']; $colBegin <= $colEnd; $colBegin++) {
                    if(!empty($board[$beginCoordinates['row']][$colBegin])) {
                        break 2;
                    } else {
                        $return[$beginCoordinates['row']][$colBegin] = $ship;
                    }
                }
                return $return;
            case 'right':
                if(($beginCoordinates['col'] + $shipArraySize) > BOARD_SIZE -1) { //check if can go right
                    break;
                }
                for($colBegin = $beginCoordinates['col'], $colEnd = ($beginCoordinates['col'] + $shipArraySize); $colBegin <= $colEnd; $colBegin++) {
                    if(!empty($board[$beginCoordinates['row']][$colEnd])) {
                        break 2;
                    } else {
                        $return[$beginCoordinates['row']][$colBegin] = $ship;
                    }
                }
                return $return;
            case 'up':
                if((getRowNumberByLetter($beginCoordinates['row']) - $shipArraySize) < 0) { //check if can go up
                    break;
                }
                for($rowBegin = (getRowNumberByLetter($beginCoordinates['row']) - $shipArraySize), $rowEnd = getRowNumberByLetter($beginCoordinates['row']); $rowBegin <= $rowEnd; $rowBegin++) {
                    if(!empty($board[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']])) {
                        break 2;
                    } else {
                        $return[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']] = $ship;
                    }
                }
                return $return;
            case 'down':
                if((getRowNumberByLetter($beginCoordinates['row']) + $shipArraySize) > BOARD_SIZE -1) { //check if can go down
                    break;
                }
                for($rowBegin = getRowNumberByLetter($beginCoordinates['row']), $rowEnd = (getRowNumberByLetter($beginCoordinates['row']) + $shipArraySize); $rowBegin <= $rowEnd; $rowBegin++) {
                    if(!empty($board[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']])) {
                        break 2;
                    } else {
                        $return[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']] = $ship;
                    }
                }
                return $return;
        }
    }

    return false;
}