Page 1 of 1

Rock,Paper,Scissors A Rock,Paper,Scissors game in PHP including the design. Rate Topic: ***** 1 Votes

#1 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 104
  • Joined: 29-July 09

Posted 14 March 2010 - 07:47 AM

Hello my name is Denis and this is a Rock,Paper,Scissors game i developed using PHP. For this tutorial we don't need access to the Mysql database, Unless you want to add this to your game, which in that case i have the attachment which connect to a Mysql.Attached File  rps.php (2.13K)
Number of downloads: 325 and Attached File  _rps.php (3.09K)
Number of downloads: 336

First // The design.
Create a page in php and call it rps.php or whatever suits you best.
<html>
<head>
</head>
<body>
<form method="post">

<table width="300" border="0" cellspacing="2" cellpadding="0" class="table">
<tr>
    <td colspan="2" align="left" class="head">Rock,Paper,Scissors</td>
    </tr>
  <?php if($valid == true ){ ?>
  <tr>
    <td width="150" align="left" class="cell"><b> Casino's choice:</b></td>
    <td width="150" align="left" class="cell"><b>Your choice:</b></td>
  </tr>
  <tr>
    <td height="100" align="center" valign="middle" style="border-right: #000000 1px solid;" class="cell"><?php echo htmlentities($casino_choice); ?></td>
    <td height="100" align="center" valign="middle" class="cell"><?php echo htmlentities($_POST['action']); ?></td>
  </tr>
  <tr>
    <td colspan="2" align="left" class="cell"><b>Result:</b> <?php echo htmlentities($status_print); ?></td>
  </tr>
	<?php } ?>
  <tr>
    <td  class="cell" height="12" colspan="2" align="center"><table width="300" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="50" align="left" class="cell"><b> Wager:</b></td>
        <td width="250" align="center" class="cell"><input name="Wager" type="text" class="entryfield" id="Wager" value="<?php if(isset($_POST['action'])){ echo "$ ".number_format($_POST['Wager']).",-"; }else{ echo "£ 1000,-"; } ?>" maxlength="20" /></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="12" colspan="2" align="center"><table width="300" border="0" cellspacing="0" cellpadding="0" >
      <tr>
        <td width="100" align="center" class="cell"><input name="action" type="submit" class="button" id="action" value="Rock." onfocus="if(this.blur)this.blur()" /></td>
        <td width="100" align="center" class="cell"><input name="action" type="submit" class="button" id="action" value="Paper." onfocus="if(this.blur)this.blur()" /></td>
        <td width="100" align="center" class="cell"><input name="action" type="submit" class="button" id="action" value="Scissors." onfocus="if(this.blur)this.blur()" /></td>
      </tr>
    </table></td>
    </tr>
</table>
<br />
</form>
</body>
</html>




Second
// Adding the php code.
i like having my php code and my design code on different pages then i require it or include it on my design page.
in the head of the design page add the code below.
I used rps.php to hold my design and _rps.php to hold my php code.
if you have a different name to _rps.php please change it in the Require section.
<? require("_rps.php");?> 


Third
//Create a second page i will call mine _rps.php this page is going to hold our php code. i do this for 2 main reason. frst it is neater and you dont have one page full of coding and it will all be confusing. Second if i have someone else helping me in designing and i am working on the coding i can code on the _rps.php while he works on the rps.php that way we don't get in each other way.
<?
$page_url = explode(".", $_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $page_url[0].".php";

if($_SERVER['REQUEST_URI'] == "/_rps.php"){
exit();
}


$_POST['Wager'] = ereg_replace("[^0-9]",'',$_POST['Wager']);
$_POST['Wager'] = round($_POST['Wager']);

$max_bet = 5000000;
$options = array("Rock.", "Paper.", "Scissors.");
$casino_choice = $options[rand(0,2)];

if(isset($_POST['action'])){

	if($_POST['Wager'] > $max_bet or $_POST['Wager'] < 1000){
		echo "Its not allowed to have a wager higher then $ ".number_format($max_bet).",- or lower then $ 1,000-.";
	}else{
	
			
		if($_POST['action'] == $casino_choice){
		$valid = true;
		$status_print = "You both picked ".$casino_choice;
		}
	
		if($_POST['action'] == "Rock." and $casino_choice == "Scissors."){
		$won = true;
		$valid = true;
		$status_print = "Rock beats Scissors. You won $ ".number_format($_POST['Wager']).",-.";
		}
	
		if($_POST['action'] == "Rock." and $casino_choice == "Paper."){
		$won = false;
		$valid = true;
		$status_print = "Paper beats Rock. You lost $ ".number_format($_POST['Wager']).",-.";
		}
	
		if($_POST['action'] == "Paper." and $casino_choice == "Rock."){
		$won = true;
		$valid = true;
		$status_print = "Paper beats Rock. You won $ ".number_format($_POST['Wager']).",-.";
		}
	
		if($_POST['action'] == "Paper." and $casino_choice == "Scissors."){
		$won = false;
		$valid = true;
		$status_print = "Scissors beats Paper. You lost $ ".number_format($_POST['Wager']).",-.";
		}
	
		if($_POST['action'] == "Scissors." and $casino_choice == "Paper."){
		$won = true;
		$valid = true;
		$status_print = "Scissors beats Paper. You won $ ".number_format($_POST['Wager']).",-.";
		}
	
		if($_POST['action'] == "Scissors." and $casino_choice == "Rock."){
		$won = false;
		$valid = true;
		$status_print = "Rock beats Scissors. You lost $ ".number_format($_POST['Wager']).",-.";
		}
	}
}

?>


please make sure you don't change the name of the buttons otherwise it will not work as it will be getting the wrong button name.

Thats all enjoy!!

This post has been edited by Denis1: 21 March 2010 - 04:53 AM


Is This A Good Question/Topic? 2
  • +

Page 1 of 1