I want to try this sequence in PHP: 10 9 8 7 6 5 4 3 2 1 3 4 5 6 7 8 9 10. The condition is I can use only one loop.
I tried this code and I know it will infinite after it prints 0. But I do not find any way to fix it.
<?php
for($i = 10; $i >= 0; $i--){
echo $i;
if($i == 0){
echo $i++;
}
}
Now I can't test it but I think it works:
<?php
$x = 10;
$cond = "DESC";
for($i = 20; $i >= 0; $i--){
echo $x;
If ( $x == 1 ) $cond = "ASC";
$cond == "DESC" ? $x-- : $x++;
}
?>
If you want to have a more simple solution you can use array:
<?php
$arr = Array (10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10);
foreach ($arr as $int){
echo $int;
}
?>
You could do this with a one-liner, if you're looking for "simple":
foreach (array_merge(range(10, 1, 1), range(3, 10, 1)) as $i) {
echo "$i ";
}
Result:
10 9 8 7 6 5 4 3 2 1 3 4 5 6 7 8 9 10
This makes the two arrays, [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ] and [ 3, 4, 5, 6, 7, 8, 9, 10 ], concatenates them, and then runs through the resultant array.
I have a problem with gradually calculating a value in PHP.
This is just an example. A user can level up, and when he reaches level 5 he unlocks the ability to play missions. The difficulty of a new generated mission is defined by a number: from 1 that is an easy mission to 10 that is a very difficult mission. See this table:
o 5: 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 and so on
o 6: 1, 2, 2, 2, 3, 3, 3, 3, 3, 3
o 7: 2, 2, 2, 3, 3, 3, 3, 3, 3, 3
o 8: 2, 2, 3, 3, 4, 4, 4, 4, 4, 4
o 9: 2, 3, 3, 4, 4, 4, 4, 4, 4, 4
o 10: 3, 3, 4, 4, 5, 5, 5, 5, 5, 5
o 11: 3, 4, 4, 5, 6, 6, 6, 6, 6, 6
o 12: 4, 4, 5, 6, 7, 7, 7, 7, 7, 7
o 13: 4, 5, 6, 7, 8, 8, 8, 8, 8, 8
o 14: 5, 6, 7, 8, 9, 9, 9, 9, 9, 9
o 15: 6, 7, 8, 9, 10, 10, 10, 10, 10, 10
o 16: 7, 8, 9, 10, 10, 10, 10, 10, 10, 10
o 17: 8, 9, 10, 10, 10, 10, 10, 10, 10, 10
o 18: 9, 10, 10, 10, 10, 10, 10, 10, 10, 10
o 19: 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
o 20: same as 19
This means: in level 5 the first mission is very easy (difficulty 1), the second one too, and the third mission is difficulty 2. And when the player is level 18: the first mission has a difficulty of 9 and the second and following missions have a difficulty of 10.
But: how to generate the difficulty of the missions?
I have two variables:
<?php
$current_player_level
$current_mission_in_level
?>
So:
<?php
$current_player_level=8;
$current_mission_in_level=3;
//This should return: 3
?>
So I need a function that returns the difficulty when the two variables above are provided.
I don't have any code because I don't know how to begin.
I'd suggest storing the difficulties in a 2-dimensional array (also called a "matrix" sometimes). The code would look like so:
$matrix_val = array(5 => array(1, 1, 2, 2, 3),
6 => array(1, 2, 2, 2, 3),
7 => array(2, 2, 2, 3, 3),
8 => array(2, 2, 3, 3, 4),
9 => array(2, 3, 3, 4, 4),
10 => array(3, 3, 4, 4, 5),
11 => array(3, 4, 4, 5, 6),
12 => array(4, 4, 5, 6, 7),
13 => array(4, 5, 6, 7, 8),
14 => array(5, 6, 7, 8, 9),
15 => array(6, 7, 8, 9, 10),
16 => array(7, 8, 9, 10, 10),
17 => array(8, 9, 10, 10, 10),
18 => array(9, 10, 10, 10, 10),
19 => array(10, 10, 10, 10, 10));
Then all you have to do is:
$difficulty = $matrix_val[$current_player_level][$current_mission_in_level];
NOTE: The "current mission in level" assumes 0 is the first index. So the third mission in level 13 would be referenced as $matrix_val[13][2] (notice [2]). If you want to shift this you can either add individual keys to each level, or simply "pad" a meaningless entry at the start of each inner array. (If this is confusing, let me know and I'll expand.)
For reference, check out the official Array PHP documentation, then you might want to read up on 2-dimensional arrays, too.
You should use arrays.
You can use 2D array (like a table) to store your levels and difficulties, and then get the right value according to the position you want.
For example:
$table = array(
5 => array(1, 1, 2, 2, 3),
6 => array(1, 2, 2, 3, 3),
7 => array(2, 2, 3, 3, 4)
);
I'm not showing the all array here.
So if you want to know for example the difficuly, you can use:
$current_difficulty = $table[$current_player_level][$current_mission_in_level];
The is no much math here, just knowing arrays.
// Filling 2D array with linear gradient from left top to right bottom
// There are some math for this. But i'm not sure it is needed
$MIN = 5.0; // min difficalty
$MAX = 9.0; // max difficalty
$M = 15.0; // Level legnth - 1
$N = 7.0; // Levels count -1
$res = array();
for($y = 0; $y <= $N; $y++)
for($x = 0; $x <= $M; $x++)
{
$y1 = ($N*$y-$M*$x+$M*$M)*$N/($M*$M+$N*$N);
$res[$N-$y][$x] = round($MAX - $y1/$N*($MAX-$MIN));
}
for($i=0; $i <= $N; $i++) {
$line = $res[$i];
echo implode(' ', $line)."\n";
}
Output:
5 5 5 6 6 6 6 7 7 7 7 7 8 8 8 8
5 5 6 6 6 6 6 7 7 7 7 8 8 8 8 8
5 5 6 6 6 6 7 7 7 7 7 8 8 8 8 8
5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 9
5 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9
6 6 6 6 6 7 7 7 7 7 8 8 8 8 9 9
6 6 6 6 6 7 7 7 7 8 8 8 8 8 9 9
6 6 6 6 7 7 7 7 7 8 8 8 8 9 9 9
The code in question comes from MathGuard, a PHP anti-spam CAPTCHA script that requires the user to answer a simple math problem. It displays the digits and operator symbols as 3x5 matrices of random characters. I understand how the code works in the sense that I can follow the code and understand what it's doing; I just don't understand how one would come to this solution.
This function takes an integer that describes one line of the 3x5 matrix and converts it into a line of random characters:
function decToBin($dec) {
$pattern = "123456789ABCDEFGHIJKLMNOPQRTSTUWXYZ";
$output = " ";
$i = 0;
do {
if ($dec % 2) {
$rand = rand() % 34;
$output { 2 - $i } = $pattern { $rand };
} else {
$output { 2 - $i } = " ";
}
$dec = (int) ($dec / 2);
$i++;
} while ($dec > 0);
$output = str_replace(" ", " ", $output);
return $output;
}
Here are the digit descriptors:
$number = array (
array ( 7, 5, 5, 5, 7 ), // 0
array ( 2, 6, 2, 2, 7 ), // 1
array ( 7, 1, 7, 4, 7 ), // 2
array ( 7, 1, 7, 1, 7 ), // 3
array ( 4, 5, 7, 1, 1 ), // 4
array ( 7, 4, 7, 1, 7 ), // 5
array ( 7, 4, 7, 5, 7 ), // 6
array ( 7, 1, 1, 1, 1 ), // 7
array ( 7, 5, 7, 5, 7 ), // 8
array ( 7, 5, 7, 1, 7 ) // 9
);
My question is: how does one come to this conclusion and method of generation and know that, for example, 7 will generate a full line of random characters, and 5 only the outermost characters?
Is this just a form of code obfuscation? What makes this method better than, say, storing the digits as a string (111101101101111 as 0, for example) and replacing each 1 with a random character?
Looks like a simple bitmap to me.
7 = 1 1 1
5 = 1 0 1
5 = 1 0 1
5 = 1 0 1
7 = 1 1 1
2 = 0 1 0
6 = 1 1 0
2 = 0 1 0
2 = 0 1 0
7 = 1 1 1