2016-08-15 10 views
0

基本的な絞首刑執行人のゲームがあります。動作しているようですが、正しく動作するように文字の推測を重複させることはできません。現在、バンクには「ポップコーン」と「リンゴ」の2つの単語しかありません。初めてリンゴの "p"を推測すると、最初のpで塗りつぶされますが、2番目のpで塗りつぶされません。文字列の重複する文字を置き換える方法 - Javascript

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Hangman!</title> 
</head> 
<body> 
<h1>Hangman</h1> 
<h2>Press a letter to guess.</h2> 
<div id="secret"> </div> 

<script> 
var computerChoices = ['apples', 'popcorn']; 
var progress = ""; 
// This chooses a word from the set randomly. 

    var secretWord = computerChoices[Math.floor(Math.random() * computerChoices.length)]; 
    for (i=0; i < secretWord.length; i++){ 
     progress += "_"; 
    } 
// When the user presses the key it records the keypress and then sets it to userguess 
    document.onkeyup = function(event) { 
    var letter = String.fromCharCode(event.keyCode).toLowerCase(); 


    if (secretWord.indexOf(letter) > -1){ 
     console.log("Good Guess!"); 
     index = secretWord.indexOf(letter); 
     progress = progress.substr(0, index) + letter + progress.substr(index + 1); 

     // Placing the html into the secret ID 
     document.querySelector('#secret').innerHTML = progress; 

      if ((/([a-zA-Z]).*?\1/).test(secretWord)) {    
       console.log("Yep, there's a duplicate here") 
    }}else{ 
     console.log ("Eeeeeennnnnghh! Wrong! Try again dumbassss!"); 
    } 



} 
</script> 
</body> 
</html> 

答えて

1

あなた現在のコードは、最初に一致する文字を排他的に探しています。ある意味では、重複する文字を処理するループが必要です。

しかし、私は別の修正を提案します。「進捗マスク」を追跡するのではなく、今まで試したすべての文字のリストを維持する必要があります。

secretWordprogressには、すでに試されている誤った文字を検出するのに十分な情報がありません。これはおそらく最終的に実装したい機能です。一方、secretWordと試した文字のリストを使用して、必要なたびにprogressをすばやく再構築することができます。

試した文字がletters文字列に格納されているとします。

// classic for loop 
for(i = 0, progress = ''; i < secretWord.length; i++) { 
    progress += letters.indexOf(secretWord[i]) == -1 ? '_' : secretWord[i] 
} 

又はような暗黙的なループ:

// functional programming 
progress = secretWord.split('').map(function(letter) { 
    return letters.indexOf(letter) == -1 ? '_' : letter; 
}).join(''); 

または:

// regular expression: replace() with custom callback 
progress = secretWord.replace(/./g, function(letter) { 
    return letters.indexOf(letter) == -1 ? '_' : letter; 
}); 

または:

// regular expression: replace() without callback 
progress = secretWord.replace(new RegExp('[^' + letters + ']', 'g'), '_'); 
次いで progressのいずれかのような明示的なループを用いて計算することができます。

以下はexこのアプローチでゲームロジックを示す十分なコード。

var secretWord = 'apple', 
 
    letters = ''; 
 

 
function play(letter) { 
 
    if(letters.indexOf(letter) != -1) { 
 
    console.log("You've already tried '" + letter + "' ..."); 
 
    } 
 
    else { 
 
    letters += letter; 
 

 
    if(secretWord.indexOf(letter) == -1) { 
 
     console.log(letter + ' -> wrong!'); 
 
    } 
 
    else { 
 
     var progress = secretWord.replace(new RegExp('[^' + letters + ']', 'g'), '_'); 
 

 
     console.log(letter + ' -> ' + progress); 
 
     
 
     if(progress == secretWord) { 
 
     console.log('Well done!'); 
 
     } 
 
    } 
 
    } 
 
} 
 

 
play('i'); 
 
play('e'); 
 
play('p'); 
 
play('e'); 
 
play('a'); 
 
play('l');

1

あなたは二番目の引数を使用して、ループ内でのindexOfを呼び出す必要があり

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Hangman!</title> 
</head> 
<body> 
<h1>Hangman</h1> 
<h2>Press a letter to guess.</h2> 
<div id="secret"> </div> 

<script> 
var computerChoices = ['apples', 'popcorn']; 
var progress = ""; 
// This chooses a word from the set randomly. 

    var secretWord = computerChoices[Math.floor(Math.random() * computerChoices.length)]; 
    for (i=0; i < secretWord.length; i++){ 
     progress += "_"; 
    } 
// When the user presses the key it records the keypress and then sets it to userguess 
    document.onkeyup = function(event) { 
    var letter = String.fromCharCode(event.keyCode).toLowerCase(); 


    if (secretWord.indexOf(letter) > -1){ 
     console.log("Good Guess!"); 
     index = secretWord.indexOf(letter); 
     progress = progress.substr(0, index) + letter + progress.substr(index + 1); 
     secretWord = secretWord.substr(0, index) + '*' + secretWord.substr(index + 1); 
     console.log('Secret Word is: ' + secretWord); 

     // Placing the html into the secret ID 
     document.querySelector('#secret').innerHTML = progress; 

      if ((/([a-zA-Z]).*?\1/).test(secretWord)) {    
       console.log("Yep, there's a duplicate here") 
    }}else{ 
     console.log ("Eeeeeennnnnghh! Wrong! Try again dumbassss!"); 
    } 



} 
</script> 
</body> 
</html> 
1

を試してみてください。 例:

var word = "popcorn"; 
var result = word.indexOf("p"); 
console.log(result); // Shows "0" 

あなたは再びこれを実行しますが、 "結果" 第二引数に変数を与える:

var previousResult = 0; 
var word = "popcorn"; 
// Give as second argument the previous result increased by 1 
var result = word.indexOf("p", previousResult + 1); 
console.log(result); // Shows "2" 

をそして、あなたはwhileループロジックを実行できます。

var secretWord = "popcorn"; 
var letter = "p"; 
var result = -2; // Initialize to (-1 - 1) 
while ((result = secretWord.indexOf(letter, result + 1)) > -1) { 
    console.log(result); // Shows "0" then "2" 
    /* Do stuff */ 
} 
関連する問題