|
Post by Dashe on Apr 17, 2012 14:00:23 GMT -5
I'm trying to make a simple Choose-Your-Own Adventure story to post on a website to try and learn more about that making-websites-and-making-them-awesome stuff. Unfortunately, I've got a few paths to code that require a coin flip to determine whether the reader succeeds or fails at something without letting him or her cheat at it. There's got to be some kind of code that lets you take a URL and make it so that clicking it will take you to one out of two designated destination pages with a 50/50 chance of getting to either one, right? It doesn't sound overly complex, but I'm not that smart so I don't know the right term for that to just google it. I'm sure if I did it would give me a headache trying to figure out what I was doing, because it likely involves a long string of code I wouldn't be too sure where to place. I barely know any code outside of the basic HTML required to run the MMLS main site news applet, and I'm just trying to do this in Dreamweaver, which is probably making a bunch of you dry heave right now. Actually, I'm sure all of you are aware by this point that I'm probably dangerous around computers. So, does anybody know what I'm trying to accomplish and/or have the skills to actually do it or explain how to do it? It's got to be possible to make a URL redirect to either one of two destination pages with a 50/50 probability. It just seems too basic for me to be having this much trouble with this. :06:
|
|
|
Post by Chiz on Apr 17, 2012 14:29:41 GMT -5
It can be done pretty easily with Javascript, if you're looking for something simple. I haven't checked any of this code, but if it's wrong, I don't think it's too far off.
Your link would use this template:
<a href="" onclick="takeachance(PROB, 'SUCCESSDEST', 'FAILDEST');">Click Me</a> PROB is probability, out of 100 (for simplicity and flexibility), successdest is your success URL, and faildest is your failure URL.
for instance:
<a href="" onclick="takeachance(75, 'http://example.org/success.htm', 'http://example.org/fail.htm');">[3/4 chance] Try to fjord the river</a> and the embedded script segment (located either in the head or the top of the body section)
<script type="text/javascript"> function takeachance(winthreshold, successdest, faildest){ var chanceroll = Math.floor(Math.random()*101); if (chanceroll => winthreshold) { window.location = successdest; } else { window.location = faildest; } } </script>
EDIT: I should point out I'm not familiar with Dreamweaver - when I make webpages and scripts, I use Notepad and other, similar text editors, so I'm not sure how you'd go about injecting this into your pages.
EDIT 2: Of course, this isn't exactly cheat proof either; one could look at the source code of the web page, copy the success URL, and go there themselves. My advice is to not be terribly concerned with those who spoil the experience for themselves by explicitly finding ways to cheat, especially when there's no money, prizes, or community high score chart at stake.
|
|
|
Post by Dashe on Apr 17, 2012 14:44:39 GMT -5
Thanks so much! You have no idea how many hours I spent trying to figure out even just how to look that up! So for clarity, takeachance stays the same since it's the name of the java script thing in the header and references that, and the "click me" part is where the clicky text goes? What if I wanted to make it an image button? Dreamweaver has a page specifically for text editing. You can toggle off the visuals entirely and it'll just give you an expensive equivalent of Notepad with some colors to keep things organized.
|
|
|
Post by Chiz on Apr 17, 2012 14:50:38 GMT -5
Yeah, the actual script part shouldn't need to change (unless you want to improve on it, and there's lots of room for improvement or additional functionality). Only the link part, which has all of the important variables for outcome.
EDIT: If you want to understand the script better, I added a bit more step-by-step documentation. You don't need to use this version in your page, but it might be useful for educational purposes.
<script type="text/javascript"> function takeachance(winthreshold, successdest, faildest){ //new function, 3 arguments var chanceroll = Math.floor(Math.random()*101); //get a random number between 0-100 //(also nigh-impossible chance of 101, but it's still there, so 50% //isn't 50%, it's 50.0000000001% or so. In practice, it's dismissable) if (chanceroll => winthreshold) { //if the roll is greater-than-or-equal-to the probability... window.location = successdest; //go to successdest } else { window.location = faildest; //otherwise (if it's not greater-than-or-equal, //i.e. less-than), go to faildest } } </script>
|
|