8

私は最初のゲーム (じゃんけん) の作成に取り組んでおり、userChoiceはさみcomputerChoicerockの場合、プログラムが勝者を rock として返すことができないという問題に遭遇しました。プログラムを取得して、他の組み合わせの勝者を与えることができます。

私はここに私のコードを持っています:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
    return "The result is a tie!";
}
if(choice1 === "rock") {
    if(choice2 === "scissors") {
        return "rock wins";
    } else {
        return "paper wins";
    }
}
if(choice1 === "paper") {
    if(choice2 === "rock") {
        return "paper wins";
    } else {
        if(choice2 === "scissors") {
            return "scissors wins";
    }
}
if(choice1 === "scissors") {
    if(choice2 === "rock") {
        return "rock wins";
    } else {
        if(choice2 === "paper") {
            return "scissors wins";
        }
    }
}
}
};
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);
4

14 に答える 14

18

勉強すること:

var choices = ["rock", "paper", "scissors"];
var map = {};

choices.forEach(function(choice, i) {
    map[choice] = {};
    map[choice][choice] = "Was a tie"
    map[choice][choices[(i+1)%3]] = choices[(i+1)%3] + " wins"
    map[choice][choices[(i+2)%3]] = choice + " wins"
})

function compare(choice1, choice2) {
    return (map[choice1] || {})[choice2] || "Invalid choice";
}

拡張セットで機能する別の方法を次に示します。奇数の可能性があり、任意の時点から、反対の総数のうち、特定の時点から先読みして(そして最後に到達すると折り返して)、前半が特定の時点に勝つと仮定します。そして後半は負けます。

または、別の言い方をすれば、与えられたポイントより前にいる残りの対戦相手の半分が負け、次の半分が勝つということです。

したがって、choices配列内の適切な順序が重要です。

var choices = ["rock", "spock", "paper", "lizard", "scissors"];
var map = {};

choices.forEach(function(choice, i) {
    map[choice] = {};
    for (var j = 0, half = (choices.length-1)/2; j < choices.length; j++) {
        var opposition = (i+j)%choices.length
        if (!j)
            map[choice][choice] = "Was a tie"
        else if (j <= half)
            map[choice][choices[opposition]] = choices[opposition] + " wins"
        else
            map[choice][choices[opposition]] = choice + " wins"
    }
})

function compare(choice1, choice2) {
    return (map[choice1] || {})[choice2] || "Invalid choice";
}
于 2013-07-31T17:58:15.707 に答える
10

コードのインデントが不十分なために、問題を確認できなかった可能性があります。適切にインデントすると、問題は明らかです。

if (choice1 === "paper") {
    if (choice2 === "rock") {
        return "paper wins";
    } else {
        if (choice2 === "scissors") {
            return "scissors wins";
        }
    }
    if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            if (choice2 === "paper") {
                return "scissors wins";
            }
        }
    }
}

