我正在制作一个匹配游戏的单词,我得到一个随机单词。例如:" duck"
我将字符串转换为字符串数组
然后我为每个角色创建3个按钮 - 除了1个将与其他角色混淆的角色。所以,孩子把它拖到正确的顺序。
游戏玩法示例:
孩子有一个单词," turtle",出现的单词是" t(_)rtle"。在那之后,他有一个他有很多角色的地方,即:" a"," t"," q"," u&# 34;(正确一个)。然后他将这个词拖到正确的位置。
好吧,我设法让这个工作,但只有1个字符,
在for循环中,我这样做:
string[] stringArray = CMath.CharArrayToStringArray(getName().ToCharArray());
string randChar = getName().ToCharArray()[(CMath.randomIntBetween(0, getName().Length - 1))].ToString();
//create chars, if the char is the random character, skip its creation and store it to create it later on the place where the child can drag the words.
for (int i = 0; i < getName().Length; i++)
{
if (stringArray[i] != randChar)
{
CCharacter aChar = new CCharacter(stringArray[i], (110) + 100 * i, 750, CCharacter.TYPE_GREEN);
aChar.setInactive(true);
mCharList.Add(aChar);
}
}
这会导致以下问题:
如果您的单词有重复字符,会发生什么?即:&#34;字符&#34 ;;你有2个C,2个A,2个R,......
如果我使用上面的代码,它将留下空格,但重点是,它不应该是:&#34; ch()r()cters&#34;。 它应该是:&#34; ch(_)racters&#34;
图片示例:
答案 0 :(得分:1)
而不是使用循环只是找到随机字符的第一个出现:
myMap.emplace("test", std::move(item));
答案 1 :(得分:0)
放置
break;
在if语句的末尾:
if (stringArray[i] != randChar)
{
CCharacter aChar = new CCharacter(stringArray[i], (110) + 100 * i, 750, CCharacter.TYPE_GREEN);
aChar.setInactive(true);
mCharList.Add(aChar);
break;
}
答案 2 :(得分:0)
您可以使用索引跳过字符
string[] stringArray = CMath.CharArrayToStringArray(getName().ToCharArray());
int randIndex = CMath.randomIntBetween(0, getName().Length - 1);
//create chars, if the char is the random character, skip its creation and store it to create it later on the place where the child can drag the words.
for (int i = 0; i < getName().Length; i++)
{
if (i != randIndex)
{
CCharacter aChar = new CCharacter(stringArray[i], (110) + 100 * i, 750, CCharacter.TYPE_GREEN);
aChar.setInactive(true);
mCharList.Add(aChar);
}
}
答案 3 :(得分:0)
以下是一些允许您执行以下操作的代码:
<强>代码:强>
using System;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var word = new Word("turtle");
// either of these methods should be run at least once otherwise .HasWon will be true
// hint: do that in constructor as you wish
//word.RemoveAllOccurrencesOf('t');
word.RemoveSomeChars();
word.TryPlaceChar('u');
word.TryPlaceChar('l');
word.TryPlaceChar('e');
}
}
internal class Word
{
private const char Separator = '_';
private readonly char[] _chars;
private readonly string _name;
public char[] Chars
{
get { return _chars; }
}
public string CharsAsString
{
get { return new string(_chars); }
}
public string Name
{
get { return _name; }
}
public bool HasWon
{
get
{
return CharsAsString == Name;
}
}
public Word(string name)
{
_name = name;
_chars = _name.ToCharArray();
}
public void RemoveAllOccurrencesOf(char c)
{
for (int i = 0; i < _chars.Length; i++)
{
var c1 = _chars[i];
if (c1 == c) _chars[i] = Separator;
}
}
public void RemoveSomeChars(int percentage = 50)
{
var length = this._name.Length;
var random = new Random();
int count = (int)(length * (percentage / 100.0d));
for (int i = 0; i < count; i++)
{
var next = random.Next(length);
this._chars[next] = Separator;
}
}
public void TryPlaceChar(char c)
{
for (int i = 0; i < _chars.Length; i++)
{
if (_chars[i] == Separator && _name[i] == c)
{
_chars[i] = c;
}
}
}
}
}
<强>演示:强>
现在改进你认为合适的设计!
答案 4 :(得分:0)
....创建一个空字符串来存储值并在第一次跳过期间填充它...在输入值之后然后阻止跳过后续循环
string[] stringArray = CMath.CharArrayToStringArray(getName().ToCharArray());
string randChar = getName().ToCharArray()[(CMath.randomIntBetween(0, getName().Length - 1))].ToString();
string removedLetter = String.Emty;
//create chars, if the char is the random character, skip its creation and store it to create it later on the place where the child can drag the words.
for (int i = 0; i < getName().Length; i++)
{
if (stringArray[i] != randChar || !String.IsNullOrEmpty(removedLetter))
{
CCharacter aChar = new CCharacter(stringArray[i], (110) + 100 * i, 750, CCharacter.TYPE_GREEN);
aChar.setInactive(true);
mCharList.Add(aChar);
}
else
{
removedLetter = stringArray[i];
}
}