Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <wchar.h>
  6.  
  7. const char* help_text = "\
  8. A dice roller.\
  9. Usage: dice [-options] [rolls] \
  10. where options include:\
  11. -f Select items randomly from a file (newline separated)\
  12. ";
  13.  
  14. const char* dice_6_glyph(int x) {
  15. switch (x) {
  16. case 1: return "\u2680";
  17. case 2: return "\u2681";
  18. case 3: return "\u2682";
  19. case 4: return "\u2683";
  20. case 5: return "\u2684";
  21. case 6: return "\u2685";
  22. }
  23.  
  24. return "?";
  25. }
  26.  
  27. int dice_rand(int n) {
  28. return rand() % n;
  29. }
  30.  
  31. int dice_roll(int min, int max) {
  32. return dice_rand(max - min) + min;
  33. }
  34.  
  35. int main(int argc, char const* argv[])
  36. {
  37. int icon_flag = 0;
  38.  
  39. int rolls = 1;
  40. int min = 1;
  41. int max = 6;
  42.  
  43. int i, j, tmp;
  44.  
  45. srand(time(NULL));
  46.  
  47. for (i = 1; i < argc; i++) {
  48. if (strcmp(argv[i], "-h") == 0) {
  49. printf("%s\n", help_text);
  50. } else if (strcmp(argv[i], "-d") == 0) {
  51. icon_flag = 1;
  52. } else if (strcmp(argv[i], "-D") == 0) {
  53. icon_flag = 2;
  54. } else {
  55. rolls = atoi(argv[i]);
  56. }
  57. }
  58.  
  59. for (i = 0; i < rolls; i++) {
  60. tmp = dice_roll(min, max);
  61.  
  62. if (icon_flag) {
  63. printf("%s ", dice_6_glyph(tmp));
  64. if (icon_flag == 2) {
  65. printf("\n");
  66. continue;
  67. }
  68. }
  69. printf("%d\n", tmp);
  70. }
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement