2011-12-31 13 views
1

私はJavaを学習しており、その過程でWord Guessゲームを作成しています。 今問題は次のとおりです。手紙の位置、単語推測ゲームのスマートな方法を理解する

推測する単語を「lingo」とします。そして、あなたが示唆した言葉は「オードル」とします。 正しい文字:なし 間違った位置文字:最初の位置に「o」、4番目の位置に「l」 2番目の「o」は文字として記載してはならないことに注意してください間違った立場に陥っています。私たちは既にその前に「o」を報告しています。したがって、「ooooo」と入力すると、最後の文字のみが正しい位置に表示され、「ooooz」と入力すると最初の文字のみが強調表示され、他の文字は強調表示されません。

いくつかのソリューションを試しましたが、すべてが失敗するようです。私はそれを行うスマートな/それほど複雑ではない方法があることを知っています。それで誰も私を助けてくれる?

コード:

///Indicates whether a letter has been accounted for 
///while highlighting letters in the guess that are not 
///in the correct position 
private Boolean[][] marked = new Boolean[WordLength][5]; 

///Holds which all letters have been solved so far 
private Boolean[][] solved = new Boolean[WordLength][6]; 


public void CheckLetters() { 

    for (int j = 0; j < currentAttempt; j++) { 
     tempWord = list.get(j); //The guessed words 

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

      if (tempWord.charAt(i) == CurrentPuzzleWord.charAt(i)) { 
       solved[i][j] = true; //CurrentPuzzleWord is the string with the hidden word 

      } else if (CurrentPuzzleWord.indexOf(tempWord.charAt(i)) != -1) { 
       marked[i][j] = true; 
      } 
     } 
    } 
+0

http://www.codeproject.com/KB/game/Lingo__a_simple_word_game.aspx – earldouglas

+0

+1は、誰かがポイントを差し引いた理由を言うことはできません。よろしくお願いいたします。 –

答えて

2

だから、複数のチェックをしたいしようとしています。

String oracle = "lingo"; 
String input = "oodle"; 
String modOracle = ""; 
// ArrayList for noting the matched elements 
ArrayList<Integer> match = new ArrayList<Integer>(); 
// ArrayList for the correct letters with wrong position 
ArrayList<Integer> close = new ArrayList<Integer>(); 
// Length of the Strings of interest 
int length = oracle.length; 

最初にチェックしたいのは、明らかに一致する正しい位置にある文字です。だから、オラクル文字列とユーザー入力文字列をとり、正しいものに注目して、文字ごとに比較してください。

// may need to check that oracle and input are same length if this isn't enforced. 
for (int i = 0; i < length; i++) { 
    if (input.substring(i,i+1).equals(oracle.substring(i,i+1))) { 
     // there is a match of letter and position 
     match.add(i); 
    } 
    else 
     modOracle = modOracle + oracle.substring(i,i+1); 
} 

正しいが間違った位置にある文字についてもう一度チェックする必要があります。これを行うには、まず、実行中の前回のチェックから正しい文字を取り出します。次に、入力文字列内の、文字列中の文字と一致する各文字について、その文字列をメモし、それを残りの小切手から削除します。入力文字列全体が見えるまでこれを続けます。

for (int i = 0; i < length; i++) { 
    if (match.contains(i)) 
     continue; 

    // String to match 
    String toMatch = input.substring(i,i+1); 

    for (int j = 0; j < modOracle.length; j++) { 
     if (toMatch.equals(modOracle.substring(j,j+1))) { 
      close.add(i); 
      // then remove this letter from modOracle 
      // need small utility method for this. 
      break; 
     } 
    } 
} 

2つのチェックの結果を組み合わせてユーザーに出力します。

結果をユーザーに表示する方法がわかりませんが、arracylist matchがoracle/inputの位置に完全に対応しており、arraylist closeがoracleの位置に対応しています/その文字がどこかに現れるが、その位置には表示されないように入力する。

+0

ありがとうございました!私はこれを試してみよう! – Jape

関連する問題