我正在为我的一类游戏开发一个JavaScript二十一点游戏。到目前为止,我有一种为牌组,玩家和经销商创建筹码的方法。另一个创建甲板,一个创建每个卡作为对象。另一个洗牌,一个从牌组堆叠卡片,另一个将这些卡片添加到其他堆栈。在我完成之前,我正试图让一些显示器看看到底发生了什么,但是对于我的生活,我无法得到任何东西。我已经出演了好几个小时并完全期望它是一些愚蠢的语法错误。我真的很感激任何能够为我看一眼的人,并告诉我为什么我没有让我的显示器显示出来。随意复制和修补。
<html>
<head>
<script type="text/javascript">
function stack()
{
this.cards = new Array();
this.makeDeck = createDeck();
this.shuffle = shuffle();
this.deal = dealHand();
this.addCard = AddCard();
this.combine = Combine();
this.cardCount = CardCount();
}
function createDeck()
{
var ranks = new Array("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King");
var suits = new Array("Clubs", "Diamonds", "Hearts", "Spades");
var count = ranks.length * suits.length;
count = parseInt(count);
this.cards = new Array(count);
for (s = 0; s < suits.length; s++)
{
for (r = 0; r < ranks.length; r++)
{
this.cards[s * ranks.length + r] = new card(ranks[r], suits[s]);
}
}
}
function card (inrank, insuit)
{
this.suit = insuit;
this.rank = inrank;
}
function shuffle(n)
{
var i, j, k;
var temp;
for (i = 0; i < n; i++)
for (j = 0; j < this.cards.length; j++)
{
k = Math.floor(Math.random() * this.cards.length);
temp = this.cards[j];
this.cards[j] = this.cards[k];
this.cards[k] = temp;
}
function dealHand()
{
return this.cards.shift();
}
function addCard(card)
{
this.cards.push(card);
}
function newGame()
{
var deck = new stack();
var player = new stack();
var dealer = new stack();
deck.makeDeck;
deck.shuffle;
player.addCard(deck.deal);
player.addCard(deck.deal);
dealer.addCard(deck.deal);
dealer.addCard(deck.deal);
display();
document.write("here");
}
function display()
{
document.display.player_card_1.value ="here"; //player.cards.card[0].rank + " of " + player.cards.card[0].suit;
//document.display.player_card_1_val.value = player.cards.card[0].value;
}
</script>
<style type="text/css">
input {
background:transparent
}
</style>
</head>
<body>
<script type="text/javascript">
</script>
<table bgcolor="#33CC33">
<tr>
<td style="font:14pt bold">Your cards</td>
<td></td>
<td style="font:14pt bold">Dealer's cards</td>
<td></td>
</tr>
<tr>
<td><input type="text" readonly="readonly" value="" name="player_card_1"></td>
<td><input type="text" readonly="readonly" value="" name="p_card_1_val"></td>
<td><input type="password" readonly="readonly" value="" name="dealer_card_1"></td>
<td><input type="password" readonly="readonly" value="" name="d_card_1_val"></td>
</tr>
<tr>
<td><input type="text" readonly="readonly" value="" name="player_card_2"></td>
<td><input type="text" readonly="readonly" value="" name="p_card_2_val"></td>
<td><input type="text" readonly="readonly" value="" name="dealer_card_2"></td>
<td><input type="text" readonly="readonly" value="" name="d_card_2_val"></td>
</tr>
<tr>
<td><input type="text" readonly="readonly" value="" name="player_card_3"></td>
<td><input type="text" readonly="readonly" value="" name="p_card_3_val"></td>
<td><input type="text" readonly="readonly" value="" name="dealer_card_3"></td>
<td><input type="text" readonly="readonly" value="" name="d_card_3_val"></td>
</tr>
<tr>
<td><input type="text" readonly="readonly" value="" name="player_card_4"></td>
<td><input type="text" readonly="readonly" value="" name="p_card_4_val"></td>
<td><input type="text" readonly="readonly" value="" name="dealer_card_4"></td>
<td><input type="text" readonly="readonly" value="" name="d_card_4_val"></td>
</tr>
<tr>
<td><input type="text" readonly="readonly" value="" name="player_card_5"></td>
<td><input type="text" readonly="readonly" value="" name="p_card_5_val"></td>
<td><input type="text" readonly="readonly" value="" name="dealer_card_5"></td>
<td><input type="text" readonly="readonly" value="" name="d_card_5_val"></td>
</tr>
<tr>
<td><input type="text" readonly="readonly" value="Total"></td>
<td><input type="text" readonly="readonly" value=""></td>
<td><input type="text" readonly="readonly" value="Total"></td>
<td><input type="text" readonly="readonly" value=""></td>
</tr>
</table>
<br>
<form name="buttons" action="">
<input type="button" value="Deal" name="deal" onclick="newGame()">
<input type="button" value="Hit" name="hit">
<input type="button" value="Stand" name="stand">
</form>
</body>
</html>
答案 0 :(得分:1)
对于初学者,您的shuffle()
功能缺少结束括号:
改变这个:
function shuffle(n)
{
var i, j, k;
var temp;
for (i = 0; i < n; i++)
for (j = 0; j < this.cards.length; j++)
{
k = Math.floor(Math.random() * this.cards.length);
temp = this.cards[j];
this.cards[j] = this.cards[k];
this.cards[k] = temp;
}
对此:
function shuffle(n)
{
var i, j, k;
var temp;
for (i = 0; i < n; i++)
for (j = 0; j < this.cards.length; j++)
{
k = Math.floor(Math.random() * this.cards.length);
temp = this.cards[j];
this.cards[j] = this.cards[k];
this.cards[k] = temp;
}
}
答案 1 :(得分:0)
括号中的适当值括号:
this.cards[s * ranks.length + r] = new card(ranks[r], suits[s]);
as
(s*ranks.length) + r
!=
s * (ranks.length + r)
修改强> 我也不喜欢你的stack()函数有这么多的本地“成员”。例如,你的玩家对象是否真的需要持有createDeck()或shuffle()的本地实例?这些应该具有更高(全局)的范围。
我相信你过分复杂了。如果您愿意的话,我可以为您提供额外的建议,但会看到这一点有多远。 :)