SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    [javascript] simple solitare excerpt

    As an attempt to teach myself javascript and html5's canvas, I've chosen to write a simple solitare game. With massive assistance from sitepoint's fretburner, it works. This following excerpt is, however, mostly my own creation and I am sure there are better ways of defining and maintaining my deck array -- (though I've had no luck getting through the syntax errors in my attempts to use splice, .deck.length and prototype) .

    And, of course, my "shuffle" is a bit out-of-the-box by imbedding it as part of the getNextCard function.

    (Note that the lack of ++ convention is due to using the "vanilla" jsLint validator)

    Code:
    /* coding conventions per jsLint (http://www.jslint.com/) */
    // ---- Globals ----
    
    var cardsLeft = 52;              // logical length of deck
    var cardsPlayed = new Array(53); // cards played
    var countPlayed = 0;             // logical length of cardsPlayed
    var deck = new Array(53);        // deck of cards
    
    // ---- Functions -----
    
    function buildNewDeck() {
        'use strict';
        var i;
        for (i = 1; i < 14; i += 1) {    // iterate from ace (1) to king (13)
            deck[i] = { ordinal: i, suit: "c" };        // clubs
            deck[i + 13] = { ordinal: i, suit: "d" };   // diamonds
            deck[i + 26] = { ordinal: i, suit: "h" };   // hearts
            deck[i + 39] = { ordinal: i, suit: "s" };   // spades
        } // end for
        cardsLeft = 52;      // used in simulated shuffle getNextCard();
        // simulate draw deck with facedown card
        //   (note this is a different array)
        cardsPlayed[0] = {ordinal: 0, suit: "facedown", x: 0, y: 0};
        countPlayed = 0;
    } // end function buildNewDeck
    
    function getNextCard() {
        'use strict';
        var i, cardToReturn;
        if (cardsLeft < 1) {
            alert("Game Over");
            return false;
        }
        // get a number between 1 and the number of remaining cards
        i = Math.floor(Math.random() * cardsLeft) + 1;
        if (i > cardsLeft) {
            i = cardsLeft;
        } // end if
        cardToReturn = deck[i];
        while (i < cardsLeft) {    //logically remove and compress deck
            deck[i] = deck[i + 1];
            i += 1;
        } // end while
        cardsLeft -= 1;              //set logical length
        return cardToReturn;
    } // end function getNextCard
    What do you think?

    Regards,

    grNadpa

  2. #2
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's the GPL-Compatible Free Software License
    Copying and distribution of this file, with or without modification,
    are permitted in any medium without royalty provided the copyright
    notice and this notice are preserved. This file is offered as-is,
    without any warranty.

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    The code above has been changed making this thread moot.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •