2012-12-19 67 views
1

我必須使用多線程來製作一個簡單的基於文本的遊戲。我選擇了猜我的動物遊戲。服務器會隨機挑選一個動物併發出線索,客戶必須猜測動物是在三條線索內。客戶端/服務器通信 - 猜我的動物遊戲

但是,如果動物被猜測​​正確,程序就會轉到下一個線索。我不明白我哪裏出了問題?

另一個問題是,當客戶說到一個新的遊戲,它只是重複相同的動物。它不會改變。

我知道它只是我需要修復的協議類。請幫忙!我對這個節目感到沮喪而哭泣。

這裏是我的協議類的副本:

public class KKProtocol { 

    private static final int WAITING = 0; 
    private static final int ASKNAME = 1; 
    private static final int SENTCLUE = 2; 
    private static final int SENTCLUE2 = 3; 
    private static final int SENTCLUE3 = 4; 
    private static final int ANOTHER = 5; 
    private static final int NUMANIMALS = 4; 
    private int state = WAITING; 
    private int currentAnimal = (int) (Math.random() * 6); // number of first joke 
    private String[] clues = {"I like to play", "I like to scratch", "I eat salad", "I annoy you in the morning"}; 
    private String[] clues2 = {"Love walks", "House pet", "garden pet", "I fly everywhere"}; 
    private String[] clues3 = {"Woof", "Meow", "I live in a hutch", "Tweet Tweet"}; 
    private String[] answers = {"Dog", 
     "Cat", 
     "Rabbit", 
     "Bird",}; 
    private String[] name = {}; 

    public String processInput(String theInput) { 
     String theOutput = null; 

     // System.out.println("Welcome to my animal guessing game"); 

     if (state == WAITING) { 
      theOutput = clues[currentAnimal]; 
      state = SENTCLUE; 
     } else if (state == SENTCLUE) { 
      if (theInput.equals(answers[currentAnimal])) { 
       theOutput = "Correct...Your Score is 1....Want to play again? (y/n)"; 
       state = ANOTHER; 
      } else { 
       theOutput = clues2[currentAnimal]; 
       state = SENTCLUE2; 
      } 
     } else if (state == SENTCLUE2) { 
      if (theInput.equals(answers[currentAnimal])) { 
       theOutput = "Correct...Your Score is 2....Want to play again? (y/n)"; 
       state = ANOTHER; 
      } else { 
       theOutput = clues3[currentAnimal]; 
       state = SENTCLUE3; 
      } 
     } else if (state == SENTCLUE3) { 
      if (theInput.equals(answers[currentAnimal])) { 
       theOutput = "Correct...Your Score is 3....Want to play again? (y/n)"; 
       state = ANOTHER; 
      } else { 
       theOutput = ("it's" + answers[currentAnimal] + " you fool! Want to play again? (y/n)"); 
       state = ANOTHER; 
      } 
     } else if (state == ANOTHER) { 
      if (theInput.equalsIgnoreCase("y")) { 
       if (currentAnimal == (NUMANIMALS - 1)) { 
        currentAnimal = 0; 
       } 
       theOutput = clues[currentAnimal]; 
       // else 
       currentAnimal++; 

       state = SENTCLUE; 
      } else { 
       theOutput = "Bye."; 
       state = WAITING; 
      } 
     } 
     return theOutput; 
    } 
} 

如果您需要看到其他類請你只問。

+2

你爲什麼標記「多線程」?協議對象是否被多個線程同時使用?如果是這樣,並且由於班上沒有同步,隨機行爲也不會令人驚訝。 – assylias

+0

我標記多線程,因爲這是該單位在我的大學學位被稱爲。所以你說我需要在我的課堂上添加一個同步,它會一切正常嗎? – StephSetch

+1

我的問題不是關於你的學位的名稱:你是否從多個線程訪問協議實例? – assylias

回答

0

調試程序並在第一個if子句之前設置一個斷點。變量是怎樣的?我想你已經在你的代碼中的某處發現了一個微不足道的錯誤,它會在觀察你的程序實際執行的過程中顯示出來。

您也可以在此處粘貼一些客戶端代碼,以便有人能夠理解正在發生的事情。從評論我明白,沒有人知道你如何使用協議類。