Tech Off Thread

16 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Dice roll c#

Back to Forum: Tech Off
  • User profile image
    SL33P3R

    I am trying to write a program that randomly rolls 5 dice for two players. Five of a kind beats four of a kind, four of a kind beats three of a kind, three of a kind beats a pair. I have most but need help with two things.

    Compare the number of combiniations, if they are the same, the higher value win or else if the number of combination are the same with the same value, it is a tie.

    I have this so far, need help with /\  /\  /\

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Project_3_14.a
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] player1 = new int[6];
                int[] player2 = new int[6];
    
                Random rndGen = new Random();
    
                for (int i = 0; i < 5; i++)
                {
                    int diceRoll = 0;
                    
                    diceRoll = rndGen.Next(6);
                    player1[diceRoll]++;
                    Console.WriteLine("Computer rolled: {0}", diceRoll + 1);
    
                }//end for
    
                for (int i = 0; i < 5; i++)
                {
                    int diceRoll = 0;
                    diceRoll = rndGen.Next(6);
                    player2[diceRoll]++;
                    Console.WriteLine("You rolled: {0}", diceRoll + 1);
    
                }//end for
    
                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;
                }//end for
    
                if (player1[maxPlayer1] > player2[maxPlayer2])
                    Console.WriteLine("Computer won with {0} of a kind", player1[maxPlayer1], maxPlayer1 + 1);
                else
                    if (player2[maxPlayer2] > player1[maxPlayer1])
                        Console.WriteLine("You won with {0} of a kind", player2[maxPlayer2], maxPlayer2 + 1);
                    else
                        Console.WriteLine("Tie");
    
            }//end main
        }
    }
    

  • User profile image
    spivonious

    @SL33P3R: You've got most of it there. Now you just need to go through the two arrays and find out if either of them have a five-of-a-kind, four-of-a-kind, etc.

    A small suggestion would be to split things up into separate methods. It will make the code a bit easier to read. Also, rename "player1" to "computer", or create a Player class that holds a name and the dice rolls, and make two instances of the class. I don't know if you're that far into your programming class or not.

  • User profile image
    SL33P3R

    @spivonious: No im in basic logic of programming. How would I go through the two arrays and compare the number combinations to the value ex.

    computer has 3 of a kind

    player has 3 of a kind

    computer win with higher face value

    or if 

    computer has 3 of a kind 

    player has 3 of a kind

    both have same value

    tie

  • User profile image
    W3bbo

    , SL33P3R wrote

    @spivonious: No im in basic logic of programming. How would I go through the two arrays and compare the number combinations to the value ex.

    computer has 3 of a kind

    player has 3 of a kind

    computer win with higher face value

    or if 

    computer has 3 of a kind 

    player has 3 of a kind

    both have same value

    tie

    That's a simple algorithm-design problem. Your OP sounds suspiciously like a homework question, in which case people online will not provide you with a direct answer.

    Just think about how you would solve this without a computer, then instruct the computer to do the same thing.

  • User profile image
    SL33P3R

    @W3bbo:I would appreciate it if you could help me out here I am willing to donate for your help to a sulotion. Just realized my logic is wrong, it is not always correct??

  • User profile image
    Sven Groot

    You have an array with how often each value was rolled. Simply see which one occurs the most, and compare that against the other player. If it occurs more often: player wins. If it occurs the same amount of times but has a higher value: player wins. Same amount of times and same value: tie. Otherwise, the computer wins.

    Now think about how you could instruct the computer to do that.

  • User profile image
    blowdart

    Of course you could use linq quite well here Smiley

  • User profile image
    Sven Groot

    Yes, but for what appears to be a question in a beginners programming course I don't think that would be appropriate. Smiley

  • User profile image
    lensman

    I noticed that he had "Linq" in his usings...    Given blowdarts suggestion I took a stab at using Linq.  This seems a bit of an overkill however it seems sound.  I did not like the idea of hard coding the project to only roll D6 dice.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DiceRoll
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> PlayerOne = RoleDice("Computer", 6, 6);
                List<int> PlayerTwo = RoleDice("You", 6, 6);
    
                var P1 = from p in PlayerOne
                         group p by p into g
                         select new { Die = g.Key, DieCount = g.Count() };
    
                var P2 = from p in PlayerTwo
                         group p by p into g
                         select new { Die = g.Key, DieCount = g.Count() };
    
    
                // find max number of die for each hand
                int P1Max = P1.Max(w => w.DieCount); 
                int P2Max = P2.Max(w => w.DieCount);
                // using the max count of duplicates, find the maxinum dice roll
                int MaxDieP1 = P1.Where(w => w.DieCount == P1Max).Max(w => w.Die);
                int MaxDieP2 = P2.Where(w => w.DieCount == P2Max).Max(w => w.Die);
    
                if (P1Max == P2Max)
                {
                    // the number of a kind match, check max die rolled
                    if (MaxDieP1 == MaxDieP2) 
                    {
                        // both have the same number of items and die match
                        Console.WriteLine("Tie");
                    }
                    else 
                    {
                        if (MaxDieP1 > MaxDieP2) 
                        {
                            Console.WriteLine("Computer won with {0} of a kind with a die of {1}", P1Max, MaxDieP1);
                        }
                        else 
                        {
                            Console.WriteLine("You won with {0} of a kind with a die of {1}", P2Max, MaxDieP2);
                        }
                    }
                }
                else 
                {
                    if (P1Max > P2Max)
                    {
                        Console.WriteLine("Computer won with {0} of a kind", P1Max);
                    }
                    else 
                    {
                        Console.WriteLine("You won with {0} of a kind", P2Max);
                    }
                }
                Console.WriteLine("Press Return");
                Console.ReadKey();
            }
    
            private static List<int> RoleDice(string PlayerName, int DieSize, int NumberOfDice)
            {
                List<int> PlayerHand = new List<int>();
                Random r = new Random();
                while ((NumberOfDice > 0) && (DieSize > 0))
                {
                    int Roll = r.Next(DieSize) + 1;
                    Console.WriteLine("{0} rolled a {1} die", PlayerName, Roll);
                    PlayerHand.Add(Roll);
                    NumberOfDice -= 1;
                }
                if (PlayerHand.Count > 0) Console.WriteLine("{0}", "-".PadLeft(30, '-'));
                return PlayerHand;
            }
        }
    }
    

  • User profile image
    ManipUni

    static void Main(string[] args)
            {
            start:
    
                int[] player = new int[6];
                int[] computer = new int[player.Length];
                Random rndGen = new Random(unchecked((int)(DateTime.Now.Ticks + 67726965846982)));
                StringBuilder playerOutput = new StringBuilder();
                StringBuilder computerOutput = new StringBuilder();
    
                for (int i = 0; i < 10; i++)
                {
                    int diceRoll = 0;
                    player[diceRoll = rndGen.Next(player.Length)]++;
                    playerOutput.Append("  Player rolled: ");
                    playerOutput.Append(diceRoll + 1);
                    playerOutput.Append("\n");
    
                    diceRoll = 0;
                    computer[diceRoll = rndGen.Next(player.Length)]++;
                    computerOutput.Append("Computer rolled: ");
                    computerOutput.Append(diceRoll + 1);
                    computerOutput.Append("\n");
                }
    
                int playerScore = 0;
                int computerScore = 0;
    
                for (int i = player.Length - 1; i > -1; i--)
                {
                    if (playerScore == 0)
                    {
                        playerScore = (i + 1) * ((player[i] - 1) * player[i]);
                        if (playerScore != 0)
                        {
                            playerOutput.Append("\nPlayer got ");
                            playerOutput.Append(player[i]);
                            playerOutput.Append("x");
                            playerOutput.Append(i + 1);
                            playerOutput.Append("\n");
                        }
                    }
    
                    if (computerScore == 0)
                    {
                        computerScore = (i + 1) * ((computer[i] - 1) * computer[i]);
                        if (computerScore != 0)
                        {
                            computerOutput.Append("\ncomputer got ");
                            computerOutput.Append(computer[i]);
                            computerOutput.Append("x");
                            computerOutput.Append(i + 1);
                            computerOutput.Append("\n");
                        }
                    }
    
    
                }
                Console.Write(playerOutput);
                Console.WriteLine();
                Console.Write(computerOutput);
    
                if (playerScore > computerScore)
                    Console.WriteLine("\t\t\t\tPlayer wins!");
                else if (computerScore > playerScore)
                    Console.WriteLine("\t\t\t\tComputer wins!");
                else
                    Console.WriteLine("\t\t\t\tTie");
    
                Console.WriteLine(); 
                Console.ReadKey();
                Console.Clear();
                goto start;
    
            }

  • User profile image
    W3bbo

    , ManipUni wrote

     
    static void Main(string[] args)
            {
            start:
                gotostart;
     
            }

    Burn, heretic.

  • User profile image
    ManipUni

    W3bbo: Burn, heretic.

    Oh hush you. It is good debug code.

  • User profile image
    spivonious

    Well, I was impressed that no one here had given an answer to this obvious homework question. Thanks Manip and lensman! Sad

  • User profile image
    blowdart

    , W3bbo wrote

    *snip*

    Burn, heretic.

    *chortle*

    http://xkcd.com/292/

     

    Generic Forum Image

  • User profile image
    lensman

    @spivonious:  I admit I took great pause at the obvious "please write my homework for me request".  After due thought, and the lack of any responses, I figured that it is up to the morales of the end user to plagerize.  I worked a couple years in a college computer lab and was amazed how many students refused to learn.  They were more interested in getting the "A" than they were in learning the concepts.  Afterall, the purpose of such assignments (the rolling of dice) is to teach methods and concepts.  They are a way of teaching a student how to computerize a real world example.  A student which fails to learn the concepts will achieve their degree and still know nothing.

  • User profile image
    W3bbo

    A student which fails to learn the concepts will achieve their degree and still know nothing.

    Conversely a student who knows how stuff works but who fails to memorise the "textbook answers" that final exams expect will not achieve the right degree classification, but know something.

    ...which is my problem Sad

     

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.