2016-02-23 91 views
0

我目前正在一個小型項目中創建一個hang子手遊戲作爲控制檯應用程序。目前,我遇到了問題,因爲我想爲要猜測的單詞創建一個數組。這個詞不是隨機的,並且始終是相同的。在這種情況下,它是「米勒」。我想爲「miller」創建一個數組,並有一個循環來顯示單詞字符數組中的每個字母。當信件被正確猜測時,我想顯示正確的字母,如果沒有顯示特殊字符「*」代替。所以,例如,這個詞是「米勒」,並且將首先顯示爲「******」。當用戶正確猜對一個字母時,一個特殊字符會成爲正確的猜字母。因此,說用戶猜測「我」...顯示的單詞將是「*我****」。假設他猜測下一個「l」......所顯示的單詞將會是「* ill **」。我會如何去做這件事?這是我的代碼到目前爲止......我已經創建了試圖爲此創建一個數組。對不起,如果這看起來微不足道,但我是編程新手,只需要一些指導和幫助。Hang子手控制檯應用程序

TLDR; 我需要此代碼的幫助。我正在嘗試創建一個包含'miller'的數組,並且我希望能夠在用戶猜測字母正確地將特殊字符「*」的顯示更改爲正確猜測的字母時調用該數組。我被告知要做一個for循環是最好的路線,但我有點迷路。幫幫我?

static void Main(string[] args) 
    {    
     char[] guessed = new char[26]; 

     char[] word = "miller".ToCharArray(); 

     char guess; 

     int score = 0, index = 0; 
     Console.WriteLine("******"); 

     for (int i = 0; i < 10; i++) 
     { 

      Console.Write("Please enter a letter to guess: "); 

      guess = char.Parse(Console.ReadLine()); 

      if (guess == l1 || guess == l2 || guess == l3 || guess == l4 || guess == l5 || guess == l6) 
      { 
       Console.WriteLine("Your guess is correct."); 
       guessed[index] = guess; 
       index++; 
      } 
      else 
      { 
       Console.WriteLine("Your guess is incorrect."); 
       score++; 
      } 



     } 

     Console.WriteLine("Your score is " + score); 

     Console.ReadLine(); 

    } 
} 

}

+0

作爲一個方面說明,我忘了說,我原來把每個字母定義爲一個變量。所以,變量「l1」將包含「m」,「l2」將包含「i」等等。它們也被作爲char變量。我忘了提及並在發佈前改變它。謝謝! – user5927290

+1

首先將問題分解成更小的部分。這個問題太大了,沒有人爲你做這個幫助就沒有幫助 –

+0

我不這麼認爲。我唯一需要的是幫助制定數組以及如何創建for循環來更改特殊字符。我不需要有人爲我寫代碼。一個簡單的僞代碼可以幫助。 – user5927290

回答

0

包括using System.Linq;在你的文件,這裏是你的for循環。

for (int 1 = 0; i < 10; i++) 
{ 
    Console.Write("Please enter a letter to guess: "); 
    char guess = char.Parse(Console.ReadLine()); 

    if(word.Contains(guess))//checks if the word is is the array word 
    { 
     //the guess was correct. 
     Console.WriteLine("Your guess is correct."); 
     guessed[index] = guess; 
     index++; 
    } 
    else 
    { 
     //the guess was wrong. 
     console.WriteLine("Your guess is incorrect."); 
     .... 
    } 
} 

希望這會有所幫助。

0

看起來你很接近那裏。兩件事情,我不確定是if語句「猜測== || L1猜測== L2」等等等等......這可與處理:

if(word.Contains(guess)) 

其次,你需要另一個用於循環通過正確的猜測字母並放置它們。

for循環填寫字母代替星號: 把它在爲始(INT I = 10 ...)

for (int j = 0; j < word.Length; j++) 
{ 
    if (guessed.Contains(word[j])) 
    { 
     Console.Write(word[j]); 
    } 
    else 
    { 
     Console.Write("*"); 
    } 
} 
1

我不知道這是不是你在找什麼因爲,也許你下次應該更具體一些......但我試圖爲你的問題創建一個解決方案。

static void Main(string[] args) 
    { 
     char[] guessed = new char[26]; 
     char[] testword = "******".ToCharArray(); 
     char[] word = "miller".ToCharArray(); 
     char[] copy = word; 

     char guess; 

     int score = 0, index = 0; 
     Console.WriteLine(testword); 

     for (int i = 0; i < 10; i++) 
     { 

      Console.Write("Please enter a letter to guess: "); 

      guess = char.Parse(Console.ReadLine()); 
      bool right = false; 
      for (int j = 0; j < copy.Length; j++) 
      { 
       if (copy[j] == guess) 
       { 
        Console.WriteLine("Your guess is correct."); 
        testword[j] = guess; 
        guessed[index] = guess; 
        index++; 
        right = true; 
       } 
      } 

      if (right != true) 
      { 
       Console.WriteLine("Your guess is incorrect."); 
       score++; 
      } 
      else 
      { 
       right = false; 
      } 

      Console.WriteLine(testword); 


     } 

     Console.WriteLine("Your score is " + score); 

     Console.ReadLine(); 

    }