tothemax
tothemax

Reputation: 115

Same value being passed to seprate object's arrays

I am attempting to make a card game and I've included most of my code because I honestly am not sure what is causing this although I will try to be as specific as possible. When playing with 3 players they are stored as objects each with "hand" arrays I have pushed all of these player objects in an array called humanPlayers. My problem though, resides in the dealCards function. It successfully pushes the cards from the deck array into the players objects hand arrays. My problem though is that inside the switch statement of dealCards it is pushing the same 7 cards to each object when I want them to be separate cards. I have console.log() dealtCards and it seems to be 7 different cards each time it is about to push them into the players arrays but it always ends up being the same 7 cards given to all the hand arrays? Any help or suggestions are appreciated, thank you.

let deck = [
  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "rSkip", "rSkip", "rReverse", "rReverse", "r+2", "r+2",
  "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "bSkip", "bSkip", "bReverse", "bReverse", "b+2", "b+2",
  "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "gSkip", "gSkip", "gReverse", "gReverse", "g+2", "g+2",
  "y0", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "ySkip", "ySkip", "yReverse", "yReverse", "y+2", "y+2", "Wild", "Wild", "Wild", "Wild", "Wild+4", "Wild+4", "Wild+4", "Wild+4"
];

function shuffleDeck() {
  for (let i = 0; i <= deck.length; i++) {
    let randomNum = Math.floor(Math.random() * deck.length);
    deck.splice(randomNum, 0, deck[0]);
    deck.splice(0, 1, deck[randomNum + 1]);
    deck.splice(randomNum + 1, 1);
  }
}

function player() {
  this.hand = [];
};

let Player1 = new player();
let Player2 = new player();
let Player3 = new player();

let humanPlayers = [];

//initialize the participants
initializePlayers = prompt("How many players? (1-4)");

if (initializePlayers < 4) {
  let initializeString2Num = parseInt(initializePlayers);
  for (let j = 0; j <= initializeString2Num - 1; j++) {
    //pushing human objects into array
    switch (initializePlayers) {
      case "1":
        humanPlayers.push(Player1);
        break;
      case "2":
        humanPlayers.push(Player2);
        break;
      case "3":
        humanPlayers.push(Player3);
        break;
    }
  }
}
shuffleDeck();
dealCards(humanPlayers.length);

function dealCards(humans) {
  console.log("number of humans to deal to " + humanPlayers.length);

  switch (humans) {
    case 3:
      let dealtCard;
      console.log("case 3");
      for (let l = 0; l <= 7; l++) {
        if (l <= 6) {
          dealtCard = deck.shift();
        }
        if (humanPlayers[0].hand.length <= 6) {
          humanPlayers[0].hand.push(dealtCard);
          console.log(dealtCard);
        }
      }

      for (let m = 0; m <= 7; m++) {
        if (m <= 6) {
          dealtCard = deck.shift();
        }
        if (humanPlayers[1].hand.length <= 6) {
          humanPlayers[1].hand.push(dealtCard);
          console.log(dealtCard);
        }
      }
      for (let n = 0; n <= 7; n++) {
        if (n <= 6) {
          dealtCard = deck.shift();
        }
        if (humanPlayers[2].hand.length <= 6) {
          humanPlayers[2].hand.push(dealtCard);
          console.log(dealtCard);
        }
      }
      break;
  }
}

console.log(humanPlayers);

Upvotes: 0

Views: 79

Answers (3)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

Your code is abit unclear, it looks as if your assigning players before asking for how many are playing, then dealing the cards in a convoluted way.

You have multiple cards of the same value, so you will get some duplicates.

Below is a simplified rewrite which will yield the same result your after, n * player each with a hand of cards from the shuffled deck.

// deck
const deck = {
  cards: [
    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "rSkip", "rSkip", "rReverse", "rReverse", "r+2", "r+2",
    "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "bSkip", "bSkip", "bReverse", "bReverse", "b+2", "b+2",
    "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "gSkip", "gSkip", "gReverse", "gReverse", "g+2", "g+2",
    "y0", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "ySkip", "ySkip", "yReverse", "yReverse", "y+2", "y+2", "Wild", "Wild", "Wild", "Wild", "Wild+4", "Wild+4", "Wild+4", "Wild+4"
  ],
  shuffle() {
    for (let i = this.cards.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
    }
    return this.cards;
  }
}

// table
const table = []

// player
function player() {
  this.hand = []
  return this
}

// amount of cards to deal
const card_count = 3

// ask players
let player_count = 0
if (player_count = parseInt(prompt("How many players? (1-4)", 1), 10)) {

  // shuffle deck
  const cards = deck.shuffle()

  // create players and deal cards 
  for (let i = 1; i <= player_count; i++) {
    // new player
    let plr = new player()
    // assign player cards from shuffle deck
    plr.hand = cards.slice((i - 1) * card_count, ((i - 1) * card_count) + card_count)

    // add player to table
    table.push(plr)
  }
}

console.log(JSON.stringify(table))

Upvotes: 1

Barmar
Barmar

Reputation: 781741

The problem is your switch/case statements to fill in humanPlayers. You're using initializePlayers as the variable to test each time through the loop. This is always 3, so you keep adding Player3 to the array.

You should be testing j, and the cases should be 0, 1, and 2.

let deck = [
  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "rSkip", "rSkip", "rReverse", "rReverse", "r+2", "r+2",
  "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "bSkip", "bSkip", "bReverse", "bReverse", "b+2", "b+2",
  "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9", "gSkip", "gSkip", "gReverse", "gReverse", "g+2", "g+2",
  "y0", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8", "y9", "ySkip", "ySkip", "yReverse", "yReverse", "y+2", "y+2", "Wild", "Wild", "Wild", "Wild", "Wild+4", "Wild+4", "Wild+4", "Wild+4"
];

function shuffleDeck() {
  for (let i = 0; i <= deck.length; i++) {
    let randomNum = Math.floor(Math.random() * deck.length);
    deck.splice(randomNum, 0, deck[0]);
    deck.splice(0, 1, deck[randomNum + 1]);
    deck.splice(randomNum + 1, 1);
  }
}

function player() {
  this.hand = [];
};

let Player1 = new player();
let Player2 = new player();
let Player3 = new player();

let humanPlayers = [];

//initialize the participants
initializePlayers = prompt("How many players? (1-4)");

if (initializePlayers < 4) {
  let initializeString2Num = parseInt(initializePlayers);
  for (let j = 0; j < initializeString2Num; j++) {
    //pushing human objects into array
    switch (j) {
      case 0:
        humanPlayers.push(Player1);
        break;
      case 1:
        humanPlayers.push(Player2);
        break;
      case 2:
        humanPlayers.push(Player3);
        break;
    }
  }
}
shuffleDeck();
dealCards(humanPlayers.length);

function dealCards(humans) {
  console.log("number of humans to deal to " + humanPlayers.length);

  switch (humans) {
    case 3:
      let dealtCard;
      console.log("case 3");
      for (let l = 0; l <= 7; l++) {
        if (l <= 6) {
          dealtCard = deck.shift();
        }
        if (humanPlayers[0].hand.length <= 6) {
          humanPlayers[0].hand.push(dealtCard);
          console.log(dealtCard);
        }
      }

      for (let m = 0; m <= 7; m++) {
        if (m <= 6) {
          dealtCard = deck.shift();
        }
        if (humanPlayers[1].hand.length <= 6) {
          humanPlayers[1].hand.push(dealtCard);
          console.log(dealtCard);
        }
      }
      for (let n = 0; n <= 7; n++) {
        if (n <= 6) {
          dealtCard = deck.shift();
        }
        if (humanPlayers[2].hand.length <= 6) {
          humanPlayers[2].hand.push(dealtCard);
          console.log(dealtCard);
        }
      }
      break;
  }
}

console.log(humanPlayers);

Upvotes: 1

Robin Zigmond
Robin Zigmond

Reputation: 18249

The problem is that all the player objects in the players array are references to the same object. Check the loop where you add these, you are either adding Player1 twice, Player2 three times, or Player3 four times.

One simple way to fix this is to remove the let Player1 = new player(); etc, and just directly put new player() in the push method inside that loop. (The switch also then becomes completely pointless.) This ensures that the player objects are all distinct.

Upvotes: 1

Related Questions