Fellow human beings, today I present to you my dice rolling script! Behold, in my amateurish attempt at some raw C# code. Perhaps this will remain here regardless of the dozens of other dice rolling scripts out there.
Contains a ‘for’ loop, a ‘switch’, and a method that returns a value.
I have indeed read through other, similar iterations of this class, and I have found them to be lacking in what I might learn from, usually due to over-complication for my tiny brain, or because it invoked the use of libraries I have yet to hear of.
I did my best to make it easy to read for whichever intrigued person stumbles upon this. I deeply respect and appreciate any who may take the time to offer any criticism on my code. What did I do right? What did I do wrong? I don’t have anyone or anywhere else to share it with.
using System;
namespace C_
{
class RollDice : Program
{
//Variables
int numOfDice;
int numOfSides;
int maxDice = 100;
int minDice = 1;
int maxSides = 100;
int minSides = 1;
int totalRolled;
//Start method for number of Dice to throw - take the player input in InputMethod() which returns an int. If returning 0, then restart. Clamp the value between min/max. Continue to next function.
public void GetDice()
{
Console.WriteLine("Welcome to Roll Dice!");
Console.WriteLine("How many dice do you want to throw?");
numOfDice = InputMethod();
if(numOfDice == 0)
{
GetDice();
}
if (numOfDice > maxDice || numOfDice < minDice)
{
numOfDice = Math.Clamp(numOfDice, minDice, maxDice);
Console.WriteLine($"Number of Dice set to {numOfDice}");
}
GetSides();
}
// Get the sides of each dice by using a similar process. Roll() for each dice, then sum the totals in RollTotal().
public void GetSides()
{
Console.WriteLine("How many sides does each dice have?");
numOfSides = InputMethod();
if(numOfSides == 0)
{
GetSides();
}
if (numOfSides > maxSides || numOfSides < minSides)
{
numOfSides = Math.Clamp(numOfSides, minSides, maxSides);
Console.WriteLine($"Number of Sides set to {numOfSides}");
}
for (int x = 1; x <= numOfDice; x++)
{
Roll();
}
RollTotal();
}
// Create a new Random() instance for the number of sides and add it to the totalRolled variable.
public void Roll()
{
Console.WriteLine(".....");
Console.WriteLine("Rolling");
Random r = new Random();
var result = r.Next(1, numOfSides + 1);
Console.WriteLine($"You rolled {result}");
totalRolled += result;
}
// Output the total and prompt to roll again or quit. Reset the total..
public void RollTotal()
{
Console.WriteLine($"Total sum rolled is {totalRolled}");
Console.WriteLine(" ");
Console.WriteLine("Enter 1 to re-roll or 2 to quit.");
var answer = InputMethod();
switch (answer)
{
case 0:
RollTotal();
break;
case 1:
totalRolled = 0;
GetDice();
break;
case 2:
totalRolled = 0;
Environment.Exit(0);
break;
default:
Environment.Exit(0);
break;
}
}
//Convert the input to an integer if valid and return the value. Else return 0.
public int InputMethod()
{
var input = Console.ReadLine();
if(!Int32.TryParse(input, out int convertedStringToInt))
{
Console.WriteLine("You need to enter a valid number!");
return 0;
}
else
{
return Convert.ToInt32(input);
}
}
}
}