javascript - Dealing Cards from a Deck and Removing the Cards from an Array - Stack Overflow

I have an array of card names like this: var deckNames = [ "unused","sA", "s2&

I have an array of card names like this:

var deckNames = [ "unused",
  "sA", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "sJ", "sQ", "sK",
  "hA", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "hJ", "hQ", "hK",
  "cA", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "cJ", "cQ", "cK",
  "dA", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "dJ", "dQ", "dK",
  ];

And I want to write a function that randomly selects a card from the array and also removes it from the "deckNames" array above. I have written the following, but it does not seem to be working.

var deal = function(){
    var card = Math.floor(Math.random() * 52) + 1;
    return deckNames[card];
    deckNames.splice(card,1);
};

When I run the deal function in the console, it randomly picks and returns a card from the array, but the deckNames array itself does not get the dealt card removed from the array. How can I achieve this? Thank you.

I have an array of card names like this:

var deckNames = [ "unused",
  "sA", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "sJ", "sQ", "sK",
  "hA", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "hJ", "hQ", "hK",
  "cA", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "cJ", "cQ", "cK",
  "dA", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "dJ", "dQ", "dK",
  ];

And I want to write a function that randomly selects a card from the array and also removes it from the "deckNames" array above. I have written the following, but it does not seem to be working.

var deal = function(){
    var card = Math.floor(Math.random() * 52) + 1;
    return deckNames[card];
    deckNames.splice(card,1);
};

When I run the deal function in the console, it randomly picks and returns a card from the array, but the deckNames array itself does not get the dealt card removed from the array. How can I achieve this? Thank you.

Share Improve this question edited Oct 10, 2016 at 22:59 nbrooks 18.2k5 gold badges56 silver badges67 bronze badges asked Oct 10, 2016 at 22:47 KnowledgeMCKnowledgeMC 1031 gold badge2 silver badges7 bronze badges 1
  • 2 You return the card before you do the splice... – Robin Mackenzie Commented Oct 10, 2016 at 22:50
Add a ment  | 

3 Answers 3

Reset to default 3

Your return statement ends your function before the deck is modified. Switch the statements around so that the return is the last thing in the function. Additionally, as @DavidE points out, you can't get the card from the array after it's already been removed, so you have to retrieve it before you remove it:

var deal = function(){
    var index = Math.floor(Math.random() * 52) + 1;
    var card = deckNames[index];
    deckNames.splice(index, 1);
    return card;
};

Or simply:

var deal = function(){
    var card = Math.floor(Math.random() * 52) + 1;
    return deckNames.splice(card, 1)[0];
};

(since splice returns the removed element, wrapped in a new array).


Some other things to consider:

  • Array indices start at 0, so chances are you don't want the +1 in your random number generator. You actually want numbers from 0 to 51*:

    var card = Math.floor(Math.random() * 52);

  • Every time you deal a card the size of the deck decreases. Instead of generating random numbers up to 51 each time, base that number on the size of the deck when the function is called. Otherwise you'll get index out of bound errors. See below.


Ultimately, this gives you something like this:

var deal = function(){
    var card = Math.floor(Math.random() * deckNames.length);
    return deckNames.splice(card, 1)[0];
};

It doesn't splice because the splice call is dead-code; meaning it's impossible for it to ever run.

Once you return, you "exit" the function. You need to splice before returning:

var deal = function(){
    var card = Math.floor(Math.random() * 52) + 1;
    deckNames.splice(card,1);
    return deckNames[card];
};

Consider:

var test = function(){
    console.log("Hello");
    return;
    console.log("World");
};

test();

What do you think that prints?

Edit:

On rereading your code again, I realized there is still a major issue that will pop up if you deal enough cards. You have 52 hard-coded into your function, but you're removing a card each call. What happens if there are only 10 cards, but the random number generator gives you a 50? Change the 52 to deckNames.length to make sure you aren't trying to deal cards that don't exist (which will result in an "array out of bounds" error).

First of all, you can get rid of your "unused", then check this out:

function deal(deck){
  var card = Math.floor(Math.random()*deck.length); // notice you add the one later - that's not right
  return deck.splice(card, 1);
}
var pulledCard = deal(deckNames); // now deckNames array is altered

Now learn this:

function CardGame(deck){
  this.deck = deck;
  this.deal = function(){
    return this.deck.splice(Math.random()*this.deck.length, 1)[0];
  }
  this.shuffle = function(){{
    var l = this.deck.length;
    this.deck.sort(function(a, b){
      return 0.5 - Math.floor(Math.random()*(l+1))/l;
    });
    return this.deck;
  }
}
var game = new CardGame(deckNames);
var alteredDeck = game.shuffle(); // alteredDeck is same as game.deck;
var singleCard = game.deal(); // game.deck is spliced

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745310369a4621968.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信