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 [开发者_JS百科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.
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
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:
- up-left
- up-right
- up-left-up-left
- 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. 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.
//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.
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;
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
This should give you a starting point, all code was un-tested so I guess there may be some typos.
UPDATE
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. :)
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. Then you can assign numbers 3 and 4 to the ones that are coronated.
1.You can ask each player for example staring x,y and ending 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).
Finally you should alter the array cells that should be modified with the corresponding new values.
精彩评论