Jump to content

Russian roulette game php/ajax


creatix

Recommended Posts

Hello! 

Im new here so I hope I posted in right section. I need help with a task I've been given, 

 

Make a php-based web application where you can play "Russian roulette" 
The web interface will use Ajax and call a php backend that contains the game logic. 
 
Make a simple interface where you can choose between 1-6 in a list / radio buttons and when you click on a button called "spin" so called php backend via Ajax, this shuffles out a number between 1-6 and compare with the selected century. Get a hit so the word "BOOM" appear in the web interface, otherwise the sentence "You made it" appear.
 
Thanks
Link to comment
Share on other sites

when all you do is post the list of the requirements for your assignment, without asking a specific question, you won't get any help on a programming help forum. programming help forums are for help with code you have written, or an error with code you have written, or for specific programming questions. do you have any code or error you need help with or do you have a specific question?

  • Like 1
Link to comment
Share on other sites

  • 9 months later...

Hey creatix,

 

I would write a PHP code which uses a $_GET['variable'] to access the chamber number, and then compare this with a random number. Then finally echo the message 'BOOM' or 'You made it...'.

 

Here's an example:

$user_chamber = intval($_GET['my_chamber']);
$bullet_pos = rand(1,6);
if ($bullet_pos == $user_chamber) {
	echo "BOOM! (bullet was in chamber " . $bullet_pos . ")";
} else {
	echo "You made it bro :-)  (bullet was in chamber " . $bullet_pos . ")";
}

Then you've got to write the AJAX. It's a simple program so I would just go with an HTML file with an embedded JavaScript.

 

Here's what I came up with. If you need more help have a look at this cool tutorial: http://www.w3schools.com/php/php_ajax_php.asp

 

Notice the javascript function included in the head, it is called by the trigger button.

 

The form contains a select element and a button element. I've chose the button element rather than a submit element because we don't want to reload the whole page, just update it using AJAX.

<html>

<head>
<title>Russian roulette</title>
<script>
function pullTrigger(chamber)
{
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById("result").innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET", "roulette.php?my_chamber=" + chamber, true);
  xmlhttp.send();
}
</script>
</head>

<body>

  <div id="result">Let's play a little game...</div>

  <form>
	<select id="pistol">
	<option value="1">First chamber</option>
	<option value="2">Second chamber</option>
	<option value="3">Third chamber</option>
	<option value="4">Forth chamber</option>
	<option value="5">Fifth chamber</option>
  	<option value="6">Sixth chamber</option>
	</select>
	
	<input type="button" value="Pull the trigger" onclick="pullTrigger(document.getElementById('pistol').value)">
  </form>
  
</body>

</html>
Link to comment
Share on other sites

Appears you already wrote the code. All you have to do now is implement it.

<?php

if (isset($_GET['my_chamber']))
{
    $user_chamber = intval($_GET['my_chamber']);
    $bullet_pos = rand(1,6);

    if ($bullet_pos == $user_chamber)
    {
    	echo "BOOM! (bullet was in chamber " . $bullet_pos . ")";
    }
    else
    {
    	echo "You made it bro :-)  (bullet was in chamber " . $bullet_pos . ")";
    }
}
else
{
    'DANG! out of bullets. Time to reload';
}
Link to comment
Share on other sites

Guest
This topic is now 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.