我需要你的帮助才能让摇滚,纸张和剪刀游戏适合学校作业。我需要你的建议来帮助我让这个JS游戏应用程序正常工作。
来自其他人的更多输入使这个应用程序正常工作,越好!!
这项任务需要三件大事: * getchoices函数可以获取 来自用户的输入 *下面的条件声明 变量computer_choice和 player_choice测试随机 计算机生成的值和 做比较,然后打印出来 正确的价值 *游戏正常运作
提前感谢所有帮助!
肖恩
var $ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
$("show").onclick = getChoices;
}
compare(player_choice,computer_choice);
function getChoices() {
computer_choice = Math.random();
player_choice = $("player_choice").onclick.value
}
if(computer_choice <= 0.33 ){
computer_choice = "Rock";
} else if(computer_choice <= 0.66){
computer_choice= "Paper";
} else {
computer_choice = "Scissors";
}
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";
}
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";
}
}
};
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Shipping Order Form</title>
<link rel="stylesheet" href="assets/bootstrap.min.css">
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="assets/game.js"></script>
</head>
<body>
<p> </p>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label for="player_choice">Select either rock, paper or scissors:</label>
<select name="player_choice" id="player_choice" class="form-control">
<option value=""></option>
<option value="rock">rock</option>
<option value="paper">paper</option>
<option value="scissors">scissors</option>
</select>
</div>
<div class="form-group">
<input type="button" class="btn btn-default" value="Show" name="show" id="show" />
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
您的格式化使代码难以阅读,但我可以发现一些事情:
compare(player_choice,computer_choice);
似乎这在.js文件中全局调用,并且在加载.js文件时将被调用一次,不应该在用户做出选择后调用,例如在getChoices()的末尾?:
function getChoices() {
computer_choice = Math.random();
player_choice = $("player_choice").onclick.value
compare(player_choice,computer_choice);
}
另一个明显的错误是compare()函数的第二部分。格式化使这一点很难发现:
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";
}
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";
}
}
};
正如你所看到的那样,你的括号似乎已经搞砸了。 &#34; if(choice1 ==&#34; Paper&#34;)&#34;如果在高于它的水平上应该是另一个:
else if(choice1 == "Rock")
{
if(choice2 == "Scissors")
{
return "Rock wins";
}
else
{
return "Paper wins";
}
}
else if (choice1 == "Paper")
{
....
}
else if (choice1 == "Scissors") // this could be an else
{
...
}
除此之外,我会把沃尔蒂斯的建议铭记于心,如果你学会格式化你的代码并问一个特定的问题并解释为什么/什么不起作用你就更有可能得到一个好的答案。
祝你的学校项目好运。
PS。 compare()函数返回一个结果字符串,您可能希望在某处打印或将某个元素的文本设置为它,因为现在您将返回该字符串但不对返回的值执行任何操作。 DS
PSS。 player_choice = $(&#34; player_choice&#34;)。onclick.value可能不是你想要做的,因为我使用了JQuery已经有一段时间但是我不认为.onclick.value甚至有效吗?你可能想要$(&#34; player_choice&#34;)。value或.selectedValue。您还应该小心使用您的选项,因为在比较功能中,您需要与&#34; Rock&#34;等等,但你在select元素中的值被称为&#34; rock&#34;等DSS。
答案 1 :(得分:1)
var choices = ['scissors', 'rock', 'paper'],
_game = {},
gameNo = 0,
combinations = {
win: [
['rock', 'scissors'],
['scissors', 'paper'],
['paper', 'rock']
]
},
wins = {
cpu: 0,
player: 0,
draw: 0
};
_game.getRandomInt = function (min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
};
_game.computerChoice = function () {
return choices[
_game.getRandomInt(0, choices.length)
];
};
_game.checkCombinations = function (a, b) {
var playerWin = [a, b],
winnerBlock = $('div.winner'),
isPlayerWin = false;
$('div.player-choice').text(a.toUpperCase());
$('div.cpu-choice').text(b.toUpperCase());
if (a === b) {
wins.draw++;
return winnerBlock.text('Game #' + gameNo + ' : Draw');
}
$.each(combinations.win, function (c, d) {
if (a == d[0] && b == d[1]) {
wins.player++;
return isPlayerWin = true;
}
});
if (!isPlayerWin) {
wins.cpu++;
}
return isPlayerWin ?
winnerBlock.text('Game #' + gameNo + ' : Player wins') :
winnerBlock.text('Game #' + gameNo + ' : CPU wins');
};
_game.Play = function (choice) {
return _game.checkCombinations(
choice, _game.computerChoice()
);
};
$('div.choice[data-choice]').click(function () {
gameNo++;
_game.Play($(this).data('choice'));
$.each(wins, function (i, o) {
$('td[data-winner="' + i + '"]').text(o)
});
});
&#13;
div.container {
width: 100% !important;
text-align: center;
}
div.choice {
width: 128px;
text-align: center;
background: yellow;
padding: 16px;
display: inline-block;
margin: 0 8px;
cursor: pointer;
}
div.choice > p {
background: yellow;
width: 100% !important;
}
div.choice:hover {
background: green;
transition-duration: 1.7s;
}
div.winner {
margin: 10px 0;
padding: 4px;
}
div.results {
text-align: center;
margin: 0 auto;
width: 100%;
}
div.results table {
margin: 0 auto;
width: 468px;
}
div.results table tr td {
width: 156px !important;
padding: 8px;
text-align: center;
}
div.choices div {
width: 70px;
text-align: center;
display: inline-block;
margin: 2px;
}
&#13;
<body>
<div class="container">
<div class='choice' data-choice='rock'>
<img src='http://icons.veryicon.com/png/System/GANT%202/Rock.png'>
Rock
<br>
</div>
<div class='choice' data-choice='scissors'>
<img src='https://image.flaticon.com/icons/png/128/124/124818.png'>
Scissors
<br>
</div>
<div class='choice' data-choice='paper'>
<img src='http://icons.veryicon.com/png/Game/Larry%20Laffer/Toilet%20Paper.png'>
Paper
<br>
</div>
<div class='winner'>
</div>
<div class="choices">
<div class="player-choice"></div>
<div><img src="http://www.whbqt.info/UserFiles/image/versus.png" width="32" height="32" alt=""></div>
<div class="cpu-choice"></div>
</div>
<div class="results">
<table>
<thead>
<th>Player</th>
<th>Draws</th>
<th>CPU</th>
</thead>
<tr>
<td data-winner="player">0</td>
<td data-winner="draw">0</td>
<td data-winner="cpu">0</td>
</tr>
</table>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;