简体   繁体   English

多维数组内的运动

[英]movement inside an multi-dimensional array

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: 我有这个数组,我正在显示一个表如何使用用户输入移动当前0分配给每个数组但我计划为数组分配其他值:

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),

It is for a checkers game no mysql. 这对于跳棋游戏没有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 我已经可以将数组序列化为文本文件,但文本文件需要包含起始位置,当每个玩家进行移动时,将文件移动到文本文件中,然后回调显示

and I have already displayed the array into an html table 我已经将数组显示在一个html表中

I am also trying to restrict movement to illegal square but that's a logic problem i need work on myself 我也试图限制移动到非法广场,但这是我需要自己工作的逻辑问题

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. up-left 左上方
  2. up-right 直立
  3. up-left-up-left 上左 - 上 - 左
  4. up-right-up-right 上右右上

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. 我将假设对于普通棋子你会使用“N”而对于皇后“Q”虽然我不会在我的例子中使用皇后。 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

Now for the eating part. 现在吃的部分。 Lets imagine that the piece on $board[$x][$y] wants to eat the one that's in diagonally left. 让我们想象一下, $board[$x][$y]上的那块东西想吃掉对角线左边的那块。

//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

So you could get an input from the user that included, the piece he wants to move, and what kind of movements he wants to do (I'm assuming, you will only allow the correct moves so I will not get into that). 所以你可以从用户那里得到一个输入,包括他想要移动的部分,以及他想要做什么样的动作(我假设,你只会允许正确的移动,所以我不会进入那个)。 If you are reading it from a form submit for example you could get the movement, the position and the player (for orientation) as $_POST variables. 如果您从表单提交中读取它,例如,您可以将移动,位置和播放器(用于方向)作为$_POST变量。

Then depending on those values modify the $board array. 然后根据这些值修改$board数组。 To do so, you could use conditionals or a switch, that's up to you. 为此,您可以使用条件或开关,这取决于您。

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

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. 最后一行将允许您为移动重复使用相同的代码,将您必须添加的值乘以$way变量,以将其添加到当前位置。 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

This should give you a starting point, all code was un-tested so I guess there may be some typos. 这应该给你一个起点,所有代码都没有经过测试,所以我猜可能会有一些错别字。

UPDATE UPDATE

If all you want is to move from X,Y to X 1 Y 1 then: 如果你想要的只是从X,Y移动到X 1 Y 1那么:

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

Is about all you need. 是你所需要的。 :) :)

You can access every field of the board using its coordinates: 您可以使用其坐标访问电路板的每个字段:

$array[$y][$x]

So, if you want to move something up, you can simply do: 所以,如果你想提升一些东西,你可以简单地做:

$array[$y-1][$x] = $array[$y][$x];
$array[$y][$x] = 0;

I guess first you should fill with 1 and 2 the cells corresponding to each type of tile. 我想首先应该用1和2填充与每种类型的瓷砖相对应的单元格。 Then you can assign numbers 3 and 4 to the ones that are coronated. 然后,您可以将数字3和4分配给加冕的数字。

1.You can ask each player for example staring x,y and ending x,y 你可以问每个玩家例如凝视x,y和结束x,y

Then for doing a movment you first need to check it is allowed. 然后,为了进行动作,首先需要检查是否允许。 Obviously starting $array[$x][$y] for player 1 should contain a 1. Then You need to make rules for this, For example if you are player 1 you can only go from $array[$x][$y] to $array[$x-1][$x-1], $array[$x-1][$y*1], etc when the place you wanna go is empty (filled with 0) Then you can check if more complicated moves are allowed like eating other player tile (which requires for example if you are player 1 to check things like $array[$x-2][$y-2] equals 0 and $array[$x-1][$y-1] equals 2. Then there is a series of more complicated verifications for the coronated ones that you should write. (besides always remember that you are moving within the limits of the array dimensions). 显然,为玩家1开始$ array [$ x] [$ y]应该包含1.然后你需要为此制定规则,例如,如果你是玩家1,你只能从$ array [$ x] [$ y ]到$ array [$ x-1] [$ x-1],$ array [$ x-1] [$ y * 1]等当你想去的地方是空的(装满0)然后你可以检查如果允许更复杂的动作就像吃其他玩家牌一样(例如,如果你是玩家1则需要检查$ array [$ x-2] [$ y-2]等于0和$ array [$ x-1] [$ y-1]等于2.然后对你应该写的加冕的那些进行一系列更复杂的验证。(除了总是记住你在数组维度的范围内移动)。

Finally you should alter the array cells that should be modified with the corresponding new values. 最后,您应该更改应使用相应新值修改的数组单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM