Movement inside a multidimensional array

I have this array, which I show in a table, how I can use user input to move the currently 0 assigned to each array, but I plan to assign other values ​​to the array:

my question is: how can I move up, down, right, left and move diagonally inside 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), 

This is for playing checkers without mysql.

I can already serialize the array to a text file, but the text files should contain the starting position, and when each player makes a move, put the location where the element moves to the text file, then call back to the display

and I already mapped the array to the html table

I also try to restrict movement to an illegal area, but what is a logical problem, I need to work for myself

this loop will work with the 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 have already created a database for assessment, but this will be completed after that

+4
source share
3 answers

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

  • up left
  • up right
  • up-left-up-left
  • up-right-up-right

Note that the last two elements of the list are those that eat one piece. Once you find out that you can take the current position of the figure and move it to a new one. I'm going to suggest that for ordinary items you would use "N" and for quens "Q", although I will not use queens in my examples. I will use the usual move and then the actual meal:

 //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 meal. Suppose a piece on $board[$x][$y] wants to eat the diagonal one.

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

So, you can get input from the user who turned on, the part that he wants to move, and what movements he wants to make (I assume you will only allow the right steps so that I don’t fall into that). If you read it from the submit form, you can get the movement, position and player (for orientation) as $_POST variables.

Then, depending on these values, modify the $board array. To do this, you can use the legend or switch, which is up to you.

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

This last line will allow you to reuse the same code for the movements, multiplying the values ​​that you must add at the current position in order to move to the new one using the $way variable. For example, diagonally to the 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 the code has not been tested, so I assume there may be some typos.

UPDATE

If you only need to go from X, Y to X 1 Y 1 then:

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

That is all you need. :)

+2
source

You can access all the board fields using your coordinates:

 $array[$y][$x] 

So, if you want to move something, you can just do:

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

I assume that you must first fill in 1 and 2 cells corresponding to each type of tile. Then you can assign the numbers 3 and 4 to the crowns.

1. You can ask each player, for example, watch x, y and end with x, y

Then, to make the move, you first need to verify that it is allowed. Obviously, starting $ array [$ x] [$ y] for player 1 should contain 1. Then you need to make the 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 where you want to go is empty (0 is filled). Then you can check if more complex actions are possible, for example, using a different game tile (which requires, for example, if you are player 1 to check things like $ array [$ x-2] [$ y-2] is 0 and $ array [$ x-1] [$ y-1] is equal to 2. Then there are a number of more complex coronated checks that you must write (in addition, always remember that you are moving within the size of the array).

Finally, you must change the cells of the array, which must be changed with the corresponding new values.

+1
source

All Articles