» » Use dice to play guessing the size game (which can control probability)

 

Use dice to play guessing the size game (which can control probability)

Author: bamboo06 on 8-11-2014, 02:07, views: 6786

25
In this paper, we explain with examples of how to implement a dice to guess the size of the games. On stage we give you all about jQuery dice animation, this issue on the basis of the previous period, related to HTML, CSS, jQuery and PHP knowledge, the article focuses on the background PHP program will calculate the probabilities based on the size selected by the user, control the final throw points out, in this case a 20% chance to guess the user.
Use dice to play guessing the size game (which can control probability)

HTML
In this example uses two dice, that can throw two dice, and the resulting points should be the sum of two dice points. In addition, we placed the two boxes on the page, the user can select the size.
<p class="sel"> 
       <input type="radio" name="bs" id="big" value="1" checked> 大 
    <input type="radio" name="bs" id="small" value="0"> 小 
</p> 
<div class="wrap"> 
   <div id="dice"><span class="dice dice_1" id="dice1"></span> 
   <span class="dice dice_6" id="dice2"></span></div> 
</div> 
<p id="result">Please click above dice!</p> 

CSS
CSS can refer to the wording of the article: jQuery dice animation, the only difference is that more than a boson, in order to save space, this article is no longer emit CSS code.
jQuery
Users select the size, and then click the dice, then by $ .getJSON () sends back data.php an ajax request, submitted a selected size parameters, and then return to complete the dice animation based on the number of points, draw the throw points.
$(function(){ 
    $("#dice").click(function(){ 
        $(".wrap").append("<div id='dice_mask'></div>");//add mask
        var dice1 = $("#dice1"); 
        var dice2 = $("#dice2"); 
        var big_num = $("#big"); 
        var sel = $("#big").attr("checked")?1:0; 
        $.getJSON("data.php",{sels:sel},function(json){ 
            var num1 = json[0]; 
            var num2 = json[1]; 
            $("#result").hide(); 
            diceroll(dice1,num1);//dice 1 animation 
            diceroll(dice2,num2);//dice 2 animation
            $("#dice_mask").remove();//remove mask
            var num = parseInt(num1)+parseInt(num2); 
            $("#result").html("Your throwing points are <span>"+num+"</span>").fadeIn(2500); 
        }); 
    }); 
}); 

Keep up on the instance is different, this time we did not use a random number of points, but to define the final throw of the dice points based on PHP backend return points, using a custom function diceroll (dice, num), represents the first parameter dice dice object, the second parameter num means that the definition of points. The following is a custom function diceroll (dice, num) to achieve animation code.
function diceroll(dice,num){ 
    dice.attr("class","dice");//After clearing the last points animation
    dice.css('cursor','default'); 
    dice.animate({left: '+2px'}, 100,function(){ 
        dice.addClass("dice_t"); 
    }).delay(200).animate({top:'-2px'},100,function(){ 
        dice.removeClass("dice_t").addClass("dice_s"); 
    }).delay(200).animate({opacity: 'show'},600,function(){ 
        dice.removeClass("dice_s").addClass("dice_e"); 
    }).delay(100).animate({left:'-2px',top:'2px'},100,function(){ 
        dice.removeClass("dice_e").addClass("dice_"+num); 
        dice.css('cursor','pointer'); 
    }); 
} 

PHP
PHP file data.php foreground task is to convey the user over the parameter values (user selected large or small), the probability distribution guessed, such as the user selected large (1), the chance of a small 8, a big chance of 2, corresponding to a percentage.
$num = intval($_GET['sels']); 
if($num==1){ //big 
    $v = array(8,2); 
}else{//small
    $v = array(2,8); 
} 
$size_arr = array( 
    '0' => array('id'=>1,'v'=>$v[0]), 
    '1' => array('id'=>2,'v'=>$v[1]) 
); 

According to predetermined probability, we should return "large" or "small", if it is small, we define two dice, and the number of points 2-6, if it is large, the number of points of the dice is defined and is 8- 12, then we mt_rand () randomly taken out a number, this number is the user throwing points.
foreach ($size_arr as $key => $val) { 
    $arr[$val['id']] = $val['v']; 
} 
 
$rid = getRand($arr); //According to the probability to calculate the size
if($rid==1){ 
    $sum = mt_rand(2,6); 
}else{ 
    $sum = mt_rand(8,12); 
} 

We got the dice total points, but we are using two tablets dice, then we'll have to assign points to two tablets boson boson, so we define the following array:
$arrs = array( 
    '2' => array(array(1,1)), 
    '3' => array(array(1,2)), 
    '4' => array(array(1,3),array(2,2)), 
    '5' => array(array(1,4),array(2,3)), 
    '6' => array(array(1,5),array(2,4),array(3,3)), 
    '7' => array(array(1,6),array(2,7),array(3,4)), 
    '8' => array(array(2,6),array(3,5),array(4,4)), 
    '9' => array(array(3,6),array(4,5)), 
    '10' => array(array(4,6),array(5,5)), 
    '11' => array(array(5,6)), 
    '12' => array(array(6,6)) 
); 

It can be seen only by 1 and 2 points a composition, but may consist of four points 1 and 3 or 2 and 2 components.
Finally, based on the total number of points we want to remove from the above array corresponding group (two-dimensional), and then randomly selected one of the group, and then disrupt the order, chaos dice digital sort, the final output json format data to the front page call.
$arr_rs = $arrs[$sum]; 
$i = array_rand($arr_rs);//random array
$arr_a = $arr_rs[$i]; 
shuffle($arr_a);//Shuffled 
echo json_encode($arr_a);//export json data 

