mirror of
https://github.com/jie65535/CodeMatrix.git
synced 2025-06-01 17:29:10 +08:00
修改 目标序列匹配规则,加入KMP算法思想
修改 默认运行的矩阵大小与缓冲区长度
This commit is contained in:
parent
e78de86e8d
commit
462c7551af
@ -69,8 +69,8 @@ namespace CodeMatrix
|
|||||||
|
|
||||||
private void InitData()
|
private void InitData()
|
||||||
{
|
{
|
||||||
Rows = 5;
|
Rows = 7;
|
||||||
Columns = 5;
|
Columns = 7;
|
||||||
_currDir = Directions.Horizontal;
|
_currDir = Directions.Horizontal;
|
||||||
HoverPoint = new Point(-1, -1);
|
HoverPoint = new Point(-1, -1);
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ namespace CodeMatrix
|
|||||||
|
|
||||||
private void InitData()
|
private void InitData()
|
||||||
{
|
{
|
||||||
BufferSize = 4;
|
BufferSize = 9;
|
||||||
CurrIndex = 0;
|
CurrIndex = 0;
|
||||||
HoverCode = 0;
|
HoverCode = 0;
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ namespace CodeMatrix
|
|||||||
public int OffsetIndex { get; private set; }
|
public int OffsetIndex { get; private set; }
|
||||||
public int CurrIndex { get; private set; }
|
public int CurrIndex { get; private set; }
|
||||||
public byte[] TargetCodes { get; } = new byte[32];
|
public byte[] TargetCodes { get; } = new byte[32];
|
||||||
|
private int[] _Next = new int[32];
|
||||||
|
|
||||||
public enum State
|
public enum State
|
||||||
{
|
{
|
||||||
@ -49,14 +50,32 @@ namespace CodeMatrix
|
|||||||
|
|
||||||
private void InitData()
|
private void InitData()
|
||||||
{
|
{
|
||||||
BufferSize = 4;
|
BufferSize = 9;
|
||||||
TargetLength = Common.Random.Next(2) + 2;
|
TargetLength = Common.Random.Next(3, 6);
|
||||||
OffsetIndex = 0;
|
OffsetIndex = 0;
|
||||||
CurrIndex = 0;
|
CurrIndex = 0;
|
||||||
HoverCode = 0;
|
HoverCode = 0;
|
||||||
CurrState = State.Input;
|
CurrState = State.Input;
|
||||||
for (int i = 0; i < TargetLength; i++)
|
for (int i = 0; i < TargetLength; i++)
|
||||||
TargetCodes[i] = Common.Codes[Common.Random.Next(Common.Codes.Length)];
|
TargetCodes[i] = Common.Codes[Common.Random.Next(Common.Codes.Length)];
|
||||||
|
_Next[0] = -1;
|
||||||
|
_Next[1] = 0;
|
||||||
|
for (int i = 1, j = 0; i < TargetLength;)
|
||||||
|
{
|
||||||
|
if (j == -1 || TargetCodes[i] == TargetCodes[j])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
if (TargetCodes[i] != TargetCodes[j])
|
||||||
|
_Next[i] = j;
|
||||||
|
else
|
||||||
|
_Next[i] = _Next[j];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
j = _Next[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitComponent()
|
private void InitComponent()
|
||||||
@ -177,10 +196,25 @@ namespace CodeMatrix
|
|||||||
{
|
{
|
||||||
if (CurrState == State.Input)
|
if (CurrState == State.Input)
|
||||||
{
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (CurrIndex == -1)
|
||||||
|
{
|
||||||
|
CurrIndex = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (code == TargetCodes[CurrIndex])
|
if (code == TargetCodes[CurrIndex])
|
||||||
|
{
|
||||||
CurrIndex++;
|
CurrIndex++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
OffsetIndex++;
|
{
|
||||||
|
var next = _Next[CurrIndex];
|
||||||
|
OffsetIndex += CurrIndex - next;
|
||||||
|
CurrIndex = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (OffsetIndex + TargetLength > BufferSize)
|
if (OffsetIndex + TargetLength > BufferSize)
|
||||||
CurrState = State.Reject;
|
CurrState = State.Reject;
|
||||||
|
Loading…
Reference in New Issue
Block a user