5

I'm working on a JavaScript blackjack game for a class of mine. So far I have a method to create stacks for the deck, the player and the dealer. Another creates the deck, one creates each card as an object. Another shuffles, one deals out cards from the deck stack and another adds those cards to the other stacks. I'm trying to get some displays going to see what is happening so far before I finish it, but for the life of me I can't get anything. I've starred at this for hours and fully expect it to be some stupid syntax error. I would really appreciate anyone who could take a look at this for me and tell me why I'm not getting my displays to show up. Feel free to copy and tinker.

<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>

2 Answers2

1

For starters, your shuffle() function is missing a closing bracket:

Change this:

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;
    }

To this:

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;
    }
}    
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • Thank you, that was one of those stupid syntax errors I was talking about. It didn't get the thing running but at least it's one step closer. – Kanosthefallen Oct 26 '12 at 04:42
0

Some syntactical & logic issues

Parentheses around your appropriate values in the line:

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

as

(s*ranks.length) + r 

!= 

s * (ranks.length + r)

EDIT: I also don't like your stack() function having so many local "members". For instance, is it really necessary for your player object to hold a local instance of createDeck() or shuffle()? These should have a higher (global) scope methinks.

I believe you are overcomplicating the matter. I can offer you additional advice if you'd like, but will see how far this goes first. :)

Mike H.
  • 1,731
  • 9
  • 31