2013-12-13 63 views
1

我正在开发基于新工具和新技术的hang子手游戏,但我还没有达到这部分课程的工作内容,但是我试图取得一个良好的开端。C#hang子手,操纵字符串

到目前为止,我有这个代码(见下文)我试图显示零数组元素中的每个字符的下划线(总共七个字符),然后揭示一个字符位置,如果猜测它正确,我只是看除了我的代码之外,还有一些洞察力,可能是一篇文章,甚至是一段代码片段,这可能会让我对两难处境有所了解。

编辑:这是我的新代码,但是,在进入亚利桑那州'a'时,它只能找到并打印第一个'a'我该如何解决这个问题以找到并打印两个控制台?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Array_Arizona_Test 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     int count = 0; 
     string secretWord = "arizona"; 

     char[] secret = new char[secretWord.Length]; 

     Console.WriteLine("Welcome to Hangman!"); 

     Console.WriteLine("\nFirst Word.....\n"); 

     for (int i = 0; i < secret.Length; i++) 
     { 
      secret[i] = '_'; 
     } 

     for (int i = 0; i < secret.Length; i++) 
     { 
      Console.Write(secret[i] + " "); 
     } 

     do 
     { 
      for (int i = 0; i < secretWord.Length; i++) 
      { 
       Console.Write("\n\nPlease enter your guess : "); 
       char input = (Console.ReadLine().ToCharArray()[0]); 

       if (secretWord[i] == input) 
       { 
        secret[i] = input; 

        for (int x = 0; x < secret.Length; x++) 
        { 
         Console.Write(secret[x] + " "); 
        } 
       } 
       else 
       { 
        for (int j = 0; j < secret.Length; j++) 
        { 
         Console.Write(secret[i] + " "); 
        } 
       } 
      } 
      count++; 

     } while (count < 5); 
    } 
} 
} 
+6

有什么问题的代码? –

+0

[这是一个答案](http://stackoverflow.com/questions/20449517/find-every-same-letters-in-a-word),可以帮助你。 – paqogomez

+0

请阅读第二段的前两行,我试图让控制台显示猜测字符的位置,如果其正确,那么迄今为止,它会打印太多的下划线给控制台。 –

回答

1

这里是你应该做的

  1. 创建一个数组,并把破折号等于字
  2. 显示(步骤1)用户
  3. 当字母的数量用户猜测正确的信,把信在阵列正确位置
  4. 转到步骤2

阅读本http://en.wikipedia.org/wiki/Hangman_(game)

这是我刚写

 static void Main(string[] args) 
    { 


     string secreteWord = "Arizona"; 

     //have a dash array equal to the number of the letters of the secret word 
     char[] a = new char[secreteWord.Length]; 

     for (int i = 0; i < a.Length; i++) 
     { 
      a[i] = '_'; 
     } 

     // Tell the user the number of letters through the dashes 
     for (int i = 0; i <a.Length ; i++) 
     { 
      Console.Write(a[i]+ " "); 
     } 

     // ask the user to guess 
     Console.WriteLine(); 

     int count = 0; 
     do 
     { 
      Console.WriteLine("Enter your guess letter"); 
      char input = Console.ReadLine().ToCharArray()[0]; 

      for (int i = 0; i < secreteWord.Length; i++) 
      { 
       //if the user guessed right, replace the correct dash and display to the user 
       if (secreteWord[i] == input) 
       { 
        count++; //update the count to check when to exit 
        a[i] = input; //here if the user guess correct, we replace the dash with the input 

        //now we display the dash array after it is modified 
        for (int j = 0; j < a.Length; j++) 
        { 
         Console.Write(a[j] + " "); 
        } 
       } 
      } 
      Console.WriteLine(); 
     } 

     while (count < a.Length); 
     Console.WriteLine("You guessed it right"); 
     Console.ReadLine(); 
    } 
+0

有没有可能简化这个问题?我只是在程序编程的第四个月,我们目前没有使用var。如果你还可以包含从下划线到猜测字符更新的解释,我将不胜感激。我并不是要求出来,我更愿意完全理解它。 –

+0

我已添加一些评论,如果你没有得到它,具体,我会回答它 –

+0

@RayneBlackham你需要有你的导师认真的话!使用'var'实际上可以更快地编写代码;您只需用'var'替换类型,例如'string name =“John”'变成'var Name =“John”'。同样,'int i = 0'是'var i = 0','StringBuilder SqlQuery =“SELECT * ...”'变成'var SqlQuery =“SELECT * ...”'。这是C#的一个相对较新的特性,但它是一个简单而好的特性! –

1

你的循环更改为类似如下:

char[] guess = underscores.ToCharArray(); 
for (int x = 0; x < characters.Length; x++) 
{ 
    if (userInput == characters[x]) 
    { 
     guess[x] = userInput; 
    } 
} 
Console.Write(guess); 

你也想加入另一个循环,以允许用户在同一个词猜多次。

+0

如果我理解正确,我必须将字符串下划线更改为数组?然后使用索引来确定正确猜测的字符的位置并使用上面的代码来更新它? –

+0

是的,你是对的 - 我用一个新变量更新了我的答案中的代码 – geedubb