你将如何打印结果"计算机获胜"或"用户获胜"

时间:2014-10-24 18:10:42

标签: javascript

显然,这将打印出摇滚/纸/剪刀的胜利,或者它是一个平局。

获得结果的最佳方法是什么?#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";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } 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 wins";
        } else {
            return "rock wins";
        }
    }
}

compare(userChoice,computerChoice);

我对函数和变量有一些了解,我认为这足以解决这个问题 - 我无法弄明白。我的javascript体验不到8小时。

另外,这不是我的功课,我刚完成了一个关于codecademy的课程,并想知道这一点。

2 个答案:

答案 0 :(得分:0)

我会考虑更改程序中的字符串,而不仅仅是“剪刀获胜”,我会将其更改为“Scissors beats(在此处插入相应的选项) - 玩家获胜!!!”

您可以通过试错调试或遵循嵌套if和else的逻辑来找到相应的选择。

希望这会有所帮助;)

答案 1 :(得分:0)

这是一种通过附加到DOM来实现的简单方法。

此示例使用do {} while ()循环来询问玩家是否要玩,如果是,则要求他选择。然后它会将结果(tie或player / cpu wins)显示在页面上的output元素中。这种方法很好,因为它可以跟踪你的游戏,所以如果你需要做一个&#34;最好的3&5&#34;你可以看到谁最终赢了!

jQuery jsFiddle示例: http://jsfiddle.net/edahqpho/3/

纯javascript jsFiddle示例:http://jsfiddle.net/edahqpho/1/

HTML

<div id='output'></div>

Javascript(纯JS示例)

function play() {
    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";
    }
    console.log("Computer: " + computerChoice);
    return compare(userChoice, computerChoice);
}
var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    } 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 wins";
        } else {
            return "rock wins";
        }
    }
}

var output = document.getElementById('output');
do {
    var result = play();
    var display = document.createElement("DIV");
    var text = document.createTextNode(result);
    display.appendChild(text);
    output.appendChild(display);
}
while (window.confirm("Play again?"));

下一个片段演示了使用类似DOM注入技术的游戏的更高级可视化(使用像jQuery这样的库更容易)。

&#13;
&#13;
function play() {
do {
    var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
} while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var playResult = {
    User: userChoice,
    CPU: computerChoice,
    Winner: compare(userChoice, computerChoice)
};
return playResult;
}
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
    return "tie";
} else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "player";
    } else {
        return "cpu";
    }
} else if (choice1 === "paper") {
    if (choice2 === "rock") {
        return "player";
    } else {
        return "cpu";
    }
} else if (choice1 === "scissors") {
    if (choice2 === "paper") {
        return "player";
    } else {
        return "cpu";
    }
}
}

function getIcon(shape) {
    var url = "";
    switch (shape) {
        case "paper":
            url = "http://megaicons.net/static/img/icons_sizes/8/178/256/rock-paper-scissors-paper-icon.png";
            break;
        case "rock":
            url = "http://megaicons.net/static/img/icons_sizes/8/178/256/rock-paper-scissors-rock-icon.png";
            break;
        case "scissors":
            url = "http://megaicons.net/static/img/icons_sizes/8/178/256/rock-paper-scissors-scissors-icon.png";
            break;
    }
    return url;
}

function game() {
    var $output = $("#output");
    var result = play();
    $output.append("<tr><td><img src='" + getIcon(result.User) + "'/></td><td><img src='" + getIcon(result.CPU) + "'/></td><td>" + result.Winner.toUpperCase() + "</td></tr>");
    setTimeout(function () {
        if (window.confirm("Play again?")) {
            game()
        }
    }, 10);
}
// start the game of all games...
game();
&#13;
#output th, #output td {
margin: 10px;
padding: 10px;
font-size: 14px;
font-family:"Segoe UI", Arial, "Sans serif";
}
img {
width: 20px;
}
&#13;
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='output'>
<tr>
    <th>Player</th>
    <th>CPU</th>
    <th>Winner</th>
</tr>
</table>
&#13;
&#13;
&#13;