In: Computer Science
C# programming
Create a classes called AlphabetDice that simulates a multi-sided dices with alphabet characters on each side. For instance, a 10-sided AlphabetDice will random return 'a','b','c','d','e','f','g','h','i' or 'j'.
The custom constructor should require a random seed and the number of sides.
It should also have the following:
On a side note, to cast an integer to a char, use (char). e.g.
int x = 66;
char c = (char) x;
You can use the following in main to test your class:
AlphabetDice d = new AlphabetDice(999,4); // 999 is the seed
Console.WriteLine(d.Sides);
for(int i=0;i<100;i++)
Console.WriteLine(d.Next());
If your class is correct, adding the following line to the end of main should cause a compilation error:
d.Sides = 100;
Copy and paste the complete program here;
-----------------------------I used Visual Studio 2013, C# Language, Console Application-------------------
First we need to keep in mind that the Dice comes with 4,6,8,10,12 or 20 sides, If we are giving other side value means no meaning in that. For example if we are giving side = 7, we can make the program work but in real life there is no dice that comes with 7 sides.
If we try to set value to a read only property (which
contains only get accessor), we get compilation error.
If we need to set values then we need to write set accessor or
another way is to create private variable and then return the
private variable in read only property.(See below image :: read
only property Sides return private variable _sides). We can give
value to the private variable _sides in constructor.
--------------------------OUTPUT------------------
------------------------AlphabetDice.cs----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AlphaDiceProgram
{
class AlphabetDice
{
Random random;
//Below boolean success is used to tell to main method that sides
are valid,
//For ex: If parameterized constructor passed side = 5, then
success is set to false.
bool success;
int _sides;
//Default constructor
public AlphabetDice()
{
success = false;
_sides = 0;
}
//parameterized constructor with parameter seed and side
public AlphabetDice(int seed, int side)
{
//Dice side is between even number 4 and 12
//For ex: if side passed is 7, then if condition is false;
if (side <= 12 && side >= 4 && (side % 2 ==
0))
{
//If side is 4,6,8,10,12 then new random seed is passed ,
//side value is given to private variable _sides,
//Set success to true to tell to main method entered side is
valid.
random = new Random(seed);
_sides = side;
success = true;
}
//If side is 20
else if (side == 20)
{
random = new Random(seed);
_sides = side;
success = true;
}
//If side is other than 4,6,8,10,12,20 then success set to
false
else
{
success = false;
}
}
char Next()
{
int min = 0;
int max = Sides;
//If side entered is 4, then min = 0 and max = 4
//In random.Next(0, 4) will return 4 digits - 0,1,2,3
int randomNumber = random.Next(min, max);
//To start with small letter 'a', ASCII start with 97
//97 = a, 98 = b, 99 = c ....
char ch = (char)(97 + randomNumber);
return ch;
}
public int Sides
{
get
{
return _sides;
}
}
static void Main(string[] args)
{
Console.Write("Enter Seed : ");
int seed = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Sides(4,6,8,10,12,20) : ");
int sides = Convert.ToInt32(Console.ReadLine());
AlphabetDice AD = new AlphabetDice(seed, sides);
AD.Sides = 100;
if (AD.success)
{
Console.WriteLine(AD.Sides);
for (int i = 0; i < 100; i++)
{
Console.WriteLine(AD.Next());
}
}
else
{
Console.WriteLine("Sorry, Dice comes with 4,6,8,10,12 or 20 sides
only!!!");
}
Console.ReadLine();
}
}
}