I have a problem printing out a shuffled array, containing 3 objects, which are “Rock”, “Paper”, and “Scissors”. Those objects need to be printed out randomly in a textarea, though so far it didn’t work for me.
<?php
echo"<div style='width:300px; height:200px; overflow:scroll;'>Bob shows";
print_r($objects[0]);
echo"</div>";
echo"<br><br>";
// Display buttons for rock, paper, or scissors.
echo"<input type='submit' name='rock' value='Rock'> <input type='submit' name='paper' value='Paper'> <input type='submit' name='scissors' value='Scissors'>";
// Our gamebot.
$bot = 'Bob';
// Show random object, rock, paper, or scissors.
$objects = array("Rock", "Paper", "Scissors");
$mixthem = shuffle($objects);
?>
The RPS game is not complete yet, I stuck on the array-part. Please help if possible,
Ups, yes, that was a dumb error / mistake of mine. I declared the array lightyears away from my statement, god. It worked after I moved the declared array variable to the top.
<?php
// Show random object, rock, paper, or scissors.
$object['1'] = 'objectRock.png';
$object['2'] = 'objectPaper.png';
$object['3'] = 'objectScissors.png';
$chosen_object = mt_rand(1,3);
$object_to_display=$object["$chosen_object"];
echo "The computer has chosen <img src='$object_to_display.png'>";
?>
Could you explain what ‘didn’t work’ means?
And in that code I don’t see where you display the array (except for the print_r in the beginning, but it seems you haven’t even declared the array at that point?).
Ok, so one last thing: I’m not satisfied with the order of the random shuffled array that is displaying, in my case it is an image, just to keep it more visual attractive. I have my random array displayed as a “print_r($objects[0]);”, so if I start the rock paper scissors game by clicking on “Rock”, it displays “Rock”, because it starts off with [0]. I hope someone understands what I mean, it’s just if you played it for a while, you sort of get to know the order, so there’s no fun in playing it anymore.
This will give you a random choice of Rock, Paper or Scissors:
<?php
// Show random object, rock, paper, or scissors.
$object['1']='Rock';
$object['2']='Paper';
$object['3']='Scissors';
$chosen_object=mt_rand(1,3);
echo 'The computer has chosen '.$object["$chosen_object"];
?>