Advertisement
metalx1000

Toss Up Dice Simulator - c

Jan 31st, 2013
1,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. typedef char * string;
  7.  
  8. int main(int argc, const char* argv[]){
  9.  
  10.     int rolls;
  11.     int dice_num;
  12.     int r;
  13.     int y;
  14.     int i;
  15.    
  16.     int win = 0;
  17.     int lose = 0;
  18.     int green = 0;
  19.     int red = 0;
  20.     char *die;
  21.  
  22.     srand(time(NULL));
  23.  
  24.     string dice[6];
  25.    
  26.     dice[0] = "red";
  27.     dice[1] = "yellow";
  28.     dice[2] = "yellow";
  29.     dice[3] = "green";
  30.     dice[4] = "green";
  31.     dice[5] = "green";
  32.  
  33.     if(argc < 2 ){
  34.         printf("How many times do you want to roll?\n");
  35.         scanf("%d", &rolls);
  36.    
  37.         printf("How many dice do you want to roll?\n");
  38.         scanf("%d", &dice_num);
  39.     }
  40.     else{
  41.         rolls = atoi(argv[1]);
  42.         dice_num = atoi(argv[2]);
  43.     }
  44.     for (i=rolls;i > 0;i--){
  45.         for (y=dice_num;y > 0;y--){
  46.                 r=rand()%6;
  47.             die = dice[r];
  48.             printf("%s,",die);
  49.             if(die == "green"){
  50.                 green++;
  51.             }
  52.             else if(die == "red"){
  53.                 red++;
  54.             }
  55.         }
  56.         //printf("green: %d\nred: %d\n",green,red);
  57.         printf("\n");
  58.         if( green == 0 && red > 0 ){
  59.             lose++;
  60.         }
  61.         else{
  62.             win++;
  63.         }
  64.         red=0;
  65.         green=0;
  66.     }  
  67.  
  68.     printf("Wins: %d\nLoses: %d\n",win,lose);
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement