1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Master Mind - computer game programming in C#

  1. May 6, 2016 #1
    1. The problem statement, all variables and given/known data
    Create this website:

    In this game, there are four different coloured blocks (red, green, blue, and yellow). The computer hides three different coloured blocks from the user. The user then tries to guess the colours and the order of the blocks. After guessing the colour of the three hidden blocks, the computer displays:

    • how many of the colours are correct and
    • how many of the colours are in the right position.
    Based on this information, the user can make another guess and so on until the user has determined the correct order and colour of the blocks.

    2. Relevant equations


    3. The attempt at a solution
    String ret;

    ret = RandomNumbers();

    int intUserGuess1 = 0;// This variable will be used to store the first guess of the user inputted into the console
    int intUserGuess2 = 0;// This variable will be used to store the second guess of the user inputted into the console
    int intUserGuess3 = 0;// This variable will be used to store the third guess of the user inputted into the console

    int intBlock1 = int.Parse(ret.Substring(0, 1));
    int intBlock2 = int.Parse(ret.Substring(1, 1));
    int intBlock3 = int.Parse(ret.Substring(2, 1));

    int intPlayGames = 1;

    int intGames = 0;

    Console.WriteLine("Welcome to the game where you choose three colours and attempt to match them with the console's numbers! Enjoy!");

    while (intPlayGames == 1)
    {
    intGames++;

    Console.WriteLine("Please enter Guess Code Number 1 [1-Red, 2-Blue, 3-Green, 4-Yellow]: ");
    intUserGuess1 = int.Parse(Console.ReadLine());// This will accept the users first guess and store it under the variable intUserGuess1

    Console.WriteLine("Please enter Guess Code Number 2 [1-Red, 2-Blue, 3-Green, 4-Yellow]: ");
    intUserGuess2 = int.Parse(Console.ReadLine());// This will accept the users second guess and store it under the variable intUserGuess2

    Console.WriteLine("Please enter Guess Code Number 3 [1-Red, 2-Blue, 3-Green, 4-Yellow]: ");
    intUserGuess3 = int.Parse(Console.ReadLine());// This will accept the users third guess and store it under the variable intUserGuess3


    Console.WriteLine("You have guessed " + checkCorrectColour(intBlock1, intBlock2, intBlock3, intUserGuess1, intUserGuess2, intUserGuess3).ToString() + " colours correctly.");

    Console.WriteLine("You have guessed " + checkColoursPosition(intBlock1, intBlock2, intBlock3, intUserGuess1, intUserGuess2, intUserGuess3).ToString() + " colours in the correct position.");

    Console.WriteLine();

    Console.WriteLine("Play Again [1] Yes [2] No: ");
    intPlayGames = int.Parse(Console.ReadLine());

    if (intPlayGames == 2)
    {
    intPlayGames = 0;
    }


    Console.WriteLine();
    }

    Console.WriteLine("Bye! Thank you for playing!");


    Console.ReadKey();// Keeps console window open

    }

    static int checkCorrectColour(int a, int b, int c, int b1, int b2, int b3)// This is a new method by the name of checkCorrectColour
    {
    int num = 0;// This variable will be used to store the number

    if (b1 == a || b1 == b || b1 == c)
    {
    num++;
    }

    if (b2 == a || b2 == b || b2 == c)
    {
    num++;
    }

    if (b3 == a || b3 == b || b3 == c)
    {
    num++;
    }

    return num;// This will return the number to the main method
    }

    static int checkColoursPosition(int a, int b, int c, int b1, int b2, int b3)
    {
    int num = 0;

    if (b1 == a)
    {
    num++;
    }

    if (b2 == b)
    {
    num++;
    }

    if (b3 == c)
    {
    num++;
    }

    return num;// This will return the number to the main method
    }

    static String RandomNumbers()
    {

    String result = "";

    Random r = new Random();

    int n1, n2, n3;
    Boolean flag = true;
    Boolean flag1 = true;

    n1 = r.Next(1, 5);
    result = n1.ToString();

    n2 = r.Next(1, 5);
    while (flag == true)
    {

    if (n2 == n1)
    {
    n2 = r.Next(1, 5);
    flag = true;

    }

    else
    {

    flag = false;
    result = result + n2.ToString();

    }

    }

    n3 = r.Next(1, 5);
    while (flag1 == true)
    {

    if (n3 == n1 || n3 == n2)
    {
    n3 = r.Next(1, 5);
    flag1 = true;

    }

    else
    {

    flag1 = false;
    result = result + n3.ToString();

    }

    }


    return result;// This will return the result to the main method
    }


    }
    }

    Cannot find what is wrong with my program and I do not know where to fit in a statement that tells the winner if they have won. This program is in c#.
     
  2. jcsd
  3. May 6, 2016 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Does it work? If not, what exactly does not work?
    I would do that at the place where you tell the user how many colors are in the correct position.


    A design question: if the correct colors are "1 2 3" and the user guesses "1 1 1", do you want to tell the user they have 3 correct color guesses? That's what you currently do. Sure, "1" does appear, but not as often as that reply could suggest As you do not allow repeated colors, you know that 3 of 4 colors are used, so that number is more of a "guess the missing color" anyway.

    There are various pointless operations in the code (e.g. setting flag=true while you can be sure it is true, or setting intPlayGames to 0 if it is 2).
     
  4. May 6, 2016 #3
    Okay thank you for the suggestions will try and work it out.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Master Mind - computer game programming in C#
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)

  3. C: String arithmetic (Replies: 2)

Loading...