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



代码从询问一个代码有多少位数开始。然后,它创建一个与答案一样长的随机字母串,并开始在字符之间循环,直到找到代码。每个匹配的字符都保持在同一位置,直到每个字符匹配为止。

问题是控制台不断猜测错误的字符,导致控制台继续猜测错误的角色,但始终无法解决。

因此,我希望控制台在猜测错误字符后不要尝试它们。

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

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);
}
}
Console.WriteLine(sb.ToString());
while (indexes.Count > 0)
{
int index;
Thread.Sleep(10);
Console.Clear();
for (int i = indexes.Count - 1; i >= 0; i--)
{
index = indexes[i];
sb[index] = letters[r.Next(letters.Length)];
if (sb[index] == word[index])
{
indexes.RemoveAt(i);


}

}
var output = sb.ToString();
for (int i = 0; i < output.Length; i++)
{
if (indexes.Contains(i))
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
Console.Write(output[i]);
}
Console.WriteLine();

}
Console.ForegroundColor = ConsoleColor.Green;
string outro1 = "Password successfully breached. Have a nice day.";
foreach (char c in outro1)
{
Console.Write(c);
Thread.Sleep(20);
}
Console.WriteLine("");
Thread.Sleep(100);

Console.ReadLine();
}
catch
{

if (delta is string)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Clear();
Console.WriteLine("FATAL ERROR PRESS ENTER TO EXIT");

Console.ReadLine();
}
else
{
Console.WriteLine("welp, it was worth a try.");
Console.ReadLine();
}


}

}
}
}

正如wubbler在评论中提到的,用于创建随机密码的字符和用于猜测密码的字符之间似乎存在差异。

创建:

const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

猜测:

string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

当随机生成的密码包含任何数字时,这会导致程序无法猜测密码。通过将数字添加到可猜测的字符中,程序可以更加一致地成功。

至于请求:

我希望控制台在猜测错误字符后不要尝试这些字符已经

您可以通过为每个索引保留一个可猜测字符的集合来实现这一点,然后在猜测后从给定索引的集合中删除一个字符。以下代码满足此要求:

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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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);
}
}
Console.WriteLine(sb.ToString());
var charsToGuessByIndex = indexes.ToDictionary(k => k, v => letters);
while (indexes.Count > 0)
{
int index;
Thread.Sleep(10);
Console.Clear();
for (int i = indexes.Count - 1; i >= 0; i--)
{
index = indexes[i];
var charsToGuess = charsToGuessByIndex[index];
sb[index] = charsToGuess[r.Next(charsToGuess.Length)];
charsToGuessByIndex[index] = charsToGuess.Remove(charsToGuess.IndexOf(sb[index]), 1);
if (sb[index] == word[index])
{
indexes.RemoveAt(i);
}
}
var output = sb.ToString();
for (int i = 0; i < output.Length; i++)
{
if (indexes.Contains(i))
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
Console.Write(output[i]);
}
Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.Green;
string outro1 = "Password successfully breached. Have a nice day.";
foreach (char c in outro1)
{
Console.Write(c);
Thread.Sleep(20);
}
Console.WriteLine("");
Thread.Sleep(100);
Console.ReadLine();
}
catch
{
if (delta is string)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Clear();
Console.WriteLine("FATAL ERROR PRESS ENTER TO EXIT");

Console.ReadLine();
}
else
{
Console.WriteLine("welp, it was worth a try.");
Console.ReadLine();
}
}
}

charsToGuessByIndex跟踪每个索引可以猜测哪些字符,并在猜测字符的for循环中进行相应更新:

charsToGuessByIndex[index] = charsToGuess.Remove(charsToGuess.IndexOf(sb[index]), 1);

最新更新