feat: key history algorithm
This commit is contained in:
parent
5d2da3ddbf
commit
1869a92580
8 changed files with 115 additions and 10 deletions
34
Assets/Scripts/Domain/KeyHistory.cs
Normal file
34
Assets/Scripts/Domain/KeyHistory.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Domain {
|
||||
public class KeyHistory {
|
||||
private readonly List<int> _lastPresses = new();
|
||||
private readonly List<int> _desiredSequence;
|
||||
|
||||
public KeyHistory(List<int> desiredSequence) {
|
||||
_desiredSequence = desiredSequence;
|
||||
}
|
||||
|
||||
public void KeyPressed(int key) => _lastPresses.Add(key);
|
||||
public void ClearPressed() => _lastPresses.Clear();
|
||||
|
||||
public bool ContainsSequence() {
|
||||
if (_lastPresses.Count < _desiredSequence.Count)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < _lastPresses.Count; i++) {
|
||||
if (i >= _desiredSequence.Count)
|
||||
break;
|
||||
|
||||
int keyPressed = _lastPresses[_lastPresses.Count - 1 - i];
|
||||
int sequenceKey = _desiredSequence[_desiredSequence.Count - 1 - i];
|
||||
|
||||
if (keyPressed != sequenceKey) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue