Jump to content

Using Random To Simulate Dice Rolls


Recommended Posts

A game of roll the dice against a gambler

Content:
[code]
<br/>Roll the dice, winning is nice
<!-- content separator -->
[[name]] rolled<br />[[di1]] and [[di2]]<br />Total: [[sum]]
[/code]

[code]
@vp = array(rand(1, 6), rand(1, 6));//create player's dice rolls
@vg = array(rand(3, 6), rand(2, 6));//create gambler's dice rolls and give him a slight advantage :)
@vs[0] = @vp[0] + @vp[1];//sum of player's rolls
@vs[1] = @vg[0] + @vg[1];//sum of gamberl's rolls

@vk = 0;//intialize player counter, could support more players in the game
@tpl = array();//amke sure @tpl is clear
//assign values to @tpl
@tpl[@vk]['di1'] = @vp[0];
@tpl[@vk]['di2'] = @vp[1];
@tpl[@vk]['name'] = uv('name');
@tpl[@vk]['sum'] = @vs[@vk];

++@vk;//increment player counter
@tpl[@vk]['di1'] = @vg[0];
@tpl[@vk]['di2'] = @vg[1];
@tpl[@vk]['name'] = "Gambler";
@tpl[@vk]['sum'] = @vs[@vk];

mds_template(@content[1],@tpl,false,2,'style="width:180;border:1px solid"');//call template function
retrieve(@vd);//get the player's scorign array
if(@vd == null)//check if the score were initialized yet, if not, initiliaze them
@vd = array(0, 0, 0);
//check for wins, losses or draws and increment appropriate counter
if(@vs[0] > @vs[1])
{
echo "You have won!";
@vd[0]++;
}
else if(@vs[0] < @vs[1])
{
echo "You lost =(";
@vd[1]++;
}
else
{
echo "It was a tie";
@vd[2]++;
}
store(@vd);//save the player's progress
//output the score
echo "<br/><br/>Your progress against the gambler<br/>";
echo "Wins: " . @vd[0] . "<br/>Losses: " . @vd[1] . "<br/>Draws: " . @vd[2];
[/code]

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Forum Statistics

    16,218
    Total Topics
    176,785
    Total Posts
  • Recently Browsing

    No registered users viewing this page.

  • Upcoming Events

    No upcoming events found
  • Recent Event Reviews

×
×
  • Create New...