Cube Randomizer
Moderators: egami, macek, gesf
I know there is that possibility, but I want to make it so that the variable gets stored and if in the next loop the rand() generates the same number it will cancel it (probably with and if statement)
I can't seem to figure that out because in the next loop it forgets the last one
Code: Select all
if(isset($_POST["send"]))
{
$amount = $_POST["amount"];
for($i = 1; $i <= $amount; $i++)
{
$rotation = rand(1,6);
if($rotation == 1)
{
$one = rand(1,3);
if($one == 1)
{
echo"U ";
}
if($one == 2)
{
echo"'U ";
}
else
{
echo"U2 ";
}
}
You have already worked out the solution:
now what you need is something to compare it to ... ?????
A while loop may serve you better than an if statement?
I want to make it so that the variable gets stored
Code: Select all
$rotation = rand(1,6);
A while loop may serve you better than an if statement?
I'm trying but it doesn't want to work, it still gives me the same side twice
Code: Select all
<?php
if(isset($_POST["send"]))
{
$amount = $_POST["amount"];
$store_random = 0;
while($store_random !== $rotation && $i <= $amount)
{
for($i = 1; $i <= $amount; $i++)
{
$rotation = rand(1,6);
if($rotation == 1)
{
$one = rand(1,3);
$store_random == $rotation;
if($one == 1)
{
echo"U ";
}
if($one == 2)
{
echo"'U ";
}
else
{
echo"U2 ";
}
}
if($rotation == 2)
{
$two = rand(1,3);
$store_random == $rotation;
if($two == 1)
{
echo"L ";
}
if($two == 2)
{
echo"L2 ";
}
else
{
echo"'L ";
}
}
if($rotation == 3)
{
$three = rand(1,3);
$store_random == $rotation;
if($three == 1)
{
echo"F ";
}
if($three == 2)
{
echo"F2 ";
}
else
{
echo"'F ";
}
}
if($rotation == 4)
{
$four = rand(1,3);
$store_random == $rotation;
if($four == 1)
{
echo"R ";
}
if($four == 2)
{
echo"R2 ";
}
else
{
echo"'R ";
}
}
if($rotation == 5)
{
$five = rand(1,3);
$store_random == $rotation;
if($five == 1)
{
echo"B ";
}
if($five == 2)
{
echo"B2 ";
}
else
{
echo"'B ";
}
}
if($rotation == 6)
{
$six = rand(1,3);
$store_random == $rotation;
if($six == 1)
{
echo"D ";
}
if($six == 2)
{
echo"D2 ";
}
else
{
echo"'D ";
}
}
}
}
}
?>
That's a good start, what I had in mind for the while loops is as follows:
Once you have found a new non-repeating number, store that for the next comparison; you can't use the same variable to store the new number and then check it against itself, so keep track of the last number as well.
Here's a script to have a play with, and includes a bit of extra security, you must never trust user input as it can totally screw a script over unless your careful:
Code: Select all
while ($previous == $current){
$current = rand(1,6);
}
Here's a script to have a play with, and includes a bit of extra security, you must never trust user input as it can totally screw a script over unless your careful:
Code: Select all
<!-- Basic form to input a number -->
<form method="post" action="">
<label for = "amount">Number of rotations</label>
<input type="text" name="amount">
<input type="submit" value="Submit">
</form>
<?php
// Verify a POST request has been made
if ($_SERVER["REQUEST_METHOD"] != "POST") {
exit;
}
// Declare the previous and current
$previous = 0;
$current = 0;
// Check to see if a valid number has been sent from the form
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
if (!$amount) {
exit;
}
for ($i=0; $i < $amount; $i++){
while ($previous == $current){
$current = rand(1,6);
}
// A number has been chosen which is not the same as the previously used number
// So store it for the comparison on the next run
$previous = $current;
echo 'The random number is: ' . $current . '<br>';
// Now do your rotations ...
//
}
I think I'm very close, but it doesn't want to work.
Could you look at this, I still don't know what's going on.
Could you look at this, I still don't know what's going on.
Code: Select all
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cube Randomizer</title>
<style>
body
{
background-color: #c40d0d;
color: white;
font-size: 40px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<form action="randomizer.php" method="post">
<center><h1>Randomize your cube</h1>
<label for = "amount">Number of rotations:</label>
<input type="number" name="amount" style="width: 150px; height: 30px; font-family: Arial, Helvetica, sans-serif, 12px;"/><br /><br />
<input type="submit" name="send" value="Randomize" style="width: 150px; height: 40px; font-family: Arial, Helvetica, sans-serif, 12px;"/>
</form>
<br /><br />
<?php
if ($_SERVER["REQUEST_METHOD"] != "POST")
{
exit;
}
if(isset($_POST["send"]))
{
$current = 0;
$previous = 0;
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
if (!$amount)
{
exit;
}
for($i = 1; $i <= $amount; $i++)
{
while ($previous == $current)
{
$current = rand(1,6);
}
if($current == 1)
{
$one = rand(1,3);
if($one == 1)
{
echo"U ";
}
if($one == 2)
{
echo"'U ";
}
else
{
echo"U2 ";
}
}
if($rotation == 2)
{
$two = rand(1,3);
if($two == 1)
{
echo"L ";
}
if($two == 2)
{
echo"L2 ";
}
else
{
echo"'L ";
}
}
if($rotation == 3)
{
$three = rand(1,3);
if($three == 1)
{
echo"F ";
}
if($three == 2)
{
echo"F2 ";
}
else
{
echo"'F ";
}
}
if($rotation == 4)
{
$four = rand(1,3);
if($four == 1)
{
echo"R ";
}
if($four == 2)
{
echo"R2 ";
}
else
{
echo"'R ";
}
}
if($rotation == 5)
{
$five = rand(1,3);
if($five == 1)
{
echo"B ";
}
if($five == 2)
{
echo"B2 ";
}
else
{
echo"'B ";
}
}
if($rotation == 6)
{
$six = rand(1,3);
if($six == 1)
{
echo"D ";
}
if($six == 2)
{
echo"D2 ";
}
else
{
echo"'D ";
}
}
$previous = $current;
}
}
?>
</center></body>
</html>
You'll need to provide more information on what your problems are if the following does not solve it.
Do you have error reporting set to display?
In the mean time, compare these two lines:
and then compare them to the rest of your if statements, having error reporting on would have helped on this.
Note: <center> is not valid html 5, use css.
Also note that you can check your HTML and CSS using the following:
Check CSS here
Check HTML here
It's also good practice to put your CSS in its own file and load it using
style.css
However, you should really concentrate your efforts on getting the script to work before you look at how it should appear.
Do you have error reporting set to display?
In the mean time, compare these two lines:
Code: Select all
if($current == 1)
Code: Select all
if($rotation == 2)
Note: <center> is not valid html 5, use css.
Also note that you can check your HTML and CSS using the following:
Check CSS here
Check HTML here
It's also good practice to put your CSS in its own file and load it using
Code: Select all
<link href="/style.css" rel="stylesheet">
Code: Select all
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cube Randomizer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='/style.css' rel='stylesheet'>
</head>
<body>
Code: Select all
body
{
background-color: #c40d0d;
color: white;
font-size: 40px;
font-family: Arial, Helvetica, sans-serif;
}
input[type=submit]
{
width: 150px;
height: 40px;
font-family: Arial, Helvetica, sans-serif; /* invalid in font-family, 12px;*/
}
input[type=number]
{
width: 150px;
height: 30px;
font-family: Arial, Helvetica, sans-serif; /* invalid in font-family, 12px;*/
}
sorry for the vague answer, I should mention that it's only displaying U, 'U and U2 and if I let's say do 8 rotations (in the Input) it will only show like the 3 times it became $current = 1.
I'm trying to debug it now.
<center></center> worked for me, don't know why then.
ah I see what you mean, I'll see if it works now
I'm trying to debug it now.
<center></center> worked for me, don't know why then.
ah I see what you mean, I'll see if it works now

http://www.caelanvaneijnsbergen.infdlw. ... omizer.php
try it here it doesn't work and I have the same problem with it repeating the same side
here is the code:
try it here it doesn't work and I have the same problem with it repeating the same side
here is the code:
Code: Select all
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cube Randomizer</title>
<style>
body
{
background-color: #c40d0d;
color: white;
font-size: 40px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<form action="randomizer.php" method="post">
<center><h1>Randomize your cube</h1>
<label for = "amount">Number of rotations:</label>
<input type="number" name="amount" style="width: 150px; height: 30px; font-family: Arial, Helvetica, sans-serif, 12px;"/><br /><br />
<input type="submit" name="send" value="Randomize" style="width: 150px; height: 40px; font-family: Arial, Helvetica, sans-serif, 12px;"/>
</form>
<br /><br />
<?php
if ($_SERVER["REQUEST_METHOD"] != "POST")
{
exit;
}
if(isset($_POST["send"]))
{
$current = 0;
$previous = 0;
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
if (!$amount)
{
exit;
}
for($i = 1; $i <= $amount; $i++)
{
while ($previous == $current)
{
$current = rand(1,6);
$sub_rotation = rand(1,3);
}
if($current == 1)
{
if($$sub_rotation == 1)
{
echo"U ";
}
if($sub_rotation == 2)
{
echo"'U ";
}
else
{
echo"U2 ";
}
}
if($current == 2)
{
if($sub_rotation == 1)
{
echo"L ";
}
if($sub_rotation == 2)
{
echo"L2 ";
}
else
{
echo"'L ";
}
}
if($current == 3)
{
if($sub_rotation == 1)
{
echo"F ";
}
if($sub_rotation == 2)
{
echo"F2 ";
}
else
{
echo"'F ";
}
}
if($current == 4)
{
if($sub_rotation == 1)
{
echo"R ";
}
if($sub_rotation == 2)
{
echo"R2 ";
}
else
{
echo"'R ";
}
}
if($current == 5)
{
if($sub_rotation == 1)
{
echo"B ";
}
if($sub_rotation == 2)
{
echo"B2 ";
}
else
{
echo"'B ";
}
}
if($current == 6)
{
if($sub_rotation == 1)
{
echo"D ";
}
if($sub_rotation == 2)
{
echo"D2 ";
}
else
{
echo"'D ";
}
}
$previous = $current;
}
}
?>
</center></body>
</html>
what I am noticing when debugging is that if the same side gets picked two times it skips to the same side anyway. I think I know the fix.
edit:
still looking for a fix, to be clear:
if I put in 5 for the amount it does it like this, note that I also echo 'works' if is executed.'
worksD 'D works'B works'D worksB 'B works'U
edit:
still looking for a fix, to be clear:
if I put in 5 for the amount it does it like this, note that I also echo 'works' if
Code: Select all
while ($previous == $current)
{
$current = rand(1,6);
$sub_rotation = rand(1,3);
echo"works";
}
worksD 'D works'B works'D worksB 'B works'U
Modify your php.ini file to display_errors = on
Check this line very carefully:
Also check your logic here:
Follow it through and assume that $sub_rotation is 1, what is echoed?
Check this line very carefully:
Code: Select all
if($$sub_rotation == 1)
Code: Select all
if ($sub_rotation == 1) {
echo"L ";
}
if ($sub_rotation == 2) {
echo"L2 ";
} else {
echo"'L ";
}
one thing that helped was replacing the else statements with if statements.
but now it's doing this once in a while:
works'R worksworksR works'B worksworksD2 worksB2 works'F worksD2
you can see that it took that same side anyway and I'm clueless how
look here to try: http://www.caelanvaneijnsbergen.infdlw. ... omizer.php
but now it's doing this once in a while:
works'R worksworksR works'B worksworksD2 worksB2 works'F worksD2
you can see that it took that same side anyway and I'm clueless how
Code: Select all
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cube Randomizer</title>
<style>
body
{
background-color: #c40d0d;
color: white;
font-size: 40px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<form action="randomizer.php" method="post">
<center><h1>Randomize your cube</h1>
<label for = "amount">Number of rotations:</label>
<input type="number" name="amount" style="width: 150px; height: 30px; font-family: Arial, Helvetica, sans-serif, 12px;"/><br /><br />
<input type="submit" name="send" value="Randomize" style="width: 150px; height: 40px; font-family: Arial, Helvetica, sans-serif, 12px;"/>
</form>
<br /><br />
<?php
if ($_SERVER["REQUEST_METHOD"] != "POST")
{
exit;
}
if(isset($_POST["send"]))
{
$current = 0;
$previous = 0;
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
if (!$amount)
{
exit;
}
for($i = 1; $i <= $amount; $i++)
{
while($previous == $current)
{
$current = rand(1,6);
$sub_rotation = rand(1,3);
echo"works";
}
if($current == 1)
{
if($$sub_rotation == 1)
{
echo"U ";
}
if($sub_rotation == 2)
{
echo"'U ";
}
if($sub_rotation == 3)
{
echo"U2 ";
}
}
if($current == 2)
{
if($sub_rotation == 1)
{
echo"L ";
}
if($sub_rotation == 2)
{
echo"L2 ";
}
if($sub_rotation == 3)
{
echo"'L ";
}
}
if($current == 3)
{
if($sub_rotation == 1)
{
echo"F ";
}
if($sub_rotation == 2)
{
echo"F2 ";
}
if($sub_rotation == 3)
{
echo"'F ";
}
}
if($current == 4)
{
if($sub_rotation == 1)
{
echo"R ";
}
if($sub_rotation == 2)
{
echo"R2 ";
}
if($sub_rotation == 3)
{
echo"'R ";
}
}
if($current == 5)
{
if($sub_rotation == 1)
{
echo"B ";
}
if($sub_rotation == 2)
{
echo"B2 ";
}
if($sub_rotation == 3)
{
echo"'B ";
}
}
if($current == 6)
{
if($sub_rotation == 1)
{
echo"D ";
}
if($sub_rotation == 2)
{
echo"D2 ";
}
if($sub_rotation == 3)
{
echo"'D ";
}
}
$previous = $current;
}
}
?>
</center></body>
</html>
hyper wrote: ↑Thu Nov 15, 2018 2:42 amModify your php.ini file to display_errors = on
Check this line very carefully:Code: Select all
if($$sub_rotation == 1)
If you expect something to be random, don't try to force it in a particular manner; if you are not happy in the way that you think it should be, then maybe look for another method. Computers (and humans) are not very good at generating random numbers, especially when you want several in a short space of time. If you are using PHP 7, there is an alternative: random_int which is an improvement.you can see that it took that same side anyway
Thx for guiding me through this.
I think I finally did it(at least didn't get any wrong codes until now).
I made a stupid mistake at the end where I had 2 $ signs after each other like this:
I think I finally did it(at least didn't get any wrong codes until now).
I made a stupid mistake at the end where I had 2 $ signs after each other like this:
Code: Select all
if($$sub_rotation == 1)
You've done well making your own improvements, so points go to you
I'm more than happy to help someone who helps themselves.
If you are looking to learn more, there are several ways that you can compact your script; although this is only a short script, they can soon get out of control.
I'm in no way criticising what you have done, or how you have done it since what you have done is the best way when learning; you haven't complicated anything or tried to use or do something for the sake of it, your intentions are clear and it's easier to develop when everything is clear. I'm just trying to push you a bit further if you want to:
You could get the number of lines down significantly by generating your own strings based on the random number rather than using lots of if statements:
Thinking out of the box a bit -
you are selecting a random number between 1 and 6, then a number between 1 and 3 - this creates 18 possibilities on each iteration ... ?
Do you know about arrays?

If you are looking to learn more, there are several ways that you can compact your script; although this is only a short script, they can soon get out of control.
I'm in no way criticising what you have done, or how you have done it since what you have done is the best way when learning; you haven't complicated anything or tried to use or do something for the sake of it, your intentions are clear and it's easier to develop when everything is clear. I'm just trying to push you a bit further if you want to:
You could get the number of lines down significantly by generating your own strings based on the random number rather than using lots of if statements:
Thinking out of the box a bit -
you are selecting a random number between 1 and 6, then a number between 1 and 3 - this creates 18 possibilities on each iteration ... ?
Do you know about arrays?
No, I think I started coding on PHP 1-2 months ago and haven't done many other languages.
I'll most definitely will still learn it in the lessons at school.
and I realise that there are indeed many ways to make it far more efficient.
Thx a lot for giving me tips on where to look and making me realise how everything works

An array is basically a list of variables using the same name, it's a construct of most languages; you'd normally learn about them once you have learned the basics of variables.
Keep up the good work!
Keep up the good work!