Thread: Master Mind

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Thumbs up Master Mind

    I wrote a Mastermind game in C#. It is my first C# program and I only got help from MSDN and you. I attached the code. Please tell me how could I make it better and the problems it has.

    Thank You.

    NOTE: File uploader doesn't let me upload *.cs files so I renamed them.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I thought the standard extension for C# files is .cs so as to not be confused with C source files.

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Quote Originally Posted by siavoshkc
    NOTE: File uploader doesn't let me upload *.cs files so I renamed them.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Please tell me how could I make it better and the problems it has.
    I've been working on too many large .NET projects. The first thing out of my mouth was "that's it?".
    My best code is written with the delete key.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    private static string GetUserGuessString(uint charNum)
    {
        string userGuess = "";
        while (userGuess.Length < charNum)
        {
            char userGuessChar = Console.ReadKey(true).KeyChar;
            if (userGuessChar >= 'a' && userGuessChar <= 'z')
            {
                Console.Write(userGuessChar);
                userGuess += userGuessChar;
            }
        }
        return userGuess;            
    }
    Classic example of when to use a StringBuilder instead of a string. Your while loop generates one new string every pass through the while loop, making more garbage for the GC. A few simple changes improves the behavior.
    Code:
    private static string GetUserGuessString(uint charNum)
    {
        StringBuilder userGuess = new StringBuilder();
        while (userGuess.Length < charNum)
        {
            char userGuessChar = Console.ReadKey(true).KeyChar;
            if (userGuessChar >= 'a' && userGuessChar <= 'z')
            {
                Console.Write(userGuessChar);
                userGuess.Append(userGuessChar);
            }
        }
        return userGuess.ToString();            
    }
    You'll want to do this any place you're using the += operator on strings. Also, remember that you can declare variables pretty much anywhere, so take advantage of it and declare them as close as possible to where they're used. It's mostly a style thing, but it also helps other people grok your code. On any non-trivial method, I prefer to enclose my locals in an extra pair of brackets to restrict their scope further.

    If you're striving towards CLS compliance, you'll want to add the [assembly: System.CLSCompliant(true)] attribute to your AssemblyInfo.cs. If you get FxCop, a useful code diagnostic tool, you may see that the interface to your GameCore class is not CLS compliant due to the uints as arguments. Of course, since your GameCore class is a private class, it may ignore that issue altogether.
    Last edited by pianorain; 09-17-2006 at 09:07 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I changed strings to StringBuilder and added the attribute. I also added back space functionality.

    [EDIT]
    I downloaded that tool and analyzed the code. It just says your assembly should have a strong name.
    Last edited by siavoshkc; 09-18-2006 at 07:34 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rtfm
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-13-2003, 05:19 PM
  2. WANTED: Contest Master
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-23-2003, 10:15 PM
  3. Lost source... losing mind...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-27-2001, 12:31 AM