强制控制台在随机循环的代码猜测器中跳过已经猜测的字符

代码首先询问代码的位数。然后它会创建一个与答案一样长的随机字母串,并开始循环遍历字符,直到找到代码。每个匹配的字符都保存在同一个位置,直到每个字符匹配为止。


问题在于控制台不断猜测错误的字符,导致控制台继续猜测错误的字符并且从未解决它的情况。


所以,我希望控制台在猜到错误字符后不要尝试它们。


附带说明一下,如果有人对我如何更改代码以一次猜测一个字符有任何想法,然后继续下一个,请告诉我,谢谢。


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;


namespace ConsoleApp3

{

    class Program

    {

        private static Random random = new Random();

        public static string RandomString(int length)

        {

            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

            return new string(Enumerable.Repeat(chars, length)

              .Select(s => s[random.Next(s.Length)]).ToArray());

        }




        static void Main(string[] args)

        {

            Console.ForegroundColor = ConsoleColor.Green;



            string intro6 = "How many characters in the password? (USE INTEGERS)";

            foreach (char c in intro6)

            {

                Console.Write(c);

                Thread.Sleep(50);

            }

            Console.WriteLine("");

            string delta = Console.ReadLine();


            try

            {



                int passwordlength = Convert.ToInt32(delta);


                // BARRIER


                string password = RandomString(passwordlength);


                Random r = new Random();

                string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

                List<string> dictionary = new List<string>(new string[] {

            password

        });


                string word = dictionary[r.Next(dictionary.Count)];

                List<int> indexes = new List<int>();

                Console.ForegroundColor = ConsoleColor.Red;

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < word.Length; i++)

                {

                    sb.Append(letters[r.Next(letters.Length)]);

                    if (sb[i] != word[i])

                    {

                        indexes.Add(i);


                    }

                }


哔哔one
浏览 122回答 1
1回答

烙印99

正如 wubbler 在他的评论中提到的那样,用于创建随机密码的字符与用于猜测密码的字符之间似乎存在差异。去创造:const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";猜测:string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";当随机生成的密码包含任何数字时,这会导致程序无法猜测密码。通过将数字添加到可猜测的字符中,程序会更加一致地成功。至于请求:我希望控制台在猜到错误字符后不要尝试它们您可以通过为每个索引保留一个可猜测字符的集合来实现此目的,然后在猜测到给定索引后从它的集合中删除一个字符。下面的代码满足了这一点:static void Main(string[] args){&nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Green;&nbsp; &nbsp; string intro6 = "How many characters in the password? (USE INTEGERS)";&nbsp; &nbsp; foreach (char c in intro6)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.Write(c);&nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(50);&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine("");&nbsp; &nbsp; string delta = Console.ReadLine();&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int passwordlength = Convert.ToInt32(delta);&nbsp; &nbsp; &nbsp; &nbsp; // BARRIER&nbsp; &nbsp; &nbsp; &nbsp; string password = RandomString(passwordlength);&nbsp; &nbsp; &nbsp; &nbsp; Random r = new Random();&nbsp; &nbsp; &nbsp; &nbsp; string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";&nbsp; &nbsp; &nbsp; &nbsp; List<string> dictionary = new List<string>(new string[] { password });&nbsp; &nbsp; &nbsp; &nbsp; string word = dictionary[r.Next(dictionary.Count)];&nbsp; &nbsp; &nbsp; &nbsp; List<int> indexes = new List<int>();&nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Red;&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < word.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.Append(letters[r.Next(letters.Length)]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sb[i] != word[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexes.Add(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(sb.ToString());&nbsp; &nbsp; &nbsp; &nbsp; var charsToGuessByIndex = indexes.ToDictionary(k => k, v => letters);&nbsp; &nbsp; &nbsp; &nbsp; while (indexes.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = indexes.Count - 1; i >= 0; i--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = indexes[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var charsToGuess = charsToGuessByIndex[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb[index] = charsToGuess[r.Next(charsToGuess.Length)];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charsToGuessByIndex[index] = charsToGuess.Remove(charsToGuess.IndexOf(sb[index]), 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sb[index] == word[index])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexes.RemoveAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var output = sb.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < output.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (indexes.Contains(i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Red;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Cyan;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(output[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Green;&nbsp; &nbsp; &nbsp; &nbsp; string outro1 = "Password successfully breached. Have a nice day.";&nbsp; &nbsp; &nbsp; &nbsp; foreach (char c in outro1)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(20);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("");&nbsp; &nbsp; &nbsp; &nbsp; Thread.Sleep(100);&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }&nbsp; &nbsp; catch&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (delta is string)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ForegroundColor = ConsoleColor.Red;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("FATAL ERROR PRESS ENTER TO EXIT");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("welp, it was worth a try.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}charsToGuessByIndex跟踪每个索引可以猜测哪些字符,并在猜测字符的 for 循环内相应地更新:charsToGuessByIndex[index] = charsToGuess.Remove(charsToGuess.IndexOf(sb[index]), 1);
打开App,查看更多内容
随时随地看视频慕课网APP