HTML & Javascript 二十一点游戏不会显示赢家和牌局
HTML & Javascript blackjack game will not display winners and hands
我正在尝试使用 JavaScript 和 HTML 开发二十一点游戏。
我已经在 REPL 上单独使用 JavaScript 对其进行了测试,它运行良好,我现在尝试将其移至 html 页面,以便我最终可以添加一些图形.
但是当我运行下面的代码none玩家手牌,庄家手牌或赢家出现在屏幕上时,但提示确实如此。
有人能看出这有什么问题吗?
function play() {
playersQ = prompt('How many people are playing?');
playershands = [];
i = 0;
count = 0;
var player_cards = 0;
function player() {
var player_cards = Math.floor(Math.random() * 21) + 1;
while (player_cards < 21) {
Hit = prompt('Players cards are ' + player_cards + ' Would you like stick or twist?(S/T)')
if (Hit == 'T') {
player_cards = player_cards + Math.floor(Math.random() * 11) + 1;
} else if (Hit == 'S') {
break;
} else if (player_cards > 21) {
alert('Player is bust');
break;
}
}
return player_cards;
}
function house() {
var house_cards = Math.floor(Math.random() * 21) + 1;
document.getElementById('housecurrenthand').innerHTML = ('House has ' + house_cards);
while (house_cards < 18) {
house_cards = house_cards + Math.floor(Math.random() * 11);
document.getElementById('housecurrenthand').innerHTML = ('House has ' + house_cards);
if (house_cards > 21) {
alert('House is bust')
}
}
return house_cards;
}
while (i < playersQ) {
playershands.push(player());
i = i + 1;
}
var house_hand = house();
while (count < playersQ) {
count2 = count + 1
if (playershands[count] > house_hand && playershands[count] < 22) {
document.getElementById('playershand' + [i]).innerHTML = ('Player ' + count2 + ' has ' + playershands[count]);
document.getElementById('winner').innerHTML = ('Player wins');
count = count + 1;
} else if (house_hand > playershands[count] && house_hand < 22) {
document.getElementById('househand').innerHTML = ('House has' + house_hand);
document.getElementById('winner').innerHTML = ('House wins');
break;
} else if (house_hand == playershands[count]) {
document.getElementById('winner').innerHTML = ('draw');
break;
}
}
}
<head>
<h1 align="center">21 Game</h1>
</head>
<body bgcolor="#458B00">
<div align='center'>
<h3>House Cards</h3>
<h5 id='currenthousehand'></h5>
<h5 id='househand'> </h5>
<h3>Players Cards</h3>
<h5 id='playershand1'> </h5>
<h5 id='playershand2'> </h5>
<h5 id='playershand3'> </h5>
<h5 id='playershand4'> </h5>
<h5 id='playershand5'> </h5>
<h5 id='playershand6'> </h5>
<h5 id='playershand7'> </h5>
<h5 id='playershand8'> </h5>
<h5 id='playershand9'> </h5>
<h5 id='playershand10'> </h5>
<h4 id='winner'> </h4>
<button onclick="play()" float='bottom'>Play</button>
</div>
</body>
您正在尝试获取第 66 行(功能房)中 ID 为 housecurrenthand 的 DOM 元素的值,但在您的 html 文件中,您已使用 ID currenthousehand(第 12 行)声明了 header。
如果您不使用 Google Chrome,我鼓励您尝试一下。当您按下 F12 时,有开发者工具可以帮助您找出问题所在。
查看 Google Chrome 的官方文档,其中有很多关于如何调试 Web 代码的信息。如果您使用的是任何其他浏览器,只需查找文档或教程如何处理当前问题。
我正在尝试使用 JavaScript 和 HTML 开发二十一点游戏。
我已经在 REPL 上单独使用 JavaScript 对其进行了测试,它运行良好,我现在尝试将其移至 html 页面,以便我最终可以添加一些图形.
但是当我运行下面的代码none玩家手牌,庄家手牌或赢家出现在屏幕上时,但提示确实如此。
有人能看出这有什么问题吗?
function play() {
playersQ = prompt('How many people are playing?');
playershands = [];
i = 0;
count = 0;
var player_cards = 0;
function player() {
var player_cards = Math.floor(Math.random() * 21) + 1;
while (player_cards < 21) {
Hit = prompt('Players cards are ' + player_cards + ' Would you like stick or twist?(S/T)')
if (Hit == 'T') {
player_cards = player_cards + Math.floor(Math.random() * 11) + 1;
} else if (Hit == 'S') {
break;
} else if (player_cards > 21) {
alert('Player is bust');
break;
}
}
return player_cards;
}
function house() {
var house_cards = Math.floor(Math.random() * 21) + 1;
document.getElementById('housecurrenthand').innerHTML = ('House has ' + house_cards);
while (house_cards < 18) {
house_cards = house_cards + Math.floor(Math.random() * 11);
document.getElementById('housecurrenthand').innerHTML = ('House has ' + house_cards);
if (house_cards > 21) {
alert('House is bust')
}
}
return house_cards;
}
while (i < playersQ) {
playershands.push(player());
i = i + 1;
}
var house_hand = house();
while (count < playersQ) {
count2 = count + 1
if (playershands[count] > house_hand && playershands[count] < 22) {
document.getElementById('playershand' + [i]).innerHTML = ('Player ' + count2 + ' has ' + playershands[count]);
document.getElementById('winner').innerHTML = ('Player wins');
count = count + 1;
} else if (house_hand > playershands[count] && house_hand < 22) {
document.getElementById('househand').innerHTML = ('House has' + house_hand);
document.getElementById('winner').innerHTML = ('House wins');
break;
} else if (house_hand == playershands[count]) {
document.getElementById('winner').innerHTML = ('draw');
break;
}
}
}
<head>
<h1 align="center">21 Game</h1>
</head>
<body bgcolor="#458B00">
<div align='center'>
<h3>House Cards</h3>
<h5 id='currenthousehand'></h5>
<h5 id='househand'> </h5>
<h3>Players Cards</h3>
<h5 id='playershand1'> </h5>
<h5 id='playershand2'> </h5>
<h5 id='playershand3'> </h5>
<h5 id='playershand4'> </h5>
<h5 id='playershand5'> </h5>
<h5 id='playershand6'> </h5>
<h5 id='playershand7'> </h5>
<h5 id='playershand8'> </h5>
<h5 id='playershand9'> </h5>
<h5 id='playershand10'> </h5>
<h4 id='winner'> </h4>
<button onclick="play()" float='bottom'>Play</button>
</div>
</body>
您正在尝试获取第 66 行(功能房)中 ID 为 housecurrenthand 的 DOM 元素的值,但在您的 html 文件中,您已使用 ID currenthousehand(第 12 行)声明了 header。
如果您不使用 Google Chrome,我鼓励您尝试一下。当您按下 F12 时,有开发者工具可以帮助您找出问题所在。
查看 Google Chrome 的官方文档,其中有很多关于如何调试 Web 代码的信息。如果您使用的是任何其他浏览器,只需查找文档或教程如何处理当前问题。