- #1
Veronica_Oles
- 142
- 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.
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#.