5

我正在为我的一类人开发一个 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>
4

2 回答 2

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;
    }
}    
于 2012-10-26T04:36:19.690 回答
0

一些语法和逻辑问题

行中适当值的括号:

this.cards[s * ranks.length + r] = new card(ranks[r], suits[s]);

作为

(s*ranks.length) + r 

!= 

s * (ranks.length + r)

编辑: 我也不喜欢你的 stack() 函数有这么多本地“成员”。例如,您的播放器对象是否真的需要保存 createDeck() 或 shuffle() 的本地实例?这些应该具有更高(全局)范围的methinks。

我相信你把事情复杂化了。如果你愿意,我可以为你提供额外的建议,但会先看看这能走多远。:)

于 2012-10-26T05:39:46.470 回答