一个多维数组内运动 [英] movement inside an multi-dimensional array

查看:80
本文介绍了一个多维数组内运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这阵,我有一个表显示如何使用用户输入的运动
目前0分配给每一个数组,但我打算到数组分配其他值:

I have this array that I am displaying with a table how can i use user input for movement currently 0 is assigned to every array but I plan on assigning other values to the array:

我的问题是 - 我怎么能向上,下,左,右,以及使用阵列内的角移动用户输入

my question is - how can i move up, down, right, left, and move diagonally within the array using user input

Array ( [0] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [1] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [2] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [3] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [4] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [5] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [6] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 
        [7] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) 

    ); 


                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),
                array(0,0,0,0,0,0,0,0),

这是一个跳棋游戏没有mysql。

It is for a checkers game no mysql.

我已经可以序列化数组转换成文本文件,但文本文件需要包含起始位置,当每个球员做一招就把位置这块移动到文本文件,然后回调到显示器

I can already serialize the array into text file, but the text files needs to contain the start position and when each player makes a move put the location the piece move to in the text file then call back to the display

和我已经显示的数组转换成HTML表格

and I have already displayed the array into an html table

我也试图限制运动,以非法方,但这是一个逻辑的问题,我需要我自己的工作。

I am also trying to restrict movement to illegal square but that's a logic problem i need work on myself

将与code这个循环的工作低于

will this loop work with code below

                $row = 0;
                print "<form>";
                print "<table border = 1>";
                while ($row < 8){ // Counts to 8. (from 0...7 = 8 times. 0 ... 8 = 9 times)
                   print "<tr>";
                   $row++;
                   $col = 0; // reset column to 0 each time printing one row.

                   while ($col < 8){
                    print "<td>";
                    if($board[$row][$col] == 0)
                    {

                    print "<input type=\"checkbox\" name=\"box[]\" value=\"$value\">";
                    // Add \ before " otherwise it will treat as the end of the quote.

                    }
                    print "</td>";
                    $col++;

                   }

                   print "</tr>";

                }
                print "</table>";
                print "</form>";

我已经创建了一个数据库,以保持得分,但将在此之后

I already created a database for keeping score but that will be finished after this

推荐答案

您需要定义可移动的游戏,在这种情况下,从球员角度,你可以说,一个球员可以移动的作品:

You need to define the available movement for the game, in this case, from the players point of view you can say that a player can move it's piece:


  1. 左上方

  2. 右上方

  3. 左上方向上左

  4. 右上 - 右上方

请注意,该列表的最后两个元件是那些一块吃另一种。一旦你知道,你可以采取计件的当前位置,并将其移动到新的一个。我要去假设正常的作品,你会使用N和女王Q虽然我不会在我的例子中使用王后。
我会用一个正常的举动,然后实际的饮食之一:

Note that the two last elements of the list are those of one piece eating another one. Once you know that you can take the current position of the piece and move it to the new one. I'm going to assume that for normal pieces you would use "N" and for queens "Q" although I will not use queens in my examples. I will use a normal move and then an actual eating one:

//Piece at $board[$x][$y] moves diagonally to the left.
$board[$x-1][$y+1] = $board[$x][$y]; // This space is occupied
$board[$x][$y] = 0; //Now the space is empty

现在的饮食的一部分。让我们想象一下在 $板片[$ X] [$ Y] 想吃一个在左斜。

Now for the eating part. Lets imagine that the piece on $board[$x][$y] wants to eat the one that's in diagonally left.

//Eating action from $board[$x][$y]
$board[$x-1][$y+1] = 0; //It's been eaten!
$board[$x-2][$y+2] = $board[$x][$y]; // This space is occupied

所以,你可以得到,包括他想要做的(我假设什么样的运动的用户,他要动片,和输入,你将只允许正确的动作,所以我不会进入那)。如果从一个表单提交例如读它你可以得到运动,位置和播放器(定向)为 $ _ POST 变量。

然后根据这些数值修改 $板阵列。要做到这一点,你可以使用条件或交换机,这是给你的。

Then depending on those values modify the $board array. To do so, you could use conditionals or a switch, that's up to you.

$way = ($_POST['player'] === 'up')? 1:-1;

这最后一行将允许你重新使用相同的code的运动,由 $乘以你要添加到当前位置的值才能到新的,方式变量。例如,外出时左斜将是:

That last line will allow you to re-use the same code for the movements, multiplying the values you have to add to the current position to get to the new one, by the $way variable. For instance, going diagonally left would be:

//if player is 'up' then the value of $way is 1 so
$board[$x+(-1*$way)][$y+(1*$way)] = $board[$x][$y]; // position 2,2 becomes 1,3
//if player is not 'up' then the value of $way is -1 so
$board[$x+(-1*$way)][$y+(1*$way)] = $board[$x][$y]; // position 2,2 becomes 3,1

这应该给你一个起点,所有code是未经过测试,所以我想可能有一些错别字。

This should give you a starting point, all code was un-tested so I guess there may be some typos.

更新

如果你想要的是从X,Y移到X 1 1 然后

If all you want is to move from X,Y to X1Y1 then:

$board[$var3][$var4] = $board[$var1][$var2];
$board[$var1][$var2] = 0;

讲述的是你所需要的。 :)

Is about all you need. :)

这篇关于一个多维数组内运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