diceroll.c 777 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* diceroll.c -- dice role simulation */
  2. /* compile with mandydice.c */
  3. #include "diceroll.h"
  4. #include <stdio.h>
  5. #include <stdlib.h> /* for library rand() */
  6. int roll_count = 0; /* external linkage */
  7. static int rollem(int sides) /* private to this file */
  8. {
  9. int roll;
  10. roll = rand() % sides + 1;
  11. ++roll_count; /* count function calls */
  12. return roll;
  13. }
  14. int roll_n_dice(int dice, int sides)
  15. {
  16. int d;
  17. int total = 0;
  18. if (sides < 2)
  19. {
  20. printf("Need at least 2 sides.\n");
  21. return -2;
  22. }
  23. if (dice < 1)
  24. {
  25. printf("Need at least 1 die.\n");
  26. return -1;
  27. }
  28. for (d = 0; d < dice; d++)
  29. total += rollem(sides);
  30. return total;
  31. }