TimeAxis
TimeAxis

Reputation: 3

Passing hands of cards around a table in Javascript

This seems like a stupidly simple thing to do, but I can't figure out what I'm doing wrong. The goal is to have an array of 8 other arrays, which each contain hands of cards (in the example, the arrays just contain arbitrary numbers). Then, depending on whether passDirection is set to -1 or 1, each array is cycled through and replaced with the one next to it. The desired end result is that the values of playerList essentially shift by 1 either up or down, and this can be repeated several times without issue.

What's actually happening with the code I have below, though, is that all the arrays are just being replaced with what's at index 0, except for the first one. How can I fix this?

var playerList = new Array;
var passDirection = -1;

for(i = 0; i < 8; i++) {
  playerList.push([playerList.length,i]); // Fill Arrays with arbitrary data
}

for (i=0; i< playerList.length; i++) {
  console.log(i + ": " + playerList[i]); // Check their values before anything is done to them
}

for(q=0; q < 5; q++){ // Repeat the process 5 times, just because.

  var bufferArray = playerList[0]; // Put Array Element 0's value in a buffer as it will be replaced first

  for(i = 0; i < playerList.length && i > (playerList.length * -1); i += passDirection) {
      var catcher = i; // 'catcher' should be the array that gets replaced
      var passer = catcher - passDirection; // 'passer' should be the one it gets replaced with
      if (catcher < 0) {
          catcher = catcher + playerList.length;
      }
      if (passer < 0) {
          passer = passer + playerList.length;
      } else if (passer >= playerList.length) {
          passer = passer - playerList.length;
      }

      if (passer == 0) {
          playerList[catcher] = bufferArray;

      } else {
          playerList[catcher] = playerList[passer];
      }
  }

  for (i=0; i< playerList.length; i++) {
    console.log(i + ": " + playerList[i]);
  }
  console.log("...");
}

https://jsfiddle.net/3r1Lhwc5

Upvotes: 0

Views: 41

Answers (2)

trincot
trincot

Reputation: 350781

You have two errors in your code:

  • if (passer = 0) is performing an assignment. You need if (passer === 0).

  • The passer index is looking at the wrong side of the value. Currently you are first getting from 1 and putting at 0, then getting from 0 and putting at 7 (i.e. -1). Notice how you are moving the same value in the second iteration. You need to change passer = catcher - passDirection to passer = catcher + passDirection

Note that all this can be done much easier with the splice, shift, unshift, pop and push Array methods (on the main playerList).

Upvotes: 1

samlandfried
samlandfried

Reputation: 821

You can make your life easier by using Array methods to move elements from the beginning to end of an array or vice versa. Using this in the body of your for loop should do the trick:

if (passDirection === -1) {
    const first = playerList.shift();
    playerList.push(first);
}
else {
    const last = playerList.pop();
    playerList.unshift(last);
}

Upvotes: 0

Related Questions