Master mind in console [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to make some sort of mastermind game in the console where the computer generates a 3 digit code and you have to guess it. The computers also tells you which digit you get right after you take a guess. How would i best do this? I don't know a way to "chop up" input so that i can make the computer check it against the digit it has.
example:
computer generates random code: 123
you guess: 321
computer : -*- (the star indicates which digit you got right)
I have an idea about how i'd indicate what digits are right or not, but i don't know how to separate the input into parts

Convert the computer generated random number into a string. The user input is given as string anyway.
The string type has an indexer allowing you to access single characters
string s = "abc";
char a = s[0];
char b = s[1];
char c = s[2];
for (int i = 0; i < s.Length; i++) {
Console.WriteLine($"s[{i}] = '{s[i]}'");
}
You write char literals as
char ch = 'x';
using single quotes.
Example:
string userInput = Console.ReadLine();
int code = SomehowGenerateRandomCode();
string codeString = code.ToString();
// Let's assume that both numbers have 3 digits.
for (int i = 0; i < 3; i++) {
if(userInput[i] == codeString[i]) {
// digits are equal
} else {
// digits are different
}
}

To split an int to its digits you can convert to string an split like:
int i = 123;
string myInt = i.ToString();
List<int> digits =new List<int>();
foreach (char c in myInt)
{
digits.Add(int.Parse(c.ToString());
}

Related

Splitting numbers into list/array (e.g. 1998 to 1,9,9,8) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
this is my first post so excuse me if I am not being clear enough.
As the title says I want to split a number into smaller parts, for example 1998 to 1,9,9,8. I have looked around but I am stuck trying to figure out the right term to search for.
Reason why I want this done is to later multiply every other number with either 1 or 2 to be able to examine whether it's correct or not.
Sorry I can't provide any code because I am not sure how I should tackle this problem.
As #mjwills said, mod is what you want.
static IEnumerable<int> Digitize(int num)
{
while (num > 0)
{
int digit = num % 10;
yield return digit;
num = num / 10;
}
}
And then call it like so:
static void Main(string[] _)
{
int num = 1998;
int[] digits = Digitize(num).ToArray();
Console.WriteLine($"Original: {num}");
foreach (var digit in digits)
Console.WriteLine(digit);
}
make a string of that number and make it like this
```
int a = 1990;
int[] intArray = new int[a.ToString().Length];
int index = 0;
foreach( char ch in a.ToString())
{
intArray[index] = int.Parse(ch.ToString());
index++;
}

compare different combinations of a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a dictionary with key value pairs something like below
dict("A-B-C", "This is abc");
dict("X-Y-Z", "This is xyz");
so on
now when I receive an input say something like "B-A-C" the system should intelligent to know its "A-B-C" or if its "Z-Y-X" should map to "X-Y-Z", one way is to put all the combinations into the dictionary such that say dict("B-A-C", "A-B-C") but this may lead to more maintainability thing, just thinking if there is anything from .NET framework can address this issue more easily.
Is there an easy way to compare a string like below
A string either A-B or B-A is equal to A-B
A string either A-B-C or B-C-A or A-C-B or C-A-B or C-B-A equals to A-B-C
likewise a different combinations of A-B-C-D should equals to A-B-C-D
so as A-B-C-D-E
so on
A B C D E in the above example are a two letter non numeric word. something like
Su-Ma-Ju-Ve
UPDATE
For now I solved this with below code which returns the string tokens in the order which is recognized in the dictionary I maintain, not sure if its the best way but is solving the problem for now
string sortPls(string pls)
{
Dictionary<string, int> dctPls = new Dictionary<string, int>();
dctPls["Su"] = 1;
dctPls["Mo"] = 2;
dctPls["Ju"] = 3;
dctPls["Me"] = 4;
dctPls["Ve"] = 5;
dctPls["Ma"] = 6;
dctPls["Sa"] = 7;
string[] arrPls = pls.Split('-');
int j = 0;
string sortPls = string.Empty;
for(int i = 0; i < arrPls.Length; i++)
{
for (j = i + 1; j < arrPls.Length; j++)
{
if (dctPls[arrPls[j]] < dctPls[arrPls[i]])
{
string tmp = arrPls[i];
arrPls[i] = arrPls[j];
arrPls[j] = tmp;
}
}
}
for (int k = 0; k < arrPls.Length; k++)
sortPls += arrPls[k] + "-";
return sortPls.Remove(sortPls.Length - 1) ;
}
Yes. If looking for set equality, then HashSet<T> gives you the tools.
"ABBA".ToHashSet().SetEquals("BA")
Otherwise for an "anagram" type comparison, order the characters and compare sequences:
"CDAB".OrderBy(x=>x).SequenceEqual("DCBA".OrderBy(x=>x))
Split the string into individual letters, add the letters to a Set, and compare the sets using their Equals methods
void Main()
{
string check = "CADB";
string equalTo = new string(check.ToCharArray().OrderBy(x => x).ToArray());
Console.WriteLine(equalTo);
}
is just one way to do that.
I think, this will be the fastest way to do it:
bool StringEquals(string string1, string string2)
{
foreach (char ch in string1)
{
if (!string2.Contains(ch))
{
return false;
}
}
return true;
}
Sort the characters in each string, any two strings that have the same characters will have their sorted version the same. Stick the results into a `Dictionary>, with the sorted version as the key.

Creating a string using StringBuilder in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am in the process of converting a VB application. I am trying to create a string using StringBuilder as I cannot change a string using the Mid function. I have a loop although it only adds the first character to the string the rest is just white space. How can i add all the data to the string?
The code snippet is below; thanks for your help
int table = 0;
string tableData = Strings.StrDup(253, " ");
int i = 0;
string listData = null;
int pointer =0;
StringBuilder sb = new StringBuilder(tableData);
sb.Insert(0, Strings.StrDup(253, Strings.Chr(32)));
sb.Insert(0, typeOfTable + Strings.Chr(0));
pointer = 2;
for (i = 0; i <= lstTABLE.Items.Count -1; i++)
{
listData = lstTABLE.Items[i].ToString();
table = Convert.ToInt32(-(Conversion.Val(listData) * 10));
sb.Insert(pointer, Functions.FNCodeTwoChar(table));
pointer +=2;
}
sb.Insert (202,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblStartTable.Text))));
sb.Insert(205,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblEndTable.Text))));
sb.Insert(208, Strings.Space(36));
sb.Insert(244, " 0");
sb.Insert(248, " 0");
The answer to the question of "I have a loop although it only adds the first character to the string the rest is just white space. How can i add all the data to the string?" is:
The second character you add to the string is Strings.Chr(0) which is the string terminator character. When C# (or VB.Net for that matter) hit this character it stops reading the string.

