Hi Everyone,

This is my first post on this forum. I am pretty new to programming so any help is greatly valued here. Was hoping someone could steer me in the right direction with this Dice-Poker style program I am trying to write in C#.

So far I am able store random numbers between 1 and 6 in two arrays(compRoll and userRoll) and print the results.

Where I'm stuck right now is how to use a function to find matching elements in the array itself. Then storing the number of matches, even their values so that I can print whether there are 2 of a kind, 3 of a kind, etc. The player with more matches wins. In the event of a tie between the comp and user I would just need to measure the value of the matches to determine the winner. In an all out tie I guess you just re-roll.

If you have any suggestions on how I can find the matches in the array, then use them to determine the winner, I'd be very grateful.

If I am posting this in the wrong place kindly point me to the right place. Thanks for reading this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind. 
 * If both computer and user have a pair, higher dice value wins.
 * If both pair values are same then next highest number in the hand wins.
 * If all of both computer and user values are identical, Tie game.
 * If either player doesn't at least have a pair highest number wins.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            const int DICE = 5;
            int[] compRoll = new int[DICE];
            int[] userRoll = new int[DICE];
            int x = 0;
            ComputerRoll(x,compRoll,compHand,DICE);
            UserRoll(x,userRoll,userHand,DICE);
        }
        static void ComputerRoll(int x,int[] compRoll,int DICE)
        {
            Random RandNum = new Random();
            Console.Write("{0,-20}","Computer Rolled: ");
            for (x = 0;x < DICE;x++)
            {
                compRoll[x] = RandNum.Next(1, 7);
                Console.Write("{0,-2}",compRoll[x]);
            }
        }
        static void UserRoll(int x,int[] userRoll,int DICE)
        {
            Random RandNum = new Random();
            Console.Write("\n\n{0,-20}", "You Rolled: ");
            for (x = 0; x < DICE; x++)
            {
                userRoll[x] = RandNum.Next(1, 7);
                Console.Write("{0,-2}", userRoll[x]);
            }
        }
    }
}

Recommended Answers

All 20 Replies

>Finding duplicate elements in an Array.

Use Array.IndexOf() method.

Perhaps this will give you an idea of how to solve your problem.

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = new int[] { 1, 5, 3, 5, 5, 4 };

            var query = (from value in values
                         group value by value into groupjoin
                         select new
                         {
                             Value = groupjoin.Key,
                             Count = groupjoin.Count()
                         })
                        .OrderByDescending(v => v.Count)
                        .ThenBy(v => v.Value)
                        .First();

            Console.WriteLine("{0} was rolled {1} times", query.Value, query.Count);
            Console.Read();
        }
    }
}

This approach utilizes LINQ to transform and project in-memory data into other usable forms.

>Finding duplicate elements in an Array.

Use Array.IndexOf() method.

Been fooling around with the Array.IndexOf() method but I can't seem to wrap my mind around it. Since the numbers are randomly generated I can't specify the exact number to match it to. I've been looking for examples around the net but the only examples I find involve strings which their values are already declared. In other words you have to specify the value of the specific element you want to check matches for right?

Can anyone provide an example of some code where the Array.IndexOf() method might be used on an array containing random integers?

Thanks for the suggestion adatapost.

Maybe this article will help also.

I haven't tried putting .IndexOf() to code, but the mental programs in my head seem to be including iterating through the possible values of the dice and finding each instance of the value and incrementing a counter. You would need to do this for the user and the computer, count each dice value, and then come in and develop the logic to find the user's max count/value and compare that to the computer's max count/value and keep comparing until there's a tiebreaker, if one is to be found. It's possible, and someone may even come up with a better algorithm and do it more efficiently than perhaps I'm describing.

Personally, I lean back towards using LINQ. Consider this more fleshed out example:

