www.webdeveloper.com
Results 1 to 3 of 3

Thread: New to Javascript [HELP]

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Question New to Javascript [HELP]

    So I just finished a basic tutorial online on Javascript building objects, functions,etc.. and in the end had a small code for building a game of blackjack. I developed the code in a "scratch pad" type environment; it is really basic with alert boxes and I had problems displaying the score inside of them but it does work so far!

    Now I am looking to display the cards as images and display the score as the game goes along, not anything fancy just the cards showing up in the top left corner or something would be fine 2 for the player and 1 starting out for the dealer. Also if you have any suggestions on improving the game I am fine with criticism. Also I do have a folder containing the images for a deck of cards ( KS.png.. 9S.png.. ) thanks for any help in advance! Keep in mind I am new to all of this!

    Code:
    // Card Constructor 
    var Card = function(cardSuit, cardNumber){
     var suit = cardSuit; 
     var number = cardNumber; 
     this.getNumber = function(){
     return number;
     }; 
     this.getSuit = function(){ 
     return suit;
     }; 
     this.getValue = function(){ 
     if (number === 1){ 
     return 11; 
     }
     else if(number > 1 && number < 10){ 
     return number;
     }
     else{ 
     return 10;
     }
     };
    }; 
    // deal 
    deal = function(){
     var cardSuit = Math.floor(Math.random()*4);
     var cardNumber = Math.floor(Math.random()*13);
     return new Card(cardSuit, cardNumber);
    };
    var Hand = function(){
     var card1 = deal();
     var card2 = deal();
     var cards = [card1, card2];
     this.getHand = function(){
     return cards;
     };
     this.score = function(){
     var sum =0;
     var aces = 0;
     for (i=0; i0&&sum>21){
     sum-=10;
     aces--;
     }
     return sum;
     };
     this.printHand = function(){
     var string = "";
     for(i=0; i21){
     if(dealerHand.score()>21){
     return "You tied!";
     }
     else{
     return "You lose!";
     }
     }
     else if(dealerHand.score()>21){
     return "You win!";
     }
     else if(userHand.score()>dealerHand.score()){
     return "You win!";
     }
     else if(userHand.score()===dealerHand.score()){
     return "You tied!";
     }
     else{
     return "You lose!";
     }
    }
    function playGame(){
     var userHand = playAsUser();
     var dealerHand = playAsDealer();
     var results = declareWinner(userHand,dealerHand);
     console.log("You have " + userHand.printHand());
     console.log("Dealer has " + dealerHand.printHand());
     console.log(results);
    }
    playGame();

  2. #2
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    Quote Originally Posted by Timeflies View Post
    I am fine with criticism.
    As time goes by many serious web developers surround themselves with tools to help them with their task. There are a myriad of them available to do something simple or some fancy stuff. The best thing about most of those tools is they are free; yes free no cost, zilch.

    With the better tools available for free one stands out from the rest and is used by most if not all developers. When used you can inspect your html and css, but the best part is what it can do with JavaScript. It will tell you what and where your errors are, actually it lights up like a Christmas tree. If you think that is cool wait till you find out you can go through you JavaScript line by line seeing what values are in your variables just by hovering your mouse over the variable. You will think you are in heaven.

    Because this tool is so popular (and of course free) it is now incorporated within all browsers and you thought it could not get any better.

    So next time you use your browser hunt around for sumpin that looks like developer tools.

    I am not going to test how well you can take criticism at this time but let you be wowed by the developers tools in your browser.

  3. #3
    Join Date
    Feb 2013
    Posts
    2
    Thanks for the reply, is there any you would recommend?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles