Master Mind - computer game programming in C#

In summary, the program is a game where the user must guess the correct order and color of three hidden blocks. The computer will display how many colors are correct and how many are in the right position after each guess. There is currently no statement for determining the winner. The program could also use some optimization to eliminate unnecessary operations.
  • #1

Homework Statement


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.

Homework Equations




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#.
 
Physics news on Phys.org
  • #2
Veronica_Oles said:
Cannot find what is wrong with my program
Does it work? If not, what exactly does not work?
Veronica_Oles said:
I do not know where to fit in a statement that tells the winner if they have won.
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).
 
  • #3
mfb said:
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).

Okay thank you for the suggestions will try and work it out.
 

Suggested for: Master Mind - computer game programming in C#

Replies
8
Views
653
Replies
4
Views
643
Replies
11
Views
722
Replies
8
Views
1K
Replies
15
Views
707
Replies
4
Views
843
Replies
7
Views
1K
Replies
2
Views
719
Replies
2
Views
3K
Back
Top