Function getRand () is used to calculate the probability, what is getRand () code:
//Calculate the probability
function getRand($proArr) { 
    $result = ''; 
 
    //The total probability of accuracy probability array
    $proSum = array_sum($proArr); 
 
    //Probability array loop
    foreach ($proArr as $key => $proCur) { 
        $randNum = mt_rand(1, $proSum); 
        if ($randNum <= $proCur) { 
            $result = $key; 
            break; 
        } else { 
            $proSum -= $proCur; 
        } 
    } 
    unset ($proArr); 
 
    return $result; 
} 

Category: Javascript

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
6 November 2017 05:20

chandulal

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Well, since gmail no other email startup has been successful. I’ve seen many http://college-paper-writing-service.reviews/essaypro-com-reviews/ using gmail and not any other email.

<
  • 0 Comments
  • 0 Articles
7 January 2018 15:24

baby

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
The seven vertical columns at the top of the dial show the fake rolex sale months from January to December. The columns got a year disc shat shows from 2003 to 2024. The replica watches sale calendar is set by pressing the pusher at the 2 o’clock. The last two digits of the year are set first and then aligned to the column with the name of the rolex replica uk month you want. Leap years are printed in red. The bottom part of the dial shows another seven columns, containing the rolex replica sale dates from 1st to 31st. The arrangement is more like standard calendars. The days-disk is at the top of the replica watches uk columns. Pressing the pusher rotates both the year and day discs. Some might call the rolex replica featuring a rather busy-looking dial, but at 44mm, it doesn’t overfill.

<
  • 0 Comments
  • 0 Articles
27 January 2018 15:46

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
The information you have posted is very useful. The sites you have referred was good. Thanks for sharing effects of forskolin

<
  • 0 Comments
  • 0 Articles
28 January 2018 20:39

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
to offer online advanced consultancy at not too bad cost.
breast enlargement pills review

<
  • 0 Comments
  • 0 Articles
29 January 2018 21:02

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Im no expert, but I believe you just made an excellent point.
Men hoodies

<
  • 0 Comments
  • 0 Articles
31 January 2018 21:18

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog.ipl 11 live score

<
  • 0 Comments
  • 0 Articles
1 February 2018 20:59

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.academic writing services

<
  • 0 Comments
  • 0 Articles
4 February 2018 21:11

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I discovered this is an instructive and fascinating post so i suspect as much it is extremely valuable and proficient. Apc

<
  • 0 Comments
  • 0 Articles
5 February 2018 14:27

school dances

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
This article was composed by a genuine speculation essayist. I concur a considerable lot of the with the strong focuses made by the essayist. I'll be back. school dances

<
  • 0 Comments
  • 0 Articles
7 February 2018 16:50

alise

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Basisleistungen bei einer wöchentlichen Stiegenhausreinigung zum fairen Preis:
reinigungsfirma

<
  • 0 Comments
  • 0 Articles
12 February 2018 15:51

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me.SEO

<
  • 0 Comments
  • 0 Articles
13 February 2018 13:39

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Votre relation sentimentale bat de l'aile, votre couple semble prendre une mauvaise pente et vous vous inquiétez pour ce que vous réserve l'avenir ?voyance web

<
  • 0 Comments
  • 0 Articles
15 February 2018 15:04

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Where to find the Mcafee antivirus support in Australia? PCTECH24.com.au provides McAfee total protection customer care services through McAfee certified professional experts. cbse result 2018

<
  • 0 Comments
  • 0 Articles
18 February 2018 14:56

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
internet security at very affordable prices which will be suit with your budget.stranger chat

<
  • 0 Comments
  • 0 Articles
19 February 2018 21:49

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Express pest control Cyprus (Nicosia) by pestcontrolcy.com/ offers removal of any unwanted cockroaches, mice, rats or termites, from qualified and registered and professionals
express pest control

<
  • 0 Comments
  • 0 Articles
23 February 2018 21:51

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.gifts for russian girl

you really need more days but if you're tight on time this is the best thing. rubiks kube

<
  • 0 Comments
  • 0 Articles
25 February 2018 21:18

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much. and I am looking forward to Hearing from your next. 1push naija

<
  • 0 Comments
  • 0 Articles
28 February 2018 14:08

troubleshooting computer

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
lots and lots of human beings on Instagram have offered fans and engagements, from celebrities to close by corporations, however only some admit to it.troubleshooting computer

<
  • 0 Comments
  • 0 Articles
1 March 2018 20:43

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it cryptocurrency news

<
  • 0 Comments
  • 0 Articles
3 March 2018 22:58

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
We believe in living and breathing pictures. So just enjoy your wedding day, be yourself and have fun. We’ll worry about the pictures.
Personalized Jewelry

<
  • 0 Comments
  • 0 Articles
4 March 2018 22:13

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I truly delighted in investigating your site. great asset..
skin care & natural makeup

<
  • 0 Comments
  • 0 Articles
5 March 2018 13:43

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Great writeup nice to part of this group btw happy diwali wishes to you.
Crete tours

<
  • 0 Comments
  • 0 Articles
5 March 2018 20:41

Ama

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is wonderful, as well as the content! www.vicsolar.com.au

<
  • 0 Comments
  • 0 Articles
5 March 2018 21:50

shahidali

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
A conference will be held is one of the most eagerly awaited the participants. There are many participants who look forward to what could be seen. Home Contractors

<
  • 0 Comments
  • 0 Articles
6 March 2018 13:11

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I read this article. I think You put a lot of effort to create this article. I appreciate your work.The Outdoor Stuff

Information
Comment on the news site is possible only within (days) days from the date of publication.