Jump to content

random number help


CageyJ0nnY

Recommended Posts

Hi, I am new to coding in PHP and i am in the process of creating a page that lets a user choose from rock, paper or scissors. The form page links to another page which chooses a random number from 1-3. 1=rock, 2=paper and 3=scissors. A series of IF statements are used to see who has won; the user or the PC.  I cant get this script to work and I cant see why. any comments will be gratefully  received.

 

The code for the form page:

 

<html>

<head>

<title>Rock, Paper, Scissors</title>

</head>

<body>

<h3>Rock Paper Scissors</h3>

 

<p>Choose iether rock paper or scissors<P>

<form action="result.php" method="post">

  Rock <input type="checkbox" name ="rock " value="rock"/><br/>

  Paper <input type="checkbox" name ="paper" value="paper"/><br/>

  Scissors <input type="checkbox" name ="scissors" value="scissors"/><br/>

  <input type= "submit" value = "submit" /><br/>

 

</form>

</body>

</html>

 

 

The second page:

 

<?php

$computer=rand(1,3);

//1=rock, 2=paper, 3=scissors

 

if ($human==rock){

if ($computer==1){

  echo"Draw";

}

elseif ($computer==2){

  echo"Paper beats Rock - You Lose";

}

else {

  echo"Rock beats scissors - You Win!";

}

if ($human==paper){

if ($computer==1){

  echo"Paper beats Rock - You Win!";

}

elseif ($computer==2){

  echo"draw";

}

else {

  echo"Scissors beats Paper = You Lose";

}

if ($human==scissors){

if ($computer==1){

  echo"rock beats scissors - You Lose";

}

elseif ($computer==2){

  echo"Scissors beats Paper - You Win!";

}

else {

  echo"draw";

}

?>

 

MANY THANKS

Link to comment
https://forums.phpfreaks.com/topic/188911-random-number-help/
Share on other sites

This is my rendition of Rock Paper Scissors..

Ive added comments to help you along.. If you have any questions about what any part of the code does.. just ask :)

This all goes on the 1 page..

 

<?php 

$message = '';

// Define the standard values //
$values = array(1=>'Rock',2=>'Paper',3=>'Scissors');
// Make sure the form was posted //
if (isset($_POST['hand'])) {
// Get the values for the computer and the player //
$player[0] = array('name'=>'Computer','hand'=>rand(1,3));
$player[1] = array('name'=>'Player','hand'=>$_POST['hand']);
// Bring output of message // 
$message = '<b>'.$player[0]['name'].'</b> chose '.$values[$player[0]['hand']].'<br/>';
$message .= '<b>'.$player[1]['name'].'</b> chose '.$values[$player[1]['hand']].'<hr/>';
// Check if both hands are the same //
if ($player[1]['hand'] == $player[0]['hand']) {
	$message = "Both players selected ".$values[$player[0]['hand']]." which resulted in a Draw!";
// Now we check to see if either player had a rock and the other had paper.
} elseif (($player[1]['hand'] == 1 && $player[0]['hand'] == 3) || ($player[1]['hand'] == 3 && $player[0]['hand'] == 1)) {
	// Show the correct winner by checking who had the rock and who didnt.. I used the ternary operator cause its cleaner //
	$message .= ($player[1]['hand'] == 1 ? $player[1]['name'].' wins with '.$values[$player[1]['hand']] : $player[0]['name'].' wins with '.$values[$player[0]['hand']]);
} elseif (($player[1]['hand'] == 2 && $player[0]['hand'] == 3) || ($player[1]['hand'] == 3 && $player[0]['hand'] == 2)) {
	$message .= ($player[1]['hand'] == 3 ? $player[1]['name'].' wins with '.$values[$player[1]['hand']] : $player[0]['name'].' wins with '.$values[$player[0]['hand']]);
} elseif (($player[1]['hand'] == 2 && $player[0]['hand'] == 1) || ($player[1]['hand'] == 1 && $player[0]['hand'] == 2)) {
	$message .= ($player[1]['hand'] == 2 ? $player[1]['name'].' wins with '.$values[$player[1]['hand']] : $player[0]['name'].' wins with '.$values[$player[0]['hand']]);
}
} else {
$message = 'Please select your move';
}

?>

<html>
<head>
<title>Rock, Paper, Scissors</title>
</head>
<body>
<h3>Rock Paper Scissors</h3>
<p>Choose either rock paper or scissors<P>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
  Rock <input type="radio" name="hand" value="1"/><br/>
  Paper <input type="radio" name="hand" value="2"/><br/>
  Scissors <input type="radio" name="hand" value="3"/><br/>
   <input type= "submit" value="Play!" name="submit"  /><br/><br/>
   <div style="background-color:#f6f6f6;padding:5px;width:350px;border:1px solid black;"><?php echo $message; ?></div>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/188911-random-number-help/#findComment-997506
Share on other sites

That may be just more clear for an initiate  8)

 

<?php

$computer = rand( 1, 3 );

switch ( $human ) {
case 'rock':
	$human = 1;
	break;
case 'paper':
	$human = 2;
	break;
default:
	$human = 3;
}

if ( $computer == $human ) {
echo 'Draw';
} else {
if ( $human == 1 ) {
	if ( $computer == 2 ) {
		echo 'Paper beats Rock - You Lose';
	} else {
		echo 'Rock beats Scissors - You Win';
	}
} elseif ( $human == 2 ) {
	if ( $computer == 1 ) {
		echo 'Paper beats Rock - You Win';
	} else {
		echo 'Scissors beats Paper - You Lose';
	}
} else {
	if ( $computer == 1 ) {
		echo 'Rock beats Scissors - You Lose';
	} else {
		echo 'Scissors beats Paper - You Win';
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/188911-random-number-help/#findComment-997514
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.