Click here to Skip to main content
15,919,245 members
Home / Discussions / C#
   

C#

 
AnswerRe: Is there a use for... Pin
PIEBALDconsult26-Aug-11 14:46
mvePIEBALDconsult26-Aug-11 14:46 
GeneralRe: Is there a use for... Pin
Narf the Mouse26-Aug-11 15:05
Narf the Mouse26-Aug-11 15:05 
AnswerRe: Is there a use for... Pin
Narf the Mouse26-Aug-11 15:18
Narf the Mouse26-Aug-11 15:18 
GeneralRe: Is there a use for... Pin
BillWoodruff27-Aug-11 18:16
professionalBillWoodruff27-Aug-11 18:16 
GeneralRe: Is there a use for... Pin
Narf the Mouse27-Aug-11 19:36
Narf the Mouse27-Aug-11 19:36 
AnswerRe: Is there a use for... Pin
jschell28-Aug-11 8:03
jschell28-Aug-11 8:03 
GeneralRe: Is there a use for... Pin
Narf the Mouse28-Aug-11 8:56
Narf the Mouse28-Aug-11 8:56 
AnswerRe: Is there a use for... Pin
Narf the Mouse28-Aug-11 11:33
Narf the Mouse28-Aug-11 11:33 
And here is a heavily-commented "Guess My Number" game using this paradigm (granted, it's not runnable without the classes, but it is heavily-commented. Should I release that code? I've no idea if anyone is the least bit intruiged):

C#
// A simple "Guess my number" program.

            // The length of the number to guess.
            DTVar<int> length = 7;
            // A starting phrase.
            Console.WriteLine("Guess my Number, a {0}-digit number.", length);
            // True if or when the player guesses entirely correctly.
            DTVar<bool> allCorrect = false;
            // A function that matches the output of two DTArgs<char> arguments,
            // and returns a string with X's in all locations where they don't match.
            DTFunc<char, char, string> matchingF = new DTFunc<char, char, string>(
                (a, b) =>
                {
                    // Our return string.
                    string r = "";
                    // If our first DTArgs<char> has values remaining,
                    while (a.Count > 0)
                    {
                        // We can call a DTArgs to return the next argument,
                        // or we can convert it.
                        // As DTBase<T> works on the principle that
                        // the thing is what it returns when called,
                        // the conversion can be implicit.
                        // A DTArgs is an argument queue, essentially.
                        char a1 = a;
                        char b1 = b.Call;

                        // If the two characters match, add that character to the string.
                        // if they don't match, add 'X' to the string.
                        r += (a1 == b1 ? a1 : 'X');
                    }
                    return r;
                }
            );
            // A function to generate a new "phone number".
            DTBase<string> numberF = new DTFunc<string>(
                delegate()
                {
                    // Stores "length" number of random integers from 0 to 9 in a string.
                    string r = "";
                    // "length" may be a DTVar<int>, but because the use is unambiguous,
                    // it can use implicit conversion.
                    for (int t = 0; t < length; ++t) r += Rand.Next(10);
                    // Returns the string "phone number".
                    return r;
                }
            );
            // A function to read a new guess from the console.
            DTBase<string> guessF = new DTFunc<string>(delegate() { return Console.ReadLine(); });
            // A function to checks if the first string argument is correct by
            // checking to see if it contains a 'X'.
            // Only one argument should be entered at a time.
            DTFunc<string, bool> correctF = new DTFunc<string, bool>(a => { return !a.Call.Contains('X'); });


            // Get our number to guess.
            DTVar<string> number = numberF.Call;
            // The maximum number of guesses allowed.
            int count = 20;
            do
            {
                // We call the guess function to get the player's next guess.
                DTVar<string> guess = guessF.Call;

                // We can either call or convert the number and the guess,
                // either works.
                // DTArgs<T>.AddRange can be given an IEnumerable to add
                // everything in the enumerable to the arguments.
                // In this case, DTArgs<T> is DTArgs<char> and the
                // IEnumerable is a string.
                matchingF.Args1.AddRange = number.Call;
                // We could just call "guessF" here, but that might be
                // too ambiguous for the reader.
                matchingF.Args2.AddRange = (string)guess;

                // Likewise, we can convert-call the matching function,
                // now that it has proper arguments.
                DTVar<string> result = (string)matchingF;

                // Add the result string to the arguments of the function
                // that checks if the result string is entirely correct,
                // then store that value in "allCorrect".
                // As "allCorrect" is a DTVar<bool>, we can use implicit
                // conversion.
                // Again, we could directly store "matchingF" in the args,
                // (that is, its call).
                correctF.Args.Add = result;
                allCorrect = correctF.Call;
                
                // Write the result to the console.
                Console.WriteLine(result.Call);
            } while (!allCorrect && --count >= 0);

            // Win/lose output.
            Console.WriteLine("You {0}", (allCorrect ? "Win! :)" : "Loose. :("));

GeneralRe: Is there a use for... [modified] Pin
BillWoodruff28-Aug-11 19:37
professionalBillWoodruff28-Aug-11 19:37 
AnswerRe: Is there a use for... Pin
BobJanova30-Aug-11 0:20
BobJanova30-Aug-11 0:20 
GeneralRe: Is there a use for... Pin
Narf the Mouse30-Aug-11 0:38
Narf the Mouse30-Aug-11 0:38 
GeneralRe: Is there a use for... Pin
BobJanova30-Aug-11 6:52
BobJanova30-Aug-11 6:52 
AnswerRe: Is there a use for... Pin
GParkings1-Sep-11 7:04
GParkings1-Sep-11 7:04 
GeneralRe: Is there a use for... Pin
Narf the Mouse1-Sep-11 7:16
Narf the Mouse1-Sep-11 7:16 
GeneralRe: Is there a use for... Pin
GParkings1-Sep-11 7:23
GParkings1-Sep-11 7:23 
GeneralRe: Is there a use for... Pin
Narf the Mouse1-Sep-11 8:26
Narf the Mouse1-Sep-11 8:26 
QuestionInheritance Pin
lukeer26-Aug-11 3:47
lukeer26-Aug-11 3:47 
AnswerRe: Inheritance Pin
Rob Philpott26-Aug-11 3:55
Rob Philpott26-Aug-11 3:55 
AnswerRe: Inheritance Pin
MicroVirus26-Aug-11 4:17
MicroVirus26-Aug-11 4:17 
GeneralMessage Removed Pin
26-Aug-11 5:40
mentorNot Active26-Aug-11 5:40 
GeneralRe: Inheritance Pin
MicroVirus26-Aug-11 6:09
MicroVirus26-Aug-11 6:09 
GeneralMessage Removed Pin
26-Aug-11 7:10
mentorNot Active26-Aug-11 7:10 
GeneralRe: Inheritance Pin
jschell26-Aug-11 8:36
jschell26-Aug-11 8:36 
GeneralRe: Inheritance Pin
MicroVirus26-Aug-11 14:05
MicroVirus26-Aug-11 14:05 
GeneralMessage Removed Pin
26-Aug-11 15:00
mentorNot Active26-Aug-11 15:00 

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.