该代码旨在通过创建一个随机字符串开始,该字符串具有用户选择的字符数。然后,它会随机循环字符,直到一个字符与字符串匹配,在这种情况下,它会锁定它并将其着色为青色。任何已尝试用于字符串的字符都将被忽略。
问题在于程序同时猜测所有字符,而不是按预期一个接一个地猜测并在完成当前字符后继续下一个字符。这导致完成较小的字符串和较长的字符串之间的差异是对数而不是加法。
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.WriteLine("How many characters in the password?");
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(50);
Console.Clear();
拉风的咖菲猫
相关分类