using System;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] userRolls = new int[] { 1, 5, 3, 5, 5 };
            int[] compRolls = new int[] { 1, 5, 5, 5, 1 };

            var userCounts = GetCounts(userRolls);
            var compCounts = GetCounts(compRolls);

            int max = userCounts.Count() < compCounts.Count() ? userCounts.Count() : compCounts.Count();

            bool userWins = false;
            bool compWins = false;

            for (int i = 0; i < max; i++)
            {
                // implement logic to determine winner   

                // use userCounts[i] and compCounts[i] for the comparisons (suggestion) 
            }

            if (userWins)
                Console.WriteLine("The user wins.");
            else if (compWins)
                Console.WriteLine("The computer wins");
            else
                Console.WriteLine("The game is tied.");
            
            Console.Read();
        }

        static List<DiceRollCount> GetCounts(int[] diceRolls)
        {
            var query = (from value in diceRolls
                         group value by value into groupjoin
                         select new DiceRollCount
                         {
                             DiceValue = groupjoin.Key,
                             RollCount = groupjoin.Count()
                         })
                        .OrderByDescending(v => v.RollCount)
                        .ThenByDescending(v => v.DiceValue);

            return query.ToList();
        }
    }

    class DiceRollCount
    {
        public int DiceValue { get; set; }
        public int RollCount { get; set; }
    }
}

The point is the algorithm I was talking about above is being handled by .NET's own querying capabilities in the method GetCounts (in conjunction with the newly defined DiceRollCount class).

commented: Cool indeed! +6

Very cool. Looks like it might help a ton. Thanks a lot apegram!

I hope it gives you ideas, not only for this but how to leverage LINQ in other programs. One note: I made a slight correction to my GetCounts method, I had an ordering incorrect (the .ThenBy clause is now .ThenByDescending).

Hey y'all,

Thanks for the help. Using LINQ is brand new to me. Because of your help I hope to able to incorporate it down the road. For now I just went back to my pseudo-code and found another way to find matches in the compRoll array and store the number of matches in a countSum[] array.

I still have a lot of work to do but I thought I'd share my pseudo-code so far. I'll be sure to post my progress and the final code in C#.

Make sure to toggle to plain text. Don't want to confuse anyone.

Start
	const int SIZE = 6
	const int DICE = 5
	num randNum(1-6)	
	num countSum[SIZE] 
	num compRoll[DICE] 
	num userRoll[DICE] 
	num count
	num x = 0
	for(x = 0;x < DICE;x = x + 1)
		compRoll[x] = randNum(1-6)
		print compRoll[x]	
	for(x = 0;x < DICE;x = x + 1)
		userRoll[x] = randNum(1-6)
		print userRoll[x]
	count = 1
	while(count <= SIZE)
		for(x = 0;x < DICE;x = x + 1)
			if compRoll[x] = count
				countSum[count-1] = countSum[count-1] + 1
		count = count + 1

/*Cycled through the compRoll array and checked for
matches for the numbers 1-6 in each element of the compRoll array
then added number of 1's,2's,3's,etc. to countSum[] in each correctly #ed element

The for loop and the inner if loop accomplish this:

check all elements of compRoll and see if there is a 1(count = 1). 
If so add 1 in the countSum[count-1] at element 0


The while loop increments count by 1 each time till SIZE reaches 7.

begin again:

check all elements of compRoll and see if there is a 2(count = 2). 
If so add 1 in the countSum[count-1] at element 1*/

Take a look at,

int[] countSum=new int[7];

            for (x = 0; x < DICE; x++)
            {
                countSum[compRoll[x]]++;
            }
            Console.WriteLine();
            for(x=0;x<countSum.Length;x++)
                Console.Write("  " + countSum[x]);
            ..

So basically I can get rid of the if statement by incrementing countSum array and compRoll array at the same time? Very cool. Hadn't thought of that. Less code is always better. Thanks adata post.

Take a look at,

int[] countSum=new int[7];

            for (x = 0; x < DICE; x++)
            {
                countSum[compRoll[x]]++;
            }
            Console.WriteLine();
            for(x=0;x<countSum.Length;x++)
                Console.Write("  " + countSum[x]);
            ..

Ok here is my program. I changed the rules a bit. Can anyone figure out why the compRoll and the userRoll always come out the same, resulting in a tie. I'm stuck.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            const int SIZE = 6;
            const int DICE = 5;
            int[] compRoll = new int[DICE];
            int[] userRoll = new int[DICE];
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            compRoll = ComputerRoll(x,compRoll,DICE);
            userRoll = UserRoll(x,compRoll,DICE);
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }

        static int[] ComputerRoll(int x, int[] compRoll, int DICE)
         {
             Random RandNum = new Random();
             Console.Write("{0,-20}", "Computer Rolled: ");
             for (x = 0; x < DICE; x++)
             {
                 compRoll[x] = RandNum.Next(1, 7);
                 Console.Write("{0} ", compRoll[x]);
             }
             return compRoll;
         }
         static int[] UserRoll(int x, int[] userRoll, int DICE)
         {
             Random RandNum = new Random();
             Console.Write("\n\n{0,-20}", "You Rolled: ");
             for (x = 0; x < DICE; x++)
             {
                 userRoll[x] = RandNum.Next(1, 7);
                 Console.Write("{0} ", userRoll[x]);
             }
             return userRoll;
         }
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }

        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points;
                    }
                }
                match++;
            }

            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {

            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}", "You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}

