0

Im facing issues when im attempting to make comparison between a generated code which is stored in a array and a users "Guess" which is stored in a different array

Im using a random number generator to store values in a array to act as a code as shown. NumbersArray has a size defined by a users input.

Random r = new Random();
for (int i = 0; i < numbersArray.Length; i++)
{
   numbersArray[i] = r.Next(1, digitRange); //digitRange represents the maximum digit value, such as not exceeding "9"
   Console.Write(numbersArray[i]);
}

This i know stores each individual digit as a place on the array.

Then the user attempts to guess the code, i then store the attempted guess on an array but currently it stores the whole number in one place on the array.

for (int i = 0; i < N; i++) // where "N" is the defined array length
{
    Console.WriteLine("\nWhat is your guess?");
    currentGuessArray[i] = Convert.ToInt32(Console.ReadLine());
}

How can i store the users input in the second array as each individual digit in a place on the array so both arrays have 1 digit per a position.

Amit
  • 1,821
  • 1
  • 17
  • 30
Ross
  • 19
  • 1
  • 9
  • This feels like a XY problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why are you doing this? Why do you want to store one digit per position? – mjwills May 06 '18 at 14:08
  • @mjwills because i want to compare each digit of the code to the guess the user has made, for example if the code is 1234 and the user guesses 3111 my response would be the code contains the number 3 and 1 but in different positions. Unless there is a simpler way of doin it – Ross May 06 '18 at 14:53
  • 1
    Sounds like https://en.wikipedia.org/wiki/Mastermind_(board_game) – Hans Kesting May 06 '18 at 17:33
  • You may be right! – Ross May 06 '18 at 17:44
  • Possible duplicate of [How to compare arrays in C#?](https://stackoverflow.com/questions/4423318/how-to-compare-arrays-in-c) – mjwills May 06 '18 at 21:42

1 Answers1

1

As you know , a string is a sequence of characters , So you could do something like this :

Console.WriteLine("\nWhat is your guess?");
string userGuess = Console.ReadLine();
// Storing each individual character from userGuess to currentGuessArray
for (int i = 0; i < numbersArray.Length; i++)
{
     // You have to use ToString() method to make sure that the digits will be
     // converted to integer not their ASCI values
     currentGuessArray[i] = Convert.ToInt32(userGuess[i].ToString());
}

The idea is to store the number that the user enters in a string variable , then store each individual digits from that number to your currentGuessArray . This way you will have only one digit per position in the array .

youness
  • 11
  • 2
  • Thanks for that, if every digit is correct when the guess is compared to the code generated is there a way of outputting something like "You have guessed the code" if you understand what i mean, i will edit the main post to attempt to explain further – Ross May 06 '18 at 17:17
  • Yes that's easy , but I need to understand something . When you compare the values in the two arrays , do you also compare their positions ? For example if the generated code was like this : 12345 , and I entered something like this 25314 , is it counted as a successful guess ? – youness May 07 '18 at 00:43
  • I compare their positions aswell. for example if the code is 1234 and the user guesses 3111 my response would be the code contains the number 3 and 1. If the guess is 3111 i want their to be a end condition for example which then states something along the lines of "Code has been guessed" i used sequenceEquals to do this but i need something which isn't a extension, thanks again buddy – Ross May 07 '18 at 10:39
  • If you want to compare the positions as well, you'll have to do that using a loop and compare each value from the first array with it's position to an other value from the second array. You're welcome. – youness May 07 '18 at 21:57