Tech Off Thread

9 posts

Yet another C# dice question.

Back to Forum: Tech Off
  • User profile image
    Ledurgeir

    Now, I've been trying to write a program that allows me to have two "players", and each player rolls 5 dice. Then I want to program to tell me which "player" won by calculating who had more dice with the same value (as in, player1 had two dice with the value six, but player2 had three dice with the value four, so player2 won).

    I'm not sure how to do this, and my attempts so far have pretty much been useless. If you could shove me in the right direction as to how to solve the problem with how many dice are the same value, it would be great.

    Please bear in mind that I'm a relative newbie to C#.

    Thanks in advance.

  • User profile image
    Ambition

    Random random = new Random();

    for(int i = 0; i < 10; i++)
    {
       int random = random.Next(1,6);
    }

    ?

  • User profile image
    Pop Catalin Sever

    here's one way to do it :

    using System;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Security.Permissions;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection.Emit;
    using System.Reflection;

    namespace DiceGame {
        class Program {
            static void Main(string[] args) {

                // Create 2 arrays of integers to hold player dice rolls
                int[] player1 = new int[6];
                int[] player2 = new int[6];

                // Create a random number generator
                Random randomGenerator = new Random();

                // Roll dice 5 times for each player
                for (int i = 0; i < 5; i++) {
                    // Increment the array value at the position generated by the dice roll
                    // for example a dice roll of 3 increments the array at posion 3
                    int diceRoll = 0;
                    diceRoll = randomGenerator.Next(6);
                    player1[diceRoll]++;
                    //Write Player 1  dice roll
                    Console.WriteLine("Player 1 rolled: {0}", diceRoll + 1);
                    diceRoll = randomGenerator.Next(6);
                    player2[diceRoll]++;
                    //Write Player 2  dice roll
                    Console.WriteLine("Player 2 rolled: {0}", diceRoll + 1);
                }

                // Find max number of rolls with the same value
                // we effectively search the position in the arrays holding the max value
                int maxPlayer1 = 0, maxPlayer2 = 0;
                for (int i = 1; i < 5; i++) {
                    if (player1[i] > player1[maxPlayer1]) maxPlayer1 = i;
                    if (player2[i] > player2[maxPlayer2]) maxPlayer2 = i;
                }

                // Write results to console;
                if (player1[maxPlayer1] > player2[maxPlayer2])
                    Console.WriteLine("Player 1 won with {0} rolls of {1}", player1[maxPlayer1], maxPlayer1 + 1);
                else
                if (player2[maxPlayer2] > player1[maxPlayer1])
                    Console.WriteLine("Player 2 won with {0} rolls of {1}", player2[maxPlayer2], maxPlayer2 + 1);
                else
                    Console.WriteLine("Tie");
            }
        }
    }


    As an exercise  try to refactor the code to use functions that take a player numeber to do the work.

  • User profile image
    odujosh

    I would recommend making a dice object. And a DiceCollection that allow you to look at the dice in different ways like:

    int sides = 6;
    int numberOfDice = 5;
    //Provide overload also for rolling dice of mix # of sides as well?
    DiceCollection dCollection = new DiceCollection
    (sides, numberOfDice);

    dCollection.Roll();
    Console.WriteLine(dCollection.ResultsAbove(5));
    Console.WriteLine(dCollection.ResultsOf(5));

    OO is a much better design. Even with the above snippet you can see how you can abstract away alot of the work Pop shows.

    With a mix collection of dice (20 sided, 6 sided etc) Collection.Roll() would call the childs Roll method and the child would store it result. All Results methods would be a matter of iterating over the children and performing some predicate on them. (Result >= expected)

    The to make the game you suggest Each player would have a DiceCollection and it would be a simple matter of checking the results of everyone playing and displaying however you want.

  • User profile image
    PeterF

    Random rndGen = new Random();
    int[] player1 = { 0, 0, 0, 0, 0, 0 };
    int[] player2 = { 0, 0, 0, 0, 0, 0 };
    for (int i = 0; i < 5; i++)
    {
       player1[rndGen.Next(0, 5)] += 1;
       player2[rndGen.Next(0, 5)] += 1;
    }
    //Let's assume in array entry 0 we store the value 6, in 1->5 etc.
    int iWinner = 0; // if iWinner == 0 we have a draw
    int iHowmany = 0; // here we store how many of a kind has won
    for (int i = 0; i < 5; i++)
    {
       
    if (player1[i] > player2[i] && player1[i] > iHowmany)
       {
          iWinner = 1;
          iHowmany = player1[i];
       }
       
    else if (player1[i] < player2[i] && player2[i] > iHowmany)
       {
          iWinner = 2;
          iHowmany = player2[i];
       }
    }

    // Look in iWinner who has won

    Note that this doesn't hold into account that 2 pairs of 3 and 4 could be valued more than 1 pair of 5 Smiley

    Good luck,
    Peter

  • User profile image
    odujosh

    Here is a fully functional Dice Game sample:
    http://foreachdev.net/blog/2007/09/28/dice-game-sample-for-channel9/

  • User profile image
    Matthew van Eerde

    I think the key is to think about how you determine who has the most dice with the same value, breaking it down into small enough steps that you can explain it to someone over the phone.

    (The ultimate goal is to explain it to a computer, which is much harder.)

  • User profile image
    odujosh

    Matthew van Eerde wrote:
    I think the key is to think about how you determine who has the most dice with the same value, breaking it down into small enough steps that you can explain it to someone over the phone.

    (The ultimate goal is to explain it to a computer, which is much harder.)


    In my sample I got him to datatable in a non here is my 100 line main function implementation of the whole program Smiley From there he can query it however he likes to decide on breaking ties, what ranges count, etc. If he was particulairly clever he can hide the results table and have the game object decide the winner or create a game manager that gets passed the results. 

  • User profile image
    Ledurgeir

    That was exactly what I was looking for, thanks alot.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.