簡體   English   中英

Javascript中的石頭剪刀布游戲功能

[英]Rock Paper Scissors Game Function in Javascript

我有一個簡單的 Rock Paper Scissors 游戲的代碼,但是當我運行它時,它只要求我輸入 Rock、Paper 或 Scissors 的輸入,然后就沒有其他內容了。 之后就沒有警報了。

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 alert("The result is a tie!");
    }

    if (choice1 === "Rock")
    {
        if (choice2 === "Scissors")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Paper wins!");
        }
    }
    else if (choice1 === "Scissors")
    {
        if (choice2 === "Rock")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Schissors wins!");
        }
    }
};

compare(userChoice, computerChoice);

順便說一句,我正在使用在線編輯器。

jsFiddle

不需要return

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";
}

function compare(choice1, choice2) {
    if (choice1 === choice2) {
        alert("The result is a tie!");
    }

    if (choice1 === "Rock") {
        if (choice2 === "Scissors") {
            alert("Rock wins!");
        } else if (choice2 === "Paper") {
            alert("Paper wins!");
        }
    } else if (choice1 === "Scissors") {
        if (choice2 === "Rock") {
            alert("Rock wins!");
        } else if (choice2 === "Paper") {
            alert("Schissors wins!");
        }
    }
};

compare(userChoice, computerChoice);

您的問題是您正在混合案例。 您將computerChoice<\/code>設置為"rock"<\/code> 、 "paper"<\/code>或"scissors"<\/code>但隨后在函數內部執行與"Rock"<\/code> 、 "Paper"<\/code>或"Scissors"<\/code> 。

只需使用小寫字母,並在prompt()<\/code>調用后添加userChoice = userChoice.toLowerCase()<\/code> 。

僅當choice1"Rock""Scissors"完全匹配時,才會顯示任何內容。

你漏掉了"Paper" 更重要的是,您排除了用戶拼寫錯誤的可能性。

你應該有一個“其他一切”分支。 這至少會給你一些反饋。

var compare = function (choice1, choice2)
{
    if (choice1 === choice2)
    {
        return alert("The result is a tie!");
    }

    if (choice1 === "Rock")
    {
        if (choice2 === "Scissors")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Paper wins!");
        }
    }
    else if (choice1 === "Scissors")
    {
        if (choice2 === "Rock")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Schissors wins!");
        }
    }
    alert('Unhandled combination! choice1 = ' + choice1 + ', choice2 = ' + choice2);
};

javascript中的字符串比較區分大小寫:

"rock" != "Rock"

明顯的錯誤是計算機的選擇都是小寫的,而compare功能使用石頭、紙和剪刀的標題。

我還將建議對您的代碼進行一些改進。 首先, compare函數現在正在做兩件事:計算游戲結果並將其發送給用戶。 更好的方法是單獨進行。 函數應該做一件事情:

function compare(choice1, choice2) {
    ...
        return "It's a tie";
    ...
        return "Rock wins";
    ...
}

alert(compare(...))

第二,小寫用戶輸入。 這將使其格式與計算機選擇相同,並且可能使您免於用戶問題。

第三,檢查用戶輸入是否是可用變體之一:

var userChoice = prompt("Do you ...?").toLowerCase();

var variants = ['rock', 'paper', 'scissors'];
if (variants.indexOf(userChoice) === -1) {
    alert("Don't cheat! Choose one of ...")
}

第四,這個游戲只有六個可能的選擇對,所以compare功能不應該那么長。 使其更簡單的一種方法是明確列出所有可能的選擇對:

var choicePairs = {
    'rock.rock': "It's a tie",
    'rock.paper': "Paper wins",
    'rock.scissors': "Rock wins",
    'paper.rock': "Paper wins",
    ...
}

function compare(choice1, choice2) {
    return choicePairs[choice1 + '.' + choice2];
} 

最后,您可以重用variants數組來選擇計算機選項:

var computerChoice = variants[Math.floor(Math.random() * variants.length))];

為了清楚起見,您可以將這個 select-random-element-from-array 存儲在單獨的函數中。

好好編碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM