Master Mind - computer game programming in C#

Click For Summary
SUMMARY

The forum discussion centers on a C# implementation of the Master Mind game, where users guess the colors and order of three hidden blocks. The code provided includes methods for checking correct colors and their positions, but lacks a definitive win condition statement. Users expressed concerns about the logic for counting correct guesses, especially regarding repeated colors, and pointed out unnecessary operations in the code. Suggestions were made to improve the clarity and functionality of the program.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with console applications in C#
  • Knowledge of random number generation in programming
  • Basic concepts of game logic and user input handling
NEXT STEPS
  • Implement a win condition check in the game logic
  • Refactor the color checking methods to handle repeated guesses correctly
  • Optimize the random number generation to avoid unnecessary checks
  • Explore design patterns for game development in C#
USEFUL FOR

C# developers, game programmers, and students learning about console applications and game logic implementation.

Veronica_Oles
Messages
141
Reaction score
3

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
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).
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
14K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K