Advertisement
A4L

Chapter - 17 - RandomDie

A4L
Dec 7th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /*Die Rolling. Tons of games use dice. The Random class gives us the ability to simulate die rolling.
  7. Many games give the player the task of rolling multiple six-sided dice and adding up the results.
  8. We’re going to write a program that makes life easier for the player of a game like this. Start the
  9. program off by asking the player to type in a number of dice to roll. Create a new Random object and
  10. use the Random.Next method to simulate that many die rolls. Add the total up and print the result
  11. to the user. (You should only need one Random object for this.)
  12. For bonus points, keep looping and handle new numbers until they enter “quit” or “exit.”*/
  13. namespace Chapter___17___RandomDie
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             bool loopAll = false;
  20.             while (loopAll == false)
  21.             {
  22.                 Console.Write("Please enter amount of D6 Die to roll (between 1 and 22) : ");
  23.                 string input = UserInput();
  24.                 if (int.TryParse(input, out int n))
  25.                 {
  26.                     int timesToRoll = Convert.ToInt32(input);
  27.                     int[] rolls = new int[timesToRoll];
  28.                     Random ranSeed = new Random();
  29.                     for (int i = 0; i < timesToRoll; i++)
  30.                     {
  31.                         rolls[i] = DieD6(ranSeed);
  32.                     }
  33.                     Console.Write($"You have rolled {timesToRoll} D6 die : ");
  34.                     PrintIntArray(rolls);
  35.                     Console.WriteLine("\n");
  36.                 }
  37.                 else
  38.                 {
  39.                     Console.ReadKey();
  40.                     loopAll = true;
  41.                 }
  42.             }
  43.         }
  44.         static void PrintIntArray(int[] a)
  45.         {
  46.             Console.Write(a[0]);
  47.             int total = a[0];
  48.             for (int i=1; i<a.Length;i++)
  49.             {
  50.                 total +=a[i];
  51.                 Console.Write($" , {a[i]}");
  52.             }
  53.             Console.Write($"\nFor a toal roll of : {total}");
  54.         }
  55.         static int DieD6(Random ranSeed)
  56.         {
  57.             int n = 0;
  58.             //Random randomD6 = new Random();
  59.             n = ranSeed.Next(1, 7);
  60.             return n;
  61.         }
  62.  
  63.         static string UserInput()
  64.         {
  65.             bool valid = false;
  66.             string userInput = "";
  67.             while (valid == false)
  68.             {
  69.                 userInput = Console.ReadLine();
  70.                 if (userInput.ToLower() == "quit" || userInput.ToLower() == "exit")
  71.                 {
  72.                     Console.WriteLine("\nBye, bye!");
  73.                     valid = true;
  74.                     return userInput;
  75.                 }
  76.                 else
  77.                 {
  78.                     if (int.TryParse(userInput, out int input))
  79.                     {
  80.                         if (input >= 1 && input <= 22)
  81.                         {
  82.                             valid = true;
  83.                             return userInput;
  84.                         }
  85.                         else { Console.Write("Please, only enter a number between 1-22 : "); }
  86.                     }
  87.                     else { Console.Write("Please, only enter a number between 1-22: "); }
  88.                 }
  89.             }
  90.             return userInput;
  91.          }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement