c# - Dealing with duplicate characters
问题描述
I'm making a wild west duelling game based on typing of the dead. You have a word to write in a certain amount of time. You win if you type out the word in time, you lose if you type it incorrectly/press the wrong button or if the time runs out.
Currently I've got everything working fine. A slight issue, however, is with how I'm dealing with displaying the letters you have to type on the screen.
Each character is stored into an array that is looped through and displayed on the screen. When the player presses the correct button, the corresponding display should turn red which it does most of the time. The times where it doesn't is when there are duplicate characters.
For example if I was typing the word 'dentist', when I type the first t, it won't turn red. However, when I get to the second t and press it, both turn red. I assume this is because I'm looping through each displayed character and checking to see if it's relevant input is being pressed and because there's two and I can only type one character at a time one is always false which 'overrides' the one that is true. I'm not sure how to implement a solution with how I'm currently dealing input so any help is appreciated!
Code:
if (Duelling)
{
if (currentWord.Count > 0 && Input.inputString == currentWord[0].ToLower())
{
print(Input.inputString);
string pressedKey = currentWord[0];
currentWord.Remove(currentWord[0]);
}
else if (Input.inputString != "" && Input.inputString != currentWord[0].ToLower())
{
DuelLost();
}
if (currentWord.Count <= 0)
{
DuelWon();
}
foreach(Transform Keypad in keyDisplay.transform)
{
//print(Keypad.Find("KeyText").GetComponent<Text>().text);
Keypad.Find("KeyText").GetComponent<Text>().color = currentWord.Contains(Keypad.Find("KeyText").GetComponent<Text>().text) ? Color.black : Color.red;
}
}
解决方案
我相信问题在于您的颜色更新逻辑。Contains
如果您的数组包含您要查找的文本,则自然返回 true。由于“dentist”中的第二个 T 在您键入第一个 T 后仍然存在于数组中,因此该组件不会改变其颜色。当输入第二个 T 时,Ts 的所有实例都会从列表中清除,并且由于您一直循环遍历所有 Text 组件,因此它们都将变为红色。
没有冒犯,但你正在做这个相当......粗暴。请允许我提出一个更优雅的方法:
public String currentWord;
private List<Text> letterViews = new List<Text>();
private int curIndex = 0;
void Start() {
// Populate the list of views ONCE, don't look for them every single time
letterViews = ... // How you do this is entirely up to you
}
void Update() {
// ...
if (Duelling) {
// If we've gone through the whole word, we're good
if (curIndex >= currentWord.Length) DuelWon();
// Now check input:
// Note that inputString, which I've never used before, is NOT a single character, but
// you're using only its first character; I'll do the same, as your solution seems to work.
if (Input.inputString[0] == currentWord[currentIndex]) {
// If the correct character was typed, make the label red and increment index
letterViews[currentIndex].color = Color.red;
currentIndex++;
}
else DuelLost();
}
}
我敢说这是一个更简单的解决方案。DuelWon
并将DuelLost
索引重置为0,全部清除文本letterViews
并将它们变回黑色,也许。
如何填充视图列表:您可以将其公开并通过检查器手动链接它们(无聊),或者您可以使用Transform.GetChild(index)
. 你可能有足够的文本视图来容纳你最长的单词;我建议把它们都填满。您只需执行一次,这样做不会损失任何性能,并且您可以将它们重新用于字典中的任何单词。
推荐阅读
- css - 如何将 Ultimate Member 与 buddyboss 和 anspress 集成
- flutter - 无法从另一个包调用 Flutter Singleton
- javascript - 即使在使用 JavaScript 更改事件侦听器后,附加 HTML 元素的函数也不起作用
- r - 使用 ifelse 更改输出它不应该改变
- flutter - Flutter Windows:如果将 TextField 放置在 AlertDialog 内,则它不允许空间
- django - 如何组合我的 Django 模型,以免重复自己?
- sql-server - 如何将 sql 查询的结果保存在 vba 变量中?
- .net - 为什么 Serilog PostgreSQL 接收器无法通过配置工作?
- javascript - 对象之间的 Javascript 深度差异,仅显示更改
- mysql - 如何比较 MySQL 或 SQL SERVER 中的两组?