2
\$\begingroup\$

I've just completed my first JavaScript game. I strongly welcome any advice/opinions/insults on how crappy or good my game is.

Do you notice anything that is poorly done?

Is there anything that I can write better/shorter?

Here is a link where you can change the code/test the game/etc. http://labs.codecademy.com/Bnov#:workspace

suits = ["spades", "diamonds", "clubs", "hearts"];
deck = [];
var h1, h2, h3, hD, nHands; //global vars
for (var x=0; x<suits.length; x++) { for (var i=2; i<10; i++) { deck.push(i + " of " + suits[x]); } }
function shuffle(deck) {
 for (var i = deck.length - 1; i > 0; i--) {
 var j = Math.floor(Math.random() * (i + 1));
 var temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; } return deck; }
 shuffle(deck);
function Hand(name, sChips, cChips) {
 this.name = name;
 this.sChips = sChips; // Starting Chips
 this.cChips = cChips; // Current Chip count
}
function numHands() {
nHands = parseInt(prompt("How many hands do you want to play?(1,2 or 3)"), 10);
 if (nHands > 0 && nHands < 4) {
 x = 150000/nHands;
 if (nHands > 0) { h1 = new Hand("First Hand", x, x);
 if (nHands > 1) { h2 = new Hand("Second Hand", x, x);
 if (nHands > 2) { h3 = new Hand("Third Hand", x, x); } }
 hD = new Hand("Dealer"); } }
 else { numHands(); } }
function setBetAmount(hand) {
 hand.betAmount = parseInt(prompt(hand.name + ": " + "Place your bet! (bet between 0 and " + hand.cChips + ")"), 10);
 if (hand.betAmount >= 0 && hand.betAmount <= hand.cChips) {
 return hand.betAmount;
 }
 else { setBetAmount(hand); }
}
function recordBetAmount() {
 if (nHands > 0) { setBetAmount(h1);
 if (nHands > 1) { setBetAmount(h2);
 if (nHands > 2) { setBetAmount(h3); } } } }
function dealtwo() { xy = [deck.shift(), deck.shift()]; return xy; }
function dealone() { yx = deck.shift(); return yx; }
function dealCards() {
 if (nHands > 0) { h1.cards = dealtwo();
 if (nHands > 1) { h2.cards = dealtwo();
 if (nHands > 2) { h3.cards = dealtwo(); } }
 hD.cards = dealtwo(); } }
function addValueOfCards(cards) {
 var yyy = 0;
 for (var x = 0; x < cards.length; x++) {
 wwww = cards[x];
 yyy += parseInt(wwww[0], 10);
 }
 return yyy; 
}
function hitOrStay(cards, hand) {
 var x = addValueOfCards(cards);
 if (x < 17) {
 var option = prompt(hand.name + ": You have " + x + ". Do you want to hit or stay?").toLowerCase();
 if (option === "hit" || option === "h") {
 cards.push(dealone());
 hitOrStay(cards, hand);
 }
 else if (option === "stay" || option === "s") {
 console.log(hand.name + ": " + hand.cards);
 }
 else {
 hitOrStay(cards, hand);
 }
 }
 else if (x === 17 || x === 18) {
 console.log(hand.name + ": " + hand.cards);
 }
 else {
 console.log(hand.name + ": " + hand.cards);
 }
}
function dealerHitOrStay(cards) {
 var x = addValueOfCards(cards);
 if (x < 15) {
 cards.push(dealone());
 dealerHitOrStay(cards);
 }
 else if (x > 14 && x < 19) {
 console.log("Dealer Hand: " + hD.cards);
 }
 else {
 console.log("Dealer Hand: " + hD.cards);
 }
}
function tableOptions() {
 if (nHands > 0) {
 hitOrStay(h1.cards, h1);
 if (nHands > 1) {
 hitOrStay(h2.cards, h2);
 if (nHands > 2) {
 hitOrStay(h3.cards, h3);
 }
 }
 dealerHitOrStay(hD.cards);
 }
}
function recap() {
 if (nHands > 0) {
 wonOrLost(h1.cards, h1);
 if (nHands > 1) {
 wonOrLost(h2.cards, h2);
 if (nHands > 2) {
 wonOrLost(h3.cards, h3);
 }
 }
 again();
 }
}
function wonOrLost(cards, hand) {
 var x = addValueOfCards(cards);
 var y = addValueOfCards(hD.cards);
 if (x < 19 && (x > y || y > 18)) {
 console.log("\n" + hand.name + ": You Won. (you had " + x + ", dealer had " + y + ")");
 hand.cChips = hand.cChips + hand.betAmount;
 console.log(" + " + hand.betAmount);
 }
 else if (x < 18 && y < 19 && y > x) {
 console.log("\n" + hand.name + ": You Lost. (you had " + x + ", dealer had " + y + ")");
 hand.cChips = hand.cChips - hand.betAmount;
 console.log(" - " + hand.betAmount);
 }
 else if (x > 18) {
 console.log("\n" + hand.name + ": You Lost. (you busted with " + x + ")");
 hand.cChips = hand.cChips - hand.betAmount;
 console.log(" - " + hand.betAmount);
 }
 else {
 console.log("\n" + hand.name + ": You Tied. (you and dealer both had " + x + ")");
 console.log(" + 0");
 }
}
function again() {
 if (deck.length > 10) {
 xxxx = prompt("Hit Enter to play another round. Type NO, then hit enter, to stop playing.").toLowerCase();
 if (xxxx === "no") {
 console.log("You chose to stop. Thanks for playing!");
 }
 else {
 console.log("____________\n");
 recordBetAmount();
 dealCards();
 tableOptions();
 recap();
 }
 }
 else {
 console.log("\nGame Over! (not enough cards to keep playing)");
 console.log("_________________________________________________");
 if (nHands === 1) {
 x = h1.cChips; }
 else if (nHands === 2) {
 x = h1.cChips + h2.cChips; }
 else if (nHands === 3) {
 x = h1.cChips + h2.cChips + h3.cChips; }
 if (x > 150000) {
 x = x - 150000;
 console.log("Your total score is: " + x); }
 else { console.log("Your total score is: 0. (which is the worst you can do)"); }
 }
}
numHands();
recordBetAmount();
dealCards();
tableOptions();
recap();
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 25, 2013 at 2:14
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

My remarks about the Ruby version of your code also apply to your JavaScript port.

For example hitOrStay() should not recurse, because you're just using it as a goto.

The players' hands should be stored in an array so that you don't have to write special cases for one, two, or three players.

answered Dec 25, 2013 at 7:19
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.