Click here to Skip to main content
15,913,133 members
Home / Discussions / C#
   

C#

 
AnswerRe: Domain name verification Pin
Luc Pattyn10-Dec-09 5:50
sitebuilderLuc Pattyn10-Dec-09 5:50 
GeneralRe: Domain name verification Pin
RugbyLeague10-Dec-09 5:56
RugbyLeague10-Dec-09 5:56 
GeneralRe: Domain name verification Pin
Luc Pattyn10-Dec-09 6:04
sitebuilderLuc Pattyn10-Dec-09 6:04 
QuestionDatabase column mapping - draw lines between items in DataGridView Pin
Scott Bass10-Dec-09 0:51
Scott Bass10-Dec-09 0:51 
QuestionArray list question Pin
dennis_max279-Dec-09 23:37
dennis_max279-Dec-09 23:37 
AnswerRe: Array list question Pin
Rob Philpott10-Dec-09 0:22
Rob Philpott10-Dec-09 0:22 
AnswerRe: Array list question Pin
dennis_max2710-Dec-09 1:32
dennis_max2710-Dec-09 1:32 
AnswerRe: Array list question Pin
Keith Barrow10-Dec-09 1:39
professionalKeith Barrow10-Dec-09 1:39 
First as suggested previously you need a 5 long array of ints. The method described above (checking for uniqueness) won't work as (2,3,4,5,6) is unique but not winning.
You will also find your code improves vastly if you study OO and work out how to refactor. The two guidlines I try to live by [and often fail] are "Don't Repeat Yourself" "Keep It Simple Stupid" (DRY-KISS).
For example

int randomNumber1 = random.Next(1, 7);<br />
int randomNumber2 = random.Next(1, 7);<br />
int randomNumber3 = random.Next(1, 7);<br />
int randomNumber4 = random.Next(1, 7);<br />
int randomNumber5 = random.Next(1, 7);

Fails the DRY test.

I've separated out an example Yahtzee Roller class for you, this is one implementation of many possible and is not meant to be perfect. It contains an array of roll results, abd an array of winning values, these are compared. The amin program creates an instance of the roller, and proceeds to roll until a winning set is found.

The roller:

public class YahtzeeRoller
{
    int[] rollResults = new int[]{0, 0, 0, 0, 0};
    int[] WinningValues = new int[] { 1, 2, 3, 4, 5 };
    
    Random random = new Random();

    public bool IsWinning
    {
        get
        {
            // This takes all the winning values and reports which are missing
            // in the roll results. If the count of this is 0, all winning values 
            // are present, so the user has won.
            // This requires .net 3.0 or above.
            return WinningValues.Except(rollResults).Count() == 0;
        }
    }

    public string CurrentRoll
    {
        get
        {
            // This is hacked just too show the results, not meant to be perfect.
            string result = "Roll =";
            foreach (int rollResult in rollResults)
                result += " " + rollResult;
            return result;
        }
    }

    public void Roll()
    {
        for (int i = 0; i < rollResults.Length; i++)
            rollResults[i] = random.Next(1, 7); 
    }   
}


Calling Program:

class Program
{
    static void Main(string[] args)
    {
        YahtzeeRoller yahtzeeRoller = new YahtzeeRoller();
        //The example program will roll until it wins.
        while (!yahtzeeRoller.IsWinning)
        {
            yahtzeeRoller.Roll();
            Console.Write(yahtzeeRoller.CurrentRoll);

            //Again, a hack implementation to get the results out.
            if (yahtzeeRoller.IsWinning)
                Console.WriteLine(" Yahtzee!");
            else
                Console.WriteLine(" Not Yahtzee !");
        }

        Console.ReadKey();
    }
}


Notice a few things. The re-factored code is shorter (and hopefully clearer) than the original. Additionally, it separates out the rolling functionality from the display [mostly] the use of CurrentRole property is debatable. You can now call Roll as many times as needed (it is not hard-coded), and if desired, easily change what constitutes a winning set of rolls.

I hope this helps!

CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks

AnswerRe: Array list question Pin
Luc Pattyn10-Dec-09 1:58
sitebuilderLuc Pattyn10-Dec-09 1:58 
GeneralRe: Array list question Pin
dennis_max2710-Dec-09 2:22
dennis_max2710-Dec-09 2:22 
GeneralRe: Array list question Pin
Luc Pattyn10-Dec-09 2:43
sitebuilderLuc Pattyn10-Dec-09 2:43 
AnswerRe: Array list question Pin
PIEBALDconsult10-Dec-09 5:57
mvePIEBALDconsult10-Dec-09 5:57 
QuestionSyntax error (missing operator) in query expression Pin
AlucardCode9-Dec-09 22:40
AlucardCode9-Dec-09 22:40 
AnswerRe: Syntax error (missing operator) in query expression Pin
OriginalGriff9-Dec-09 23:06
mveOriginalGriff9-Dec-09 23:06 
AnswerRe: Syntax error (missing operator) in query expression Pin
Ghydo9-Dec-09 23:17
Ghydo9-Dec-09 23:17 
GeneralRe: Syntax error (missing operator) in query expression Pin
AlucardCode9-Dec-09 23:34
AlucardCode9-Dec-09 23:34 
AnswerRe: Syntax error (missing operator) in query expression Pin
Scott Knestrick10-Dec-09 17:21
Scott Knestrick10-Dec-09 17:21 
QuestionBegin Invoke Example Pin
sureshhi9-Dec-09 22:33
sureshhi9-Dec-09 22:33 
AnswerRe: Begin Invoke Example: My Vote of 1 Pin
Keith Barrow9-Dec-09 23:38
professionalKeith Barrow9-Dec-09 23:38 
JokeRe: Begin Invoke Example Pin
Richard MacCutchan9-Dec-09 23:38
mveRichard MacCutchan9-Dec-09 23:38 
Questiondatabase Pin
farokhian9-Dec-09 22:24
farokhian9-Dec-09 22:24 
AnswerRe: database Pin
Eddy Vluggen10-Dec-09 0:35
professionalEddy Vluggen10-Dec-09 0:35 
AnswerRe: database Pin
Vimalsoft(Pty) Ltd10-Dec-09 2:08
professionalVimalsoft(Pty) Ltd10-Dec-09 2:08 
AnswerRe: database Pin
PIEBALDconsult10-Dec-09 4:01
mvePIEBALDconsult10-Dec-09 4:01 
QuestionToo many namespaces in a class Pin
Abhinav S9-Dec-09 22:07
Abhinav S9-Dec-09 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.