0

PHPを使ってワードサーチゲームを作ろうとしています。最初にテーブル/グリッドを作成し、テーブルにランダムな文字を入力します。次に、ランダムな文字を単語の文字に置き換え、単語の方向を HORIZONTAL、VERTICAL、または DIAGONAL のいずれかで決定します。問題は、単語の文字が互いに交差し、表がめちゃくちゃになることです。質問は、

  • 単語の文字が交差しない条件の設定方法
  • 現在の位置が他の単語文字によって既に占有されているかどうかを判断する方法は?

互いに交差し続ける単語の文字に問題があります。何か案が?

$row = 5;
$col = 5;

$characters = range('a','z');

        $max = count($characters) - 1;  
        $rc = array();

        for ($r=1;$r<=$row;$r++) 
        {
            for ($c=1;$c<=$col;$c++) 
           {
                $rc['r'.$r.'c'.$c] = $characters[mt_rand(0,$max)];
                $fill['r'.$r.'c'.$c] = '';

            }
        }

        $directions = array('H', 'V', 'D');

        $wrdList =array('four', 'data', 'howl');

        foreach ($wrdList as $wrd) 
        {

            $wrdLen = strlen($wrd);
            $dir = $directions[mt_rand(0,2)];

            if ($dir =="H" or $dir=="D" )
            {

                $limitRow = $row - $wrdLen+1;
                $limitCol =  $col - $wrdLen+1;
                $startPointRow = 1;
                $startPointCol = 1;

            }

            elseif ($dir=="V")
            {
                $limitRow = $row  - $wrdLen + 1;
                $limitCol =  $col;
                $startPointRow = 1;
                $startPointCol = 1; 
            }

            $temprow = mt_rand($startPointRow,$limitRow);
            $tempcol =  mt_rand($startPointCol,$limitCol);  


            while($wrdLen >0)
            {

                $thisChar= substr($wrd,0,1);
                $wrd = substr($wrd,1);
                $wrdLen--;  

                $x = 'r'.$temprow.'c'.$tempcol;


                $rc[$x] = $thisChar;


                $fill[$x] = '#2952f8';

                if($dir=="D")
                {
                    $tempcol++;
                    $temprow++;
                }
                elseif($dir=="V")
                {
                    $temprow++;

                }
                elseif($dir=="H")
                {

                    $tempcol++;
                }

            }

        }


        #--Display the random letters and the words
        echo '<table style="border:1px solid #000">';
        for ($r=1;$r<=$row;$r++) 
        {
            echo '<tr style="border:1px solid #000">';
            for ($c=1;$c<=$col;$c++) 
            {
                $thisChar=$rc['r'.$r.'c'.$c]; 
                $fills = $fill['r'.$r.'c'.$c];

                echo '<td style="border:1px solid #000; background-color: '.$fills.'">';
                echo $thisChar;
                echo '</td>';

           }
            echo '</tr>';           
        }
        echo '</table>';
?>
4

1 に答える 1

0

あなたはそれを逆にやっています。最初に好きな言葉を入れてから、ランダムな文字を入れてください。

連続する各単語を入力する前に、その単語のランダムなパスを選択し、そのパスに沿って、一致しない文字がないことを確認します。(たとえば、'alphabet' が単語 'graph' と交差し、両方に文字 'a' がある場合は問題ありません。そうでない場合は、'alphabet' の別の場所を見つけてください)。最後に、すべての単語が配置されたら、全体を調べて、文字がない場所にランダムな文字を入れます. または、すべての単語の場所が見つからない場合は、最初からやり直してください。

編集:

特定の場所で文字を見つける方法。わかった。したがって、コードを見ると、ここで非論理的なことをしています。

$rc['r'.$r.'c'.$c] = $characters[mt_rand(0,$max)];

基本的に、2 つのキーの積から 1 次元配列を作成しています。代わりに、2 次元配列を実行する必要があります。a) 2 つのキーがあり、b) 単語検索には 2 つの次元があるため、これは完全に理にかなっています。

$rc[$r][$c] = $characters[mt_rand(0,$max)];

それでは、すべてを再配置することから始めましょう。行/列のカウントを 1 ではなく 0 から開始するように書き直したことに注意してください。これは、配列のプログラミング規則であるためです。これだけのために、1 からカウントすることに頭を悩ませるつもりはありません。

$wrdList =array('four', 'banana', 'howl');
$row = 5;  //six rows counting from 0
$col = 5;  //six columns counting from 0

$rc = array();  //our tableau array
$directions = array('H', 'V', 'D');

foreach ($wrdList as $wrd)
{
    $found = false;  // by default, no spot has been found
    $tries = $row*$col; // we will try a reasonable number of times to find a spot

    while(!$found && $tries > 0) {
        $wrdLen = strlen($wrd);
        $dir = $directions[mt_rand(0,2)];

        if ($dir =="H")
        {
            $limitRow = $row;
            $limitCol =  $col - ($wrdLen - 1);
        }
        elseif($dir=="D")
        {
            $limitRow = $row - ($wrdLen - 1);
            $limitCol =  $col - ($wrdLen - 1);
        }
        elseif ($dir=="V")
        {
            $limitRow = $row  - ($wrdLen - 1);
            $limitCol =  $col;
        }

        $temprow = mt_rand(0,$limitRow);
        $tempcol = mt_rand(0,$limitCol);

        //this is my temporary placement array
        $placement = array();

        //let's use for loop so we can capitalize on having numeric keys
        $r = $temprow;
        $c = $tempcol;
        for($w = 0; $w < $wrdLen; $w++) {
            $thisChar = $wrd{$w};

            //find array keys
            if($dir == 'V' || $dir == 'D') {
                $r = $temprow + $w;
            }
            if($dir == 'H' || $dir == 'D') {
                $c = $tempcol + $w;
            }

            //look at the current tableau
            if(isset($rc[$r][$c])) {  //the intended spot has a letter
                if($rc[$r][$c] == $thisChar) {  //the intended spot's letter is the same
                    $placement[$r][$c] = $thisChar;
                    if($w == $wrdLen-1) { // this is the last letter
                        $found = true;  // we have found a path
                    }
                } else {
                    break;  //this path doesn't work
                }
            } else {
                $placement[$r][$c] = $thisChar;
                if($w == $wrdLen-1) { // this is the last letter
                    $found = true;  // we have found a path
                }
            }
        }
        if($found) {
            //put the letters out of the temporary array and into the tableau
            foreach($placement as $r=>$set) {
                foreach($set as $c=>$letter) {
                    $rc[$r][$c] = $letter;
                }
            }
        }
        $tries--;
    }

    //handle the error where no spot was found for the word
    if(!$found) {
        //your error handling here
    }
}

//random fillers
$characters = range('a','z');
$max = count($characters) - 1;
for($r = 0; $r <= $row; $r++) {
    for($c = 0; $c <= $col; $c++) {
        if(!isset($rc[$r][$c])) {
            $rc[$r][$c] = $characters[mt_rand(0,$max)];
        }
    }
}
于 2014-01-21T01:58:14.767 に答える