sfgjs sfgjs
sfgjs sfgjs

Reputation: 21

C# Dice Roll Program With Images

i am trying my best to do this program which is to make a program that lets the user to roll two dices as many as it wants, but the dice rolled can not be shown as numbers but as a image.

such as

[o]

for a die roll of 1.

i havent made the loop code for the program yet and i only know how to make the random numbers for the rolls, i just can't figure out how to make the arraylist for the images and make the code to actually use the images instead of numbers... if you know what i mean.

here is my code so far, thanks for the help!

        int[] DiceUno = new int[6];
        int[] DiceDos = new int[6];
        Random rnd = new Random();

        Console.WriteLine("This program will allow you to roll two dice");
        Console.WriteLine("\nAs many times as you want");
        Console.WriteLine("\n\nWhen you want to exit the program, please type (exit)");
        Console.WriteLine("\nPress any key to begin rolling");
        Console.Read();


        for (int i = 0; i < 1; i++)
        {
            int diceRoll = 0;
            diceRoll = rnd.Next(6);
            DiceUno[diceRoll]++;
            Console.WriteLine("Dice 1 is rolled a: {0}", diceRoll + 1);
            diceRoll = rnd.Next(6);
            DiceDos[diceRoll]++;
            Console.WriteLine("Dice 2 is rolled a: {0}", diceRoll + 1);

        }





    }
}

}

Upvotes: 2

Views: 6283

Answers (3)

Bryan Crosby
Bryan Crosby

Reputation: 6554

This should work using some quick and dirty LINQ.

var die = new Dictionary<int, string>
{
    { 1, "[     ]\n[  o  ]\n[     ]" }, //or a path to an image somewhere or anything you want
    { 2, "[     ]\n[ o o ]\n[     ]" },
    { 3, "[  o  ]\n[ o o ]\n[     ]" },
    { 4, "[ o o ]\n[     ]\n[ o o ]" },
    { 5, "[ o o ]\n[  o  ]\n[ o o ]" },
    { 6, "[ o o ]\n[ o o ]\n[ o o ]" },
};

do
{
    var shuffled = die.OrderBy(x => Guid.NewGuid()).Take(2);

    foreach (KeyValuePair<int, string> i in shuffled)
    {
        Console.WriteLine(i.Value);
        Console.WriteLine();
    }
} while (Console.ReadLine() != "(exit)");

Upvotes: 6

Nikola Markovinović
Nikola Markovinović

Reputation: 19346

If you want to output a text rather than a number, create an array of strings:

string[] images = new string[]
    { "o", "oo", "ooo", "oooo", "ooooo", "oooooo" };

And instead of diceRoll + 1 in Console.WriteLine put images[diceRoll]:

Console.WriteLine("Dice 1 is rolled a: {0}", images[diceRoll]);

Now you can play with images, perhaps create a three-line images to show numbers as they appear on die (dot-empty space).

Upvotes: 0

Stewart Dale
Stewart Dale

Reputation: 361

why not something as simple as

Dictionary<int, string> valueToDiceImage = new Dictionary<int, string>() 

{

 {0, "[0]"},

 {1, "[1]"},

 {2, "[2]"},

 {3, "[3]"},

 {4, "[4]"},

 {5, "[5]"},

 {6, "[6]"},

};

and then use it like such:

int diceRoll = rnd.next(6); 
System.Console.Write("User Rolled a " + valueToDiceImage[diceRoll] + "\n");

Upvotes: 0

Related Questions