如何用棍棒获得用ascii艺术编码的数字?
我必须得到被棍子淹没的小人物。
第一步是获得4行而不是用Alphabet控制它。
我在字符串[]
中获取文本string[] lines = File.ReadAllLines("SourceFile.txt");
前4行符合第[3]行。 我怎样才能控制同一位置的不同线? 它就像一个二维数组,或者我必须做其他事情?
答案 0 :(得分:1)
首先,您需要一个存储符号模式和指标的对象(在您的情况下为数字)。此对象还有一种在给定数组中识别其模式的方法:
public class AsciiNumber
{
private readonly char[][] _data;
public AsciiNumber(char character, char[][] data)
{
this._data = data;
this.Character = character;
}
public char Character
{
get;
private set;
}
public int Width
{
get
{
return this._data[0].Length;
}
}
public int Height
{
get
{
return this._data.Length;
}
}
public bool Match(string[] source, int startRow, int startColumn)
{
if (startRow + this.Height > source.Length)
{
return false;
}
for (var i = startRow; i < startRow + this.Height; i++)
{
var row = source[i];
if (startColumn + this.Width > row.Length)
{
return false;
}
for (var j = startColumn; j < startColumn + this.Width; j++)
{
if (this._data[i - startRow][j - startColumn] != row[j])
{
return false;
}
}
}
return true;
}
}
然后你可以创建像你处理的字母(我只使用数字1和3):
public static class Alphabet
{
private static readonly AsciiNumber Number1 = new AsciiNumber('1', new[]{
new []{'|'},
new []{'|'},
new []{'|'},
new []{'|'},
});
private static readonly AsciiNumber Number3 = new AsciiNumber('3', new[]{
new []{'-','-','-'},
new []{' ',' ','/'},
new []{' ',' ','\\'},
new []{'-','-','-'},
});
public static readonly IEnumerable<AsciiNumber> All = new[] { Number1, Number3 };
}
假设源文件中的数字具有permament和相等的高度,您可以尝试这样的代码:
string[] lines = File.ReadAllLines("SourceFile.txt");
var lineHeight = 4;
var text = new StringBuilder();
for (var i = 0; i < lines.Length; i += lineHeight)
{
var j = 0;
while (j < lines[i].Length)
{
var match = Alphabet.All.FirstOrDefault(character => character.Match(lines, i, j));
if (match != null)
{
text.Append(match.Character);
j += match.Width;
}
else
{
j++;
}
}
}
Console.WriteLine("Recognized numbers: {0}", text.ToString());
N.B。如果行高超过文件,则必须改进上面的代码。
答案 1 :(得分:0)
假设我们有这个文本,我们想要解析其中的数字:
--- --- | | | -----
/ _| | |__| |___
\ | | | |
-- --- | | ____|
首先,我们应该删除任何不必要的空格(或tab,如果存在)并在数字之间放置一个分隔符char(例如$
),获得类似于此的内容:
$---$---$|$| |$-----$
$ / $ _|$|$|__|$|___ $
$ \ $| $|$ |$ |$
$-- $---$|$ |$____|$
现在我们应该使用Split
作为分隔符,在文本的每一行调用$
函数,然后将结果与字母表进行比较。
让我们看看如何使用代码执行此操作。给定要解析string[] lines
的文本,我们将创建一个扩展方法来删除不必要的空格并改为放置separator
字符/字符串:
public static class StringHelperClass
{
// Extension method to remove any unnecessary white-space and put a separator char instead.
public static string[] ReplaceSpacesWithSeparator(this string[] text, string separator)
{
// Create an array of StringBuilder, one for every line in the text.
StringBuilder[] stringBuilders = new StringBuilder[text.Length];
// Initialize stringBuilders.
for (int n = 0; n < text.Length; n++)
stringBuilders[n] = new StringBuilder().Append(separator);
// Get shortest line in the text, in order to avoid Out Of Range Exception.
int shorterstLine = text.Min(line => line.Length);
// Temporary variables.
int lastSeparatorIndex = 0;
bool previousCharWasSpace = false;
// Start processing the text, char after char.
for (int n = 0; n < shorterstLine; ++n)
{
// Look for white-spaces on the same position on
// all the lines of the text.
if (text.All(line => line[n] == ' '))
{
// Go to next char if previous char was also a white-space,
// or if this is the first white-space char of the text.
if (previousCharWasSpace || n == 0)
{
previousCharWasSpace = true;
lastSeparatorIndex = n + 1;
continue;
}
previousCharWasSpace = true;
// Append non white-space chars to the StringBuilder
// of each line, for later use.
for (int i = lastSeparatorIndex; i < n; ++i)
{
for (int j = 0; j < text.Length; j++)
stringBuilders[j].Append(text[j][i]);
}
// Append separator char.
for (int j = 0; j < text.Length; j++)
stringBuilders[j].Append(separator);
lastSeparatorIndex = n + 1;
}
else
previousCharWasSpace = false;
}
for (int j = 0; j < text.Length; j++)
text[j] = stringBuilders[j].ToString();
// Return formatted text.
return text;
}
}
然后,在Main
方法中,我们将使用:
lines = lines.ReplaceSpacesWithSeparator("$");
和
ASCIINumbersParser parser = new ASCIINumbersParser(lines, "$");
其中ASCIINumbersParser
是这个类:
public class ASCIINumbersParser
{
// Will store a list of all the possible numbers
// found in the text.
public List<string[]> CandidatesList { get; }
public ASCIINumbersParser(string[] text, string separator)
{
CandidatesList = new List<string[]>();
string[][] candidates = new string[text.Length][];
for (int n = 0; n < text.Length; ++n)
{
// Split each line in the text, using the separator char/string.
candidates[n] =
text[n].Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
}
// Put the strings in such a way that each CandidateList item
// contains only one possible number found in the text.
for (int i = 0; i < candidates[0].Length; ++i)
CandidatesList.Add(candidates.Select(c => c[i]).ToArray());
}
}
此时,我们将使用以下类生成某些数字的ASCII艺术表示,并将结果与原始字符串进行比较(在Main
中):
public static class ASCIINumberHelper
{
// Get an ASCII art representation of a number.
public static string[] GetASCIIRepresentationForNumber(int number)
{
switch (number)
{
case 1:
return new[]
{
"|",
"|",
"|",
"|"
};
case 2:
return new[]
{
"---",
" _|",
"| ",
"---"
};
case 3:
return new[]
{
"---",
" / ",
@" \ ",
"-- "
};
case 4:
return new[]
{
"| |",
"|__|",
" |",
" |"
};
case 5:
return new[]
{
"-----",
"|___ ",
" |",
"____|"
};
default:
return null;
}
}
// See if two numbers represented as ASCII art are equal.
public static bool ASCIIRepresentationMatch(string[] number1, string[] number2)
{
// Return false if the any of the two numbers is null
// or their lenght is different.
// if (number1 == null || number2 == null)
// return false;
// if (number1.Length != number2.Length)
// return false;
if (number1?.Length != number2?.Length)
return false;
try
{
for (int n = 0; n < number1.Length; ++n)
{
if (number1[n].CompareTo(number2[n]) != 0)
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex);
return false;
}
return true;
}
}
最后,Main
函数将如下所示:
static void Main()
{
string ASCIIString = @"
--- --- | | | -----
/ _| | |__| |___
\ | | | |
-- --- | | ____| ";
string[] lines =
ASCIIString.Split(new[] {"\n","\r\n"}, StringSplitOptions.RemoveEmptyEntries);
lines = lines.ReplaceSpacesWithSeparator("$");
ASCIINumbersParser parser = new ASCIINumbersParser(lines, "$");
// Try to find all numbers contained in the ASCII string
foreach (string[] candidate in parser.CandidatesList)
{
for (int i = 1; i < 10; ++i)
{
string[] num = ASCIINumberHelper.GetASCIIRepresentationForNumber(i);
if (ASCIINumberHelper.ASCIIRepresentationMatch(num, candidate))
Console.WriteLine("Number {0} was found in the string.", i);
}
}
}
// Expected output:
// Number 3 was found in the string.
// Number 2 was found in the string.
// Number 1 was found in the string.
// Number 4 was found in the string.
// Number 5 was found in the string.
Here您可以找到完整的代码。
答案 2 :(得分:0)
我创建了所有模型并将其保存在字典中。
我将创建用于开发的目录 C:\ temp \ Validate C:\ temp \ Referenz C:\ temp \ Extract
在Dir参考中,将为字典中的每个模型创建一个文件。
我阅读了所有行,并将它们保存在字符串[]中。 编码我将亲自从字符串[]到char [] []进行编码。完成此操作后,我们将在Dir中验证MyEncoding.txt文件,其中包含新号码列表。 从上一个列表中,所有“ Char.Empty”(\ t)都转换为char null'\ 0'。
只要我从左到右逐列浏览列表,
我从列表中提取字符,具体取决于字典中Model的宽度(例如NumberOne的宽度为2,NumberFour的宽度为5)。 每个摘录将保存在Dir Extract中的新文件中 比较列表中的每个字符(如果相同)。如果它们有效,则提取的模型将保存在Dir Validated中的文件中,并带有识别的模型名称的名称, 并使用记事本打开该文件几秒钟,此后记事本将关闭,搜索过程将重复进行,直到列表末尾。 可以here找到这种方法的解决方案!