This way works, but I want to know why the previous one doesn't.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            Random RandNum = new Random();
            const int SIZE = 6;
            const int DICE = 5;
            int[] compRoll = new int[DICE]{RandNum.Next(1, 7), RandNum.Next(1, 7), 
                RandNum.Next(1, 7),RandNum.Next(1, 7), RandNum.Next(1, 7)};
            int[] userRoll = new int[DICE]{RandNum.Next(1, 7), RandNum.Next(1, 7), 
                RandNum.Next(1, 7),RandNum.Next(1, 7), RandNum.Next(1, 7)};
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            Console.Write("{0,-20}", "Computer Rolled: ");
            for (x = 0; x < DICE; x++)
            {

                Console.Write("{0,-2}", compRoll[x]);
            }

            Console.Write("\n\n{0,-20}", "You Rolled: ");
            for (x = 0; x < DICE; x++)
            {

                Console.Write("{0,-2}", userRoll[x]);
            }

            
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }
       
       /* Another way to populate the array with random #'s, but doesn't quite work.
        * static int[] ComputerRoll(int x, int[] compRoll, int DICE)
        {
            Random RandNum = new Random();
            Console.Write("{0,-20}", "Computer Rolled: ");
            for (x = 0; x < DICE; x++)
            {
                compRoll[x] = RandNum.Next(1, 7);
                Console.Write("{0} ", compRoll[x]);
            }
            return compRoll;
        }
        static int[] UserRoll(int x, int[] userRoll, int DICE)
        {
            Random RandNum = new Random();
            Console.Write("\n\n{0,-20}", "You Rolled: ");
            for (x = 0; x < DICE; x++)
            {
                userRoll[x] = RandNum.Next(1, 7);
                Console.Write("{0} ", userRoll[x]);
            }
            return userRoll;
        }*/
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }
        
        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points; 
                    }
                }
                match++;
            }
            
            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {

            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}","You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}

Look at line 32 of your code listing. You're passing the computer's array into your user function.

Here's a question for you. Your ComputerRoll and UserRoll functions are essentially performing the same task. Think whether or not you need two functions.

Look at line 32 of your code listing. You're passing the computer's array into your user function.

Here's a question for you. Your ComputerRoll and UserRoll functions are essentially performing the same task. Think whether or not you need two functions.

Ok fixed line 32. That was a typo but it did not fix the problem. I know I should be able to use just one function. When I get this working I can easily pass arguments to the one function right?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            const int SIZE = 6;
            const int DICE = 5;
            int[] compRoll = new int[DICE];
            int[] userRoll = new int[DICE];
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            compRoll = ComputerRoll(x,compRoll,DICE);
            userRoll = UserRoll(x,userRoll,DICE);
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }

        static int[] ComputerRoll(int x, int[] compRoll, int DICE)
         {
             Random RandNum = new Random();
             Console.Write("{0,-20}", "Computer Rolled: ");
             for (x = 0; x < DICE; x++)
             {
                 compRoll[x] = RandNum.Next(1, 7);
                 Console.Write("{0} ", compRoll[x]);
             }
             return compRoll;
         }
         static int[] UserRoll(int x, int[] userRoll, int DICE)
         {
             Random RandNum = new Random();
             Console.Write("\n\n{0,-20}", "You Rolled: ");
             for (x = 0; x < DICE; x++)
             {
                 userRoll[x] = RandNum.Next(1, 7);
                 Console.Write("{0} ", userRoll[x]);
             }
             return userRoll;
         }
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }

        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points;
                    }
                }
                match++;
            }

            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {

            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}", "You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}

OK, I ran your fixed program and see what is going on, and let this be your introduction to the fact that the Random class is not all that random!

When you create a Random instance, you can either pass in a seed value or you can use the parameter-less constructor (as you have). If you use a seed with a constant value, you will get the same random numbers out of the Random object every single time. If you do not provide a seed, the object will generate its own based on the current time, which is usually fine when there is the reasonable expectation that time will ellapse between instantiations of Random. But if the instantiations happen in rapid succession, it is just as if you instantiated the object using a constant seed!

