2016-01-13 34 views
0

我正在做一个单词匹配游戏,我得到一个随机单词。例如:“鸭”小型文字游戏,删除重复项目

我比字符串转换为一个字符串数组,

然后,我创建为每个字符按钮3 - 除了将与其它字符被加扰1个字符。所以,孩子把它拖到正确的顺序。

游戏例如:

孩子有一个字, “龟”,出现为 “T(_)RTLE” 这个词。在那之后,他有一个他有很多角色的地方,即:“a”,“t”,“q”,“u”(正确的一个)。然后他把这个词拖到正确的地方。

好吧,我设法得到这个工作,但只有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); 
    } 
} 

这将导致以下问题:

如果你有一个重复字符的单词会发生什么?即:“字符”;你有2个C的,2个A的,2个R的......

如果我使用上面的代码,它会留下空格,但问题是,它不应该是:“ch()r() cters”。 它应该是: “CH(_)racters”

形象的例子:

enter image description here

+0

尝试建立一个检查看看有多少个字符被遗漏。如果一个字符被遗漏,请将bool设置为true。然后只要添加所有剩余的char,如果bool为true。只是一个brainfart。 –

+0

这个问题令人难以置信的不清楚。 – Fattie

回答

1

而不是使用循环只是找到第一次发生Ë随机字符:

int index = Array.IndexOf(stringArray, randChar); 
if(index > -1){ 
    stringArray[index] = "_"; 
} 
+0

解决这个特定问题的最简单的方法。 +1 – Akaino

+0

谢谢朋友!索引方法完美运作! 你是老板! – necrolords66

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; 
    } 
+0

这没有奏效,它创建1并打破循环。 – necrolords66

+0

然后,我不明白你的逻辑。您可以创建bool []数组,并只检查其中的填充位置,而不是将其保存在列表中。所以,当有东西拖到未填充的字符串上时,您会检查字符和原始字符,如果找到空白并且位置正确 - 填充并重新绘制图像。 – eocron

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); 
    } 
} 
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; 
       } 
      } 
     } 
    } 
} 

演示:

​​

enter image description here

现在对设计改进您认为合适的!

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]; 
} 
}