以内if (choice1 === "scissors") {ですif (choice1 === "paper") {。内のコードに到達することはありません。

于 2013-07-31T17:25:36.360 に答える
1

括弧が一致していません:

if(choice1 === "paper") {
    if(choice2 === "rock") {
        return "paper wins";
    } else {
        if(choice2 === "scissors") {
            return "scissors wins";
    }
}

私は実際ifにそのブロックの最後を削除しますが、それは必要ありません。最後のブロック ( choice1 === "scissors") は正しいですが、最後の if は必須ではありません。

その特定の方法で失敗する理由を示すために、コードの関連部分を再インデントして、コードがどのように解釈されているかを示しています。

if(choice1 === "paper") {
    if(choice2 === "rock") {
        return "paper wins";
    } else {
        if(choice2 === "scissors") {
            return "scissors wins";
        }
    }
    if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        } else {
            if(choice2 === "paper") {
                return "scissors wins";
            }
        }
    }
}
于 2013-07-31T17:24:07.110 に答える
1

これが私のやり方です:


    //player choice
    var playerChoice = prompt("What is your choice of weapon: rock, paper, or scissors?");
    
    //Computer Choice
    var computerChoice = Math.ceil(Math.random() *3);
    
    //variables as numbers
    if (computerChoice < 1) {
        computerChoice = "rock";
    } else if(1 <= computerChoice <= 2) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    
    
    //defining function
    function game(playerChoice, computerChoice){
    
    //Checking for a tie
    if (playerChoice === computerChoice) {
          return "It is a tie";
        }
    
        //Check for Rock
        if (playerChoice === "rock") {
          if (computerChoice === "scissors") {
            return "Player Wins";
          } else {
            return "Computer Wins";
          }
        }
        //Check for Paper
        if (playerChoice === "paper") {
          if (computerChoice === "scissors") {
            return "Computer Wins";
          } else {
            return "Player Wins";
          }
        }
        //Check for Scissors
        if (playerChoice === "scissors") {
          if (computerChoice === "rock") {
            return "Computer Wins";
          } else {
                    return "Player Wins";
          }
        }
    }
    
    //start the game function
    game();
    //print winner
    console.log(game(playerChoice, computerChoice))

computerChoice を 0 ~ 3 の整数のランダムにすることにしましたが、その部分は削除できます。

于 2021-05-07T03:02:05.953 に答える
0

仲間の初心者として私がたどり着いた解決策は比較的単純に思えます..

var userChoice = prompt ("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
console.log(computerChoice);

if (computerChoice <=0.33) {
    "rock";
} else if (computerChoice <=0.66) {
    "paper";
} else {
    "scissors";
}
于 2016-08-09T19:38:37.190 に答える
0
var compare = function (choice1, choice2)
{
    if (choice1 === choice2)
    {
        return "The result is a tie!";
    }
    else
    {
        if(choice1 === "rock")
        {
            if(choice2 === "paper")
            {
               return "Paper beats rock. Computer Wins.";
            }
            else
            {
                return "Rock beats scissors. You win.";

            }
        }
        else
        {
            if(choice1 === "paper")
                {
                     if(choice2 === "rock")
                        {
                             return "Paper beats rock. You Win.";
                        }
            else
                {
                return "Scissors beat paper. Computer Wins.";               }

                }
    if(choice1 === "scissors")
                {
                     if(choice2 === "rock")
                        {
                             return "Rock beats scissors. Computer Wins.";
                        }
            else
                {
                return "Scissors beat paper. You Win.";               }

                }
        }
    }



};
var r = function(user)
{
    while(user < 0 | user >3)
    {user = prompt("Please don't act oversmart. Press '1' for rock, '2' for paper, and '3' for scissors.");
    }

    if(user === "1")
    user = "rock";

    else
    {
        if(user === "2")
        {user = "paper";}
        else
        {user = "scissors";}
    };
    console.log("You chose: " + user);

    computerChoice = Math.random()
    if(computerChoice <= 0.33)
    {
        computerChoice = "rock";
    }
    else
    {
        if(computerChoice > 0.33 && computerChoice <=0.66)
        {computerChoice = "paper";}
        else
        {computerChoice = "scissors";}
    }

    console.log("The computer chose: "+computerChoice)
    console.log(compare(user, computerChoice));
    if(user===computerChoice)
    {
        userChoice = user;
        return "1";}

};


var userChoice = prompt("Press '1' for rock, '2' for paper, and '3' for scissors")
var computerChoice;

var a = r(userChoice);
if(a === "1")
{//console.log("1");
while(userChoice === computerChoice)
{
    var a = prompt("Since there was a tie, please choose again. Press 1 for rock, 2 for paper and 3 for scissors.")
    var b = r(a);
    if(b !== "1")
    {break;}
}
}
于 2014-08-16T13:55:18.330 に答える
0

これは私がこの演習で作成したコードで、魔法のように機能しました... "if" ステートメントで論理演算子を使用しましたが、(明らかに) 受け入れられました。

試してみてください:D

var userChoice = prompt("Do you choose rock, paper or scissor?");
var computerChoice = Math.random();
if (computerChoice > 0 && computerChoice < 0.33) {
  computerChoice = "Rock";
} else if (computerChoice > 0.34 && computerChoice < 0.67) {
  computerChoice = "Paper";
} else {
  computerChoice = "Scissor";
}
console.log(computerChoice);

于 2016-04-14T02:06:16.720 に答える
0
var userChoice = prompt("Do you choose rock, paper or scissors? ");


var computerChoice=Math.random();

{


if(computerChoice <= ".33") 

{
    computerChoice === 'rock';


    }


    else if(computerChoice<='.66' & '>=.34')


    {

computerChoice === 'paper';


        }

        else

{

            computerChoice ===' scissors';


            }


            }


console.log( computerChoice);
于 2014-05-20T05:57:33.547 に答える
0

{} をすべて使用しない例と、else if

可能な場合は常にelse ifを使用してください.. ifステートメントは異なる場合があり、1つだけが適用されるため、else ifを使用する必要があります..

{} を必要としない条件の後にステートメントが 1 つしかない場合は if ステートメントを使用します (以下の条件 1)。以下)..しかし、それが役立つ場合は、if..else if...blockステートメントの周りでそれらを使用して、それをよりよく理解するのに役立ちます..(以下の条件3).. ..

また、それが何をするかを本当に理解していない限り、===を使用しないでください..新人であることに問題を引き起こす可能性があります..デフォルトで==を使用してください..

if(choice1 == choice2)  //condition 1
    return "The result is a tie!";
else if(choice1 == "rock") //condition 2
    if(choice2 == "scissors") 
        return "rock wins";
     else 
        return "paper wins";
else if(choice1 == "paper"){ //condition 3
    if(choice2 == "rock") 
        return "paper wins";
     else 
        return "scissors wins";
}
else if(choice1 == "scissors")
    if(choice2 == "rock")
       return "rock wins";
    else 
       return "scissors wins";
于 2013-07-31T19:39:16.873 に答える
0

私はこれを機能させました:

function playFunction() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
      alert("The result is a tie!");
}
if(choice1 === "rock") {
    if(choice2 === "scissors") {
        alert("rock wins");
    } else {
        alert("paper wins");
    }
}
if(choice1 === "paper") {
    if(choice2 === "rock") {
        alert("paper wins");
    } else {
        if(choice2 === "scissors") {
            alert("scissors wins");
    }
}
if(choice1 === "scissors") {
    if(choice2 === "rock") {
        alert("rock wins");
    } else {
        if(choice2 === "paper") {
           alert("scissors wins");
        }
    }
}
}
};
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice)
} 

私が変更したのは、メッセージを返す代わりに、答えを含むアラートをポップアップ表示することだけでした。また、HTML ボタンのクリックで呼び出すことができる 1 つの関数に入れました。

于 2013-07-31T17:58:33.740 に答える
0

これは、誰かが勝つまで完璧な自己反復ゲームを作成します。また、プレイしたゲームの数も表示されます。すべてループを使用せずに!

count = 1;

var Decisions = function() {
    if (count === 1) {
        userChoice = prompt("Do you choose rock, paper or scissors?");
    } else {
        userChoice = prompt("It's a tie. Please make your choice again!");
    }
    computerChoice = Math.random();

    if (computerChoice < 0.4) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.8) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    console.log("User: " + userChoice);
    console.log("Computer: " + computerChoice);
}

Decisions();

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {

        count = count + 1
        console.log("The result is a tie!");
        Decisions();
        return compare(userChoice, computerChoice);

    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors win";
        } else {
            return "rock wins";
        }
    }
}

console.log(compare(userChoice,computerChoice));
console.log("Wow, you played " + count + " times!");
于 2016-03-11T07:32:49.830 に答える