Jump to content

Recommended Posts

I am not a very good PHP programmer, and I got a project to "work" on.

I need help writing a Dice Roll Function. This is how I'm supposed to do the two functions:

New Function: dice_roll (int sides)

    - $ returns a random number of how many sided the dice is, between 1 and the $.

 

New Function: dice_roll_str (string dicestr)

    - A string describing the dice [0-9] + d [0-9 ]+[+-][ 0-9] + size (d <number_of_dices> <number_of_sides> [+-][modifying_value])

    - For example if I call the function with "3d6-3", this means: we throw 3 times with a 6-sided dice, add it up, then subtract 3-t.

    - The function is to return a false value on error

    - Possible errors:

      - Minimum number of of sides of the dice is 2 the maximum is 24

      - A minimum number of dices 1 the maximum is 8

      - The result should always be a positive number, which is not greater than twice of the result we got.

 

I've already done the first function. This is my code:

function dice_roll($sides_num) {
    $rand_num = rand(1, $sides_num);
    return $rand_num;
}

I'm not sure it correct.

It is the second function that I don't know how to do. Please, if you can help I'd be really grateful. If you can please put explanations on how the code works.  :confused:

Thank you so much,

Evermore

Link to comment
https://forums.phpfreaks.com/topic/245753-php-dice-roll-function-help/
Share on other sites

OK, firstly - the plural of dice is die  :D

 

Secondly - this looks more suited to the freelance section, as you are effectivly asking somone to write a self contained pice of code for you, not fix a problem with some existing code of your own. 

However, could you clear up the formaula:

"- A string describing the dice [0-9] + d [0-9 ]+[+-][ 0-9] + size (d <number_of_dices> <number_of_sides> [+-][modifying_value])"

as to what d is refferencing and what dictates the <number_of_die> and <modifying_value>?

 

And also what do you meen by

" - The result should always be a positive number, which is not greater than twice of the result we got."

Very basic example

<?php
// value for each side (using colors and the 'standard' 6 sides)
$values = array('BROWN','PINK','YELLOW','BLUE','RED','GREEN');
// random value
$number = rand(0,sizeof($values)-1);
// show result;
echo $values[$number];
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.