2012-11-29 43 views
-1

我創建一個遊戲,產生一個隨機單詞,然後讓用戶猜測這個單詞。 用戶輸入他的單詞後,遊戲會對他們進行比較,並返回哪些字母是正確的,以及他們是否有正確的位置。現在,當我輸入相同的單詞時,它會返回不同的結果。檢查信件的位置,如果它的正確的字母

這是我到目前爲止有:

class Game 
{ 
    public string CorrectPlace; 
    public string WrongPlace; 
    public string NoneExist; 
    public string CorrectWord; 
    public string InputWord; // the word the uis 
    public int points; 

    public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" }; 

    public string getRandomWord(string names) 
    { 
     Random ran = new Random(); 
     return Wordarray[(ran.Next(0,Wordarray.Length-1))]; 
    } 

    public void CheckWords(string name) 
    { 
     InputWord.ToCharArray(); 

     CorrectWord = getRandomWord(CorrectWord); // store the random word in a string 

     for (int i = 0; i < CorrectWord.Length; i++) 
     { 
      if (InputWord[i] == CorrectWord[i]) 
       MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!"); 
      else 
       break; 
     } 
    } 
} 

林調用該方法在我的形式

private void button1_Click(object sender, EventArgs e) 
    { 

     Game nc = new Game(); 

     nc.InputWord = textBox1.Text; 
     nc.CheckWords(nc.InputWord); 



     } 
+0

你的線'InputWord.ToCharArray();'沒有做任何結果被丟棄。在當前的代碼中,它沒有被使用,也沒有被要求。 – Habib

+0

你在哪裏使用你的輸入?我沒有看到輸入字在任何地方被初始化。而且你正在比較隨機值,你的邏輯並不清楚你發佈的代碼。 – ryadavilli

+0

所以你說,如果你比較同一個字它報告錯了? –

回答

0

嘗試這樣的:

class Game 
{ 
    public string CorrectPlace; 
    public string WrongPlace; 
    public string NoneExist; 
    public string CorrectWord; 
    public string InputWord; 
    public int points; 

    public string[] Wordarray = null; 

    public string getRandomWord(string names) 
    { 
     Random ran = new Random(); 
     return Wordarray[(ran.Next(0,Wordarray.Length-1))]; 
    } 

    public void Game() 
    { 
     Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" } 
     CorrectWord = getRandomWord(); // store the random word in a string 
    } 

    public void CheckWords(string name) 
    { 
     for (int i = 0; i < CorrectWord.Length; i++) 
     { 
      if (InputWord[i] == CorrectWord[i]) 
       MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!"); 
      else 
       break; 
     } 
    } 
} 

現場背後的想法是在遊戲開始時獲得一個隨機單詞(實例化類Game),然後將其與CheckWords方法中的用戶輸入進行比較。

我建議不要在循環中調用MessageBox.show,它會在每次匹配存在時彈出。 發生

+0

謝謝,但它不起作用。 – user1808211

+0

你得到了什麼? – NeverHopeless

+0

它給了我一個NULL異常錯誤... for(int i = 0;我 – user1808211

0

更改這樣的代碼。如果您有任何問題,請告訴我。

類遊戲 {

public string CorrectWord = null; 
    public string InputWord; // the word the uis 
    public int points; 

    public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" }; 

    public string getRandomWord() 
    { 
     Random ran = new Random(); 
     return Wordarray[(ran.Next(0, Wordarray.Length - 1))]; 
    } 

    public void Newgame() 
    { 
    } 

    public void CheckWords(string name) 
    { 

     char[] charInputWord = name.ToCharArray(); 


     CorrectWord = getRandomWord(); // store the random word in a string 

     Console.WriteLine("Random Word " + CorrectWord); 
     Console.WriteLine("User Word " + name); 

     char[] charCorrectWord = CorrectWord.ToCharArray(); 

     for (int i = 0; i < charInputWord.Length; i++) 
     { 
      if (charInputWord[i] == charCorrectWord[i]) 
       Console.WriteLine("Letter #" + (i + 1).ToString() + " was correct!"); 
      else 
       break; 
     } 
    } 
} 



class Program 
    { 
     static void Main(string[] args) 
     { 
      Game ab = new Game(); 
      ab.CheckWords("raser"); 

      Console.ReadLine(); 
     } 
    } 
+0

不是這個代碼有我的相同的問題。參考CorrectWord = getRandomWord(); – user1808211

+0

您沒有對該參數使用任何操作。爲什麼你只需要將字符串傳遞給該函數。Actaull需要爲項目添加此代碼char [] charInputWord = name.ToCharArray();和char [] charCorrectWord = CorrectWord.ToCharArray();並將循環條件更改爲(int i = 0; i

0

兩個namesname從未使用過,而且似乎不需要的。

它看起來像你想是這樣的(轉換爲控制檯打印,因爲我沒有你的UI源)

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

namespace WordGame 
{ 
    class Game 
    { 
     static void Main(string[] args) 
     { 
      Game g = new Game(); 
      bool gameOver = false; 

      while (!gameOver) 
      { 
       Console.WriteLine("Enter a guess (or type 'exit' to exit):"); 
       string answer = Console.ReadLine(); 
       if (answer.ToLower() == "exit") 
        break; 

       if (g.CheckWord(answer)) 
       { 
        Console.WriteLine("You win!"); 
        while (true) 
        { 
         Console.WriteLine("Play Again (y/n)?"); 
         answer = Console.ReadLine().ToLower(); 
         if (answer == "n") 
         { 
          gameOver = true; 
          break; 
         } 
         else if (answer == "y") 
         { 
          g.ChooseRandomWord(); 
          break; 
         } 
        } 
       } 
      } 
     } 

     public string CorrectWord; 

     public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" }; 
     private Random ran = new Random(); 

     public Game() 
     { 
      ChooseRandomWord(); 
     } 

     public void ChooseRandomWord() 
     { 
      CorrectWord = Wordarray[(ran.Next(0, Wordarray.Length - 1))]; 
     } 

     public bool CheckWord(string guess) 
     { 
      if (guess.Trim() == CorrectWord) 
      { 
       return true; 
      } 

      string guessLower = guess.Trim().ToLower(); 
      string correctLower = CorrectWord.ToLower(); 

      for (int i = 0; i < correctLower.Length; i++) 
      { 
       if (guessLower[i] == correctLower[i]) 
        Console.Write(guess[i]); 
       else 
        Console.Write("#"); 
      } 
      Console.WriteLine(); 

      return false; 
     } 

    } 
} 
相關問題