Jump to content

A Simple Elimination Game


PHDDriver

Recommended Posts

Hi, I was wanting to make a simple script that would assign entry numbers to every input box, then randomly generate a number, and eliminate the input box if its assigned entry number was generated. Until only 1 assigned number was left, ending the game. 

 

I don't know which is best way to do this.. jquery, js, php, or something else. php seems easiest to learn so i started with it but will change if there is easier way. am having trouble on "page2.php" part. I don't know how to carry over data from page1 to page2 

 

I'll try an explain in this step-by-step process of what im trying to do...

 

 

Say on page1.php, a person would select the number of input boxes they wanted to use, then fill in a name for each input box.

Like this:

*Clicks drop-down menu showing numbers 2-100, selects "5", then 5 input boxes appear*
 

1. Mike


2. Joe
3. Ken
4. Barbie
5. John
 
Then click "Submit" taking them to page2.php where the script randomly eliminated the input boxes 1 number at a time until only 1 number was left
 
Like This:
 
 
Game #1 is starting...
 
5 - John
2 - Joe
1 - Mike
4 - Barbie
 
 
Results
----------
 
1st Place: Ken 
 
2nd Place: Barbie
 
3rd Place: Mike
Link to comment
https://forums.phpfreaks.com/topic/278187-a-simple-elimination-game/
Share on other sites

You need JavaScript to generate the input boxes, though jQuery will be simpler to use.

 

// credits: http://stackoverflow.com/a/10142256
Array.prototype.shuffle = function() {
  var i = this.length, j, temp;
  if ( i == 0 ) return this;
  while ( --i ) {
    j = Math.floor( Math.random() * ( i + 1 ) );
    temp = this[i];
    this[i] = this[j];
    this[j] = temp;
  }
  return this;
}

var playerNames = ['Mike', 'Joe', 'Ken', 'Barbie', 'John'], // add up to 100 names here
    totalPlayers = 5, // number selected by the user
    htmlStr = '', tpl = '<div><label for="{id}">{label}</label><input type="text" id="{id}" name="names[]" value="{name}"></div>';

playerNames.shuffle().slice(0, totalPlayers).forEach(function(name, i) {
  htmlStr += tpl.replace(/\{id\}/g, 'player' + i)
                .replace(/\{label\}/g, 'Player #' + i)
                .replace(/\{name\}/g, name);
});

$(htmlStr).appendTo('#playersForm');
When you submit the form you will be able to access the names in PHP as:

 

$names = $_GET['names'];
print_r($names);

I've been reading php tutorials all day, and none of this is making much sense to me, i thought maybe it would, guess I was wrong. I am just stick with mIRC.

 

thanks again for all the help, sorry to have wasted your time. you've been very helpful ignace, thank you :) 

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.