Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中将表的坐标插入数组_Php_Arrays_Html Table_Coordinates_Cells - Fatal编程技术网

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

如何在PHP中将表的坐标插入数组,php,arrays,html-table,coordinates,cells,Php,Arrays,Html Table,Coordinates,Cells,我正在用PHP制作战列舰游戏,我需要让计算机随机选择5个坐标(x,y),它们必须在一条直线上(列或行)。如何使坐标在一条线上,如何将坐标连接到表格单元格 以下是我目前掌握的情况: <?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

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

以下是我目前掌握的情况:

<?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++;
}




?>

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

const BOARD\u SIZE=10//10x10板
常数潜艇=1;
常数驱逐舰=2;
康斯特战舰=3;
常数航空母舰=4架;
$ships_可用=[];
//根据游戏规则填充一些数据
阵列推进(船可用,潜艇);
阵列推进(船可用,潜艇);
阵列推进($ships\u可用,驱逐舰);
阵列推进($ships\u可用,驱逐舰);
阵列推进($ships\u可用,驱逐舰);
阵列推进($战舰可用,战列舰);
阵列推进($战舰可用,战列舰);
阵列推送(可用船舶,航空母舰);
$board=[];
而(计数($ships\u可用)>0){
部署船($board,$ships\u可用);
}
$lastColumnLetter=chr(ord('A')+电路板尺寸1);
//打印表
回声“;
回波“.内爆('',范围(1,电路板大小))”;
对于($i='A';$i$value){
foreach($k=>v的值){
$array1[$key][$k]=$v;
}
}
返回$array1;
}
功能部署船(&$board,&$ships\U可用){
$randomShipKey=array\u rand($ships\u可用);
$ship=$ships_可用[$randomShipKey];
$beginCoordinates=getRandomCoordinates();
$coordinates=getShipCoordinates($board,$beginCoordinates,$ship);
如果(!$坐标){
返回false;
}
未设置($ships_可用[$randomShipKey]);
$board=数组\合并\自定义($board$coordinates);
}
函数getRowNumberByLetter($letter){
返回数组搜索($letter,range('A','Z'));
}
函数getRowLetterByNumber($number){
$letters=范围('A','Z');
返回$letters[$number];
}
函数getRandomCoordinates(){
返回['row'=>chr(mt_rand(ord('A'),(ord('A')+线路板尺寸-1)),'col'=>mt_rand(0,线路板尺寸-1)];
}
函数getShipCoordinates($board、$beginCoordinates、$ship){
如果(isset($board[$beginCoordinates['row']][$beginCoordinates['col']])){
return false;//已采取锚定位置
}
如果($ship==1){
$return[$beginCoordinates['row'][$beginCoordinates['col']]]=1;
return$return;
}
$shipArraySize=$ship-1;
$directions=[‘左’、‘右’、‘上’、‘下’];
$trys=10;
而($trys>0){
美元尝试--;
$direction=$directions[array_rand($directions)];
$return=[];
开关($方向){
案例“左”:
如果($beginCoordinates['col']-$shipArraySize)<0){//检查是否可以向左移动
打破
}
对于($colBegin=($beginCoordinates['col']-$shiparaysize),$colEnd=$beginCoordinates['col'];$colbeginboard_SIZE-1){//检查是否可以正常运行
打破
}

对于($colBegin=$beginCoordinates['col'],$colEnd=($beginCoordinates['col']+$shipArraySize)这真的是一个数学问题。这应该会有帮助:不要选择5个随机位置,选择一个用于弓,然后选择一个随机方向。不仅仅是数学上的,我也不知道如何连接随机坐标和表格单元格。我想你需要做更多的研究。你要求我们为你做很多。我只是我想知道用这些坐标连接表格单元格的最佳方法是什么,不需要任何人真正写出全部源代码,我只是不知道如何连接这些元素。你是个天才,我的朋友。谢谢
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;
}