-1

I am trying to create a Blackjack game and I am stuck at the part where I am dealing to the player. My function dealHands tallies the points like I want but it will not display the Value and Suit that I need. I feel like I am missing something but not sure what. Any help would be appreciated. So far I have tried to convert player[x].Hand.push(card) to a string but that just prints obj undefined and it also doesn't tally the score since it turns the property into a string.

var suits = ["Spades","Hearts","Diamonds","Clubs"];
var values = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];
var deck = new Array();

//creates players
var players = new Array();
function createPlayers(num) {
  players = new Array();
  for (var i = 1; i <= num; i++) {
    var hand = new Array();
    var player = { Name: 'Player ' + i, ID: i, points: 0, Hand: hand };
    players.push(player);
  }
}

//Creates deck
function createDeck(x) {
    deck = new Array();
    for (var i = 0; i < values.length; i++) {
        for (var x = 0; x < suits.length; x++) {
            var weight = parseInt(values[i]);
            if (values[i] == "J" || values[i] == "Q" || values[i] == "K") {
                weight = 10;
            } if (values[i] == "A") {
                weight = 11;
            }
            var card = { Value: values[i], Suit: suits[x], Weight: weight };
            deck.push(card);
        }
    }
}

//shuffles
    function shuffle() {
        for (var i = 0; i < 1000; i++) {
            var location1 = Math.floor((Math.random() * deck.length));
            var location2 = Math.floor((Math.random() * deck.length));
            var tmp = deck[location1];
            deck[location1] = deck[location2];
            deck[location2] = tmp;
        }
    }

//starts the game
function startblackjack(num){
  currentPlayer = 0;
  createDeck();
  shuffle();
  createPlayers(num);
  dealHands()
  
  console.log(players)
  console.log(deck)

}

//Deals
function dealHands(){
  for(var i =0; i<2;i++){
    for( var x = 0; x<players.length; x++){
      var card = deck.pop();
      players[x].Hand.push(card);
      updatePoints();
    }
  }
}

//updatePoints
function updatePoints(){
  for (var i = 0; i<players.length;i++){
    getPoints(i);
  }
}

//getspoints
function getPoints(player){
  var points = 0;
  for(var i = 0; i < players[player].Hand.length; i++) {
    points += players[player].Hand[i].Weight;
  }players[player].points = points;
  return points;
}

startblackjack(1)
Wyck
  • 10,311
  • 6
  • 39
  • 60
Twall
  • 11
  • 2
  • It is a logging artifact. [Here's why](https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object) you're getting `[Object]`. Try `console.dir(players, { depth: Infinity })` – Wyck Aug 31 '23 at 02:46
  • Thanks! This does point me in the right direction. The link also helped me understand a bit. But I believe I might be adding this console.dir in the wrong place Now it is printing the full Hand but also the previous error as well. – Twall Aug 31 '23 at 03:03
  • I was suggesting to add it instead of `console.log(players)` Your question could be improved if you spelled out in detail the exact text of the expected output and the exact text of the unexpected actual output. – Wyck Aug 31 '23 at 03:36
  • Oh that makes sense and it fixed my issue! Sorry i am still new to Java script. I think my attention was focused on the wrong part of algo. As for the question you are right, it definitely could have been clearer. I thought I posted the unexpected output with the code block but I missed it in the final editting phase – Twall Aug 31 '23 at 03:55

0 Answers0