No real time is ellapsing between your function calls, because the processor can perform a gazillion operations per second, which means it can do a fraction of a gazillion in a split-second. (Can you tell this is a scientific explanation?) As a result, your Random objects are auto-generating the same seed, so the "random" numbers they're spitting out happen to be the same.

One method to fix this is to pause the current thread for a miniscule amount of time, but long enough that the seed will be different.

compRoll = ComputerRoll(x, compRoll, DICE);
            System.Threading.Thread.Sleep(10);
            userRoll = UserRoll(x, userRoll, DICE);

With that, you should begin to see different values in your arrays.

Wow. Thanks apegram. I would have never been able to figure that out on my own. Thanks for the intro to the Random class. Very good information. I changed the sleep time to 20 and it seems to work better for shorter times between execution.

One question. Would it be more efficient to run the program one way or the other? In other words, get the random numbers when I declare the array or as I have done with a method using the for loop? I'm thinking that using a method might be a better overall practice.

Anyway here are the two working versions. You all have been an enormous help:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            Random RandNum = new Random();
            const int SIZE = 6;
            const int DICE = 5;
            int[] compRoll = new int[DICE]{RandNum.Next(1, 7), RandNum.Next(1, 7), 
                RandNum.Next(1, 7),RandNum.Next(1, 7), RandNum.Next(1, 7)};
            int[] userRoll = new int[DICE]{RandNum.Next(1, 7), RandNum.Next(1, 7), 
                RandNum.Next(1, 7),RandNum.Next(1, 7), RandNum.Next(1, 7)};
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            Console.Write("{0,-20}", "Computer Rolled: ");
            for (x = 0; x < DICE; x++)
            {

                Console.Write("{0,-2}", compRoll[x]);
            }

            Console.Write("\n\n{0,-20}", "You Rolled: ");
            for (x = 0; x < DICE; x++)
            {

                Console.Write("{0,-2}", userRoll[x]);
            }
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }
        
        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points; 
                    }
                }
                match++;
            }
            
            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {

            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}","You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            const int SIZE = 6;
            const int DICE = 5;
            int[] compRoll = new int[DICE];
            int[] userRoll = new int[DICE];
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            compRoll = ComputerRoll(x,compRoll,DICE);
            System.Threading.Thread.Sleep(20);
            userRoll = UserRoll(x,userRoll,DICE);
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }
        static int[] ComputerRoll(int x, int[] compRoll, int DICE)
         {
             Random RandNum = new Random();
             Console.Write("{0,-20}", "Computer Rolled: ");
             for (x = 0; x < DICE; x++)
             {
                 compRoll[x] = RandNum.Next(1, 7);
                 Console.Write("{0} ", compRoll[x]);
             }
             return compRoll;
         }
         static int[] UserRoll(int x, int[] userRoll, int DICE)
         {
             Random RandNum = new Random();
             Console.Write("\n\n{0,-20}", "You Rolled: ");
             for (x = 0; x < DICE; x++)
             {
                 userRoll[x] = RandNum.Next(1, 7);
                 Console.Write("{0} ", userRoll[x]);
             }
             return userRoll;
         }
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }

        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points;
                    }
                }
                match++;
            }

            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {

            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}", "You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}

Using random calls within the array initializer may work, but it's kind of inelegant, although someone else could be of a different opinion on that. A method is a reasonable way to go, although I still encourage you to simplify matters and have one method to generate the random numbers rather than 2.

Hey apegram. I did what you said and made one method for rolling the dice and added one more for printing the roll. I think this good enough for now.

I guess I'll mark this thread as solved. Thanks for everything guys. I'll see you around the forum.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            const int SIZE = 6;
            const int DICE = 5;
            string comp = "Computer rolled: ";
            string user = "You rolled: ";
            int[] compRoll = new int[DICE];
            int[] userRoll = new int[DICE];
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            compRoll = RollDice(x,compRoll,DICE);
            System.Threading.Thread.Sleep(20);
            userRoll = RollDice(x,userRoll,DICE);
            PrintDiceRoll(comp, x, compRoll, DICE);
            PrintDiceRoll(user, x, userRoll, DICE);
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }

        static int[] RollDice(int x, int[] diceRoll, int DICE)
         {
             Random RandNum = new Random();
             for (x = 0; x < DICE; x++)
             {
                 diceRoll[x] = RandNum.Next(1, 7);
             }
             return diceRoll;
         }
         static void PrintDiceRoll(string name, int x, int[] diceRoll, int DICE)
         {
             Console.Write("\n\n{0,-20}",name);
             for (x = 0; x < DICE; x++)
             {
                 Console.Write("{0} ", diceRoll[x]);
             }
         }
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }
        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points;
                    }
                }
                match++;
            }
            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {
            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}", "You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}

I know this has been marked as solved, but thought i'd chime in. The reason that you are getting random numbers when you generate them at the same time you declare the array is that you are using a single instance of the Random class. You have declared one RandNum at class level and used it for both. This avoids the problem of the seed issue. If DICE = 5 you are using the first 5 numbers the RandNum spits out for the comp then the next 5 for user.
If you use two instances of the RandNum (as you do in your methods) you use the first 5 for each. This means you need to ensure each instance has a different seed so that the first 5 arent the same.

You can put these together:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_3
/*Dice Game by Brett Bedillion 
 * Computer rolls five dice and user rolls five dice. 
 * Rank: 5 of a kind > 4 of a kind > 3 of a kind > 2 of a kind.
 * There is no full house, two pair, three pair, two three of a kind  in this game yet.
 * If both computer and user have a pair, higher dice value wins.
 * If both  computer and user pair values are same then game is a tie.*/
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring RandNum here ensures it is seeded once and all subsequent calls will be as random as Random gets
            Random RandNum = new Random();
            const int SIZE = 6;
            const int DICE = 5;
            string comp = "Computer rolled: ";
            string user = "You rolled: ";
            int[] compRoll = new int[DICE];
            int[] userRoll = new int[DICE];
            int[] compCountSum = new int[SIZE];
            int[] userCountSum = new int[SIZE];
            int compHandValue = 0;
            int userHandValue = 0;
            int count = 0;
            int match = 0;
            int points = 0;
            int x = 0;

            compRoll = RollDice(x,compRoll,DICE);
            userRoll = RollDice(x,userRoll,DICE);
            PrintDiceRoll(comp, x, compRoll, DICE);
            PrintDiceRoll(user, x, userRoll, DICE);
            CalcCountSum(x, count, SIZE, DICE, compRoll, compCountSum);
            CalcCountSum(x, count, SIZE, DICE, userRoll, userCountSum);
            compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
            userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
            DecideWinner(userHandValue, compHandValue);
        }

        static int[] RollDice(int x, int[] diceRoll, int DICE)
         {
             for (x = 0; x < DICE; x++)
             {
                 diceRoll[x] = RandNum.Next(1, 7); //here you are now using the class level RandNum
             }
             return diceRoll;
         }
         static void PrintDiceRoll(string name, int x, int[] diceRoll, int DICE)
         {
             Console.Write("\n\n{0,-20}",name);
             for (x = 0; x < DICE; x++)
             {
                 Console.Write("{0} ", diceRoll[x]);
             }
         }
        static void CalcCountSum(int x, int count, int SIZE,
            int DICE, int[] diceRoll, int[] countSum)
        {
            count = 1;
            while (count <= SIZE)
            {
                for (x = 0; x < DICE; x++)
                {
                    if (diceRoll[x] == count)
                    {
                        countSum[count - 1] = countSum[count - 1] + 1;
                    }
                }
                count++;
            }
        }
        static int CalcHandValue(int x, int match, int points, int SIZE,
           int DICE, int[] countSum, int handValue)
        {
            handValue = 0;
            match = 2;
            while (match <= DICE)
            {
                for (x = 0; x < SIZE; x++)
                {
                    points++;
                    if (countSum[x] == match)
                    {
                        handValue = points;
                    }
                }
                match++;
            }
            return handValue;
        }
        static void DecideWinner(int userHandValue, int compHandValue)
        {
            if (userHandValue > compHandValue)
            {
                Console.WriteLine("\n\n{0}", "You win!");
            }
            if (compHandValue > userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Computer wins.");
            }
            if (compHandValue == userHandValue)
            {
                Console.WriteLine("\n\n{0}", "Game is a tie.");
            }
        }
    }
}

Thanks for the clear explanation Ryshad.

For your example, wouldn't that put RandNum out of the scope of the method? I get an error when I run it by your example.

"The name 'RandNum' does not exist in the current context"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.