For loop working in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am writing a program that finds the number of 1's in an array.. but the "scanning" inside the "for" loop takes place only once.
for (i = 0; i < 11; i++ )
{
k = Convert.ToInt32(Console.Read());
if (k==1)
{
ans++;
}
// Console.WriteLine("i == {0}", i);
}
Is this normal in C# or am I doing something wrong? I tried to search for this problem but cannot find any answers!
Do
Console.ReadLine()
instead of
Console.Read()
When you reach the first Console.Read() the inputstream is saved but only the first character is returned by the Read method. Subsequent calls to the Read method retrieve your input one character at a time from the inputstream.
Ex:
First iteration:
k = Console.Read(); //you input "abc1", k = a
Second iteration:
k = Console.Read(); // k = b
and so on.
When the last character in the inputstream is returned, the next call to Console.Read() will display the console again so you can input a new string and hit Enter.
Console.Read() docu
There's no array in your code, actually. Personally I'd rewrite this to:
string input = Console.ReadLine();
for (i = 0; i < input.Length; i++ )
{
int k = Convert.ToInt32(input.Substring(i, 1));
if (k==1)
{
ans++;
}
}
Which lets the user enter a string first and then does all the parsing. Of course, the user may enter more or less than exactly 11 characters in this code, so if you really need 11 numbers, I'd add extra messages and checks.
Also this code (as your original code) fail miserably if the user enters something non-numeric, so exception handling would be on the task list as well.
Try to use this code, I think it will answer your problem
string[] sample = { "1", "1", "3" };
var a = (from w in sample
where w == "1"
select w).ToList();
Console.Write(a.Count);

How do you loop Readline statements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a homework assignment and I am stuck on a certain part of the question.
How do I loop numbers regarding the readline statement.
In other words, suppose I have two numbers that I want to input. Instead of typing out the readline statement twice, how do I get a loop that would allow me to type the readline statement once?
have two numbers that I want to input. Instead of typing out the readline statement twice, how do I get a loop that would allow me to type the readline statement once
Well, if you want two numbers, then you're going to need two variables (or an array), so I assume you're trying to change this:
int firstNumber;
int secondNumber;
firstNumber = int.Parse(Console.ReadLine());
secondNumber = int.Parse(Console.ReadLine());
into something like this:
int[] myNumbers = new int[2];
for(int i = 0; i < 2; i++)
{
myNumbers[i] = int.Parse(Console.ReadLine());
}
but think about this:
Will the prompt be the same for both numbers? Or will the prompt change between inputs
Is it easier to assess the variables independently versus a part of the array/list?
Will having the numbers in a structure benefit other parts of my app (looping to get a sum, etc.)
I think a loop as fine so long as you don't end up with logic inside the lop that changes depending on which number you're using:
int[] myNumbers = new int[2];
for(int i = 0; i < 2; i++)
{
if i == 0 // bad
string prompt = "Enter the first number";
else
string prompt = "Enter the second number";
Console.WriteLine(prompt);
myNumbers[i] = int.Parse(Console.ReadLine());
}
int[] myInputs = new int[2];
for(int i=0; i < 2; ++i)
{
myInputs[i] = Int32.Parse(Console.ReadLine());
}
After this, myInputs[0] is the first value, and myInputs[1] is the second value.

Categories