Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: NiksterG on Sun 09/03/2008 20:44:22

Title: Shuffling cards not working
Post by: NiksterG on Sun 09/03/2008 20:44:22
Hi all!

I'm having the weirdest problem. I'm making a card game. Obviously, I need a shuffling algorithm. Now, a while ago, I actually had working shuffling code. However, it was very clumsy, and I could foresee problems in the future if I left it that way. So I recently changed it from using only arrays to using characters (one character for the deck, one for the discard pile, one for opponent deck, etc.) This is the code I came up with to shuffle the player's deck:


function ShufflePlayerDeck() { // shuffles player's deck twice
  int i=0;
  InventoryItem* playerdeck[40];
  SetGameOption(OPT_MULTIPLEINV, 1);

  while (i<PlayerDeck.ItemCount) {
    playerdeck[i] = PlayerDeck.ItemAtIndex[i];
    i++;
  }
  SetGameOption(OPT_MULTIPLEINV, 0);

  InventoryItem* holder;

  i=0;
  while (i<GetCardsInPlayerDeck()) {
    int randomspot = Random(GetCardsInPlayerDeck()-1);
    holder = playerdeck[i];
    playerdeck[i] = playerdeck[randomspot];
    playerdeck[randomspot] = holder;
    i++;
  }
  i=0;
  while (i<GetCardsInPlayerDeck()) {
    int randomspot = Random(GetCardsInPlayerDeck()-1);
    holder = playerdeck[i];
    playerdeck[i] = playerdeck[randomspot];
    playerdeck[randomspot] = holder;
    i++;
  }

  while (PlayerDeck.ItemAtIndex[0] != null) {
    cPlayerdeckc.LoseInventory(PlayerDeck.ItemAtIndex[0]);
  }

  i=0;
  while ((i<40) && (playerdeck[i] != null)) {
    cPlayerdeckc.AddInventory(playerdeck[i]);
    i++;
  }

}


This is basically a translation of using arrays to using characters. The problem is that now it doesn't shuffle at all! I've run through the code a hundred times. Can anyone see the problem?

The GetCardsInPlayerDeck() function returns the number of cards in the player's deck. I've checked if that may be the problem, but it isn't. It returns 26, which is the correct number of cards the player has when I try shuffling the deck.

I'm using AGS version 3.0.1 beta.
Title: Re: Shuffling cards not working
Post by: monkey0506 on Mon 10/03/2008 02:28:18
One potential problem I see would be does the PlayerDeck InvWindow update when the OPT_MULTIPLEINV flag is changed? If not, should you perhaps call UpdateInventory() afterwards?

Other than that...I know you said GetCardsInPlayerDeck() returns 26...without actually having seen the function is there any possibility it's returning different values throughout the function? Does storing its value once (in an int variable) and then referencing that variable instead of calling the function again change the result? It shouldn't, but it was a possibility that crossed my mind. Also I think it should be slightly faster (run-time) that way anyway. ;)
Title: Re: Shuffling cards not working
Post by: NiksterG on Wed 12/03/2008 01:36:59
Sorry for the long wait for a reply. School's been keeping me busy, that's for sure.

I tried what you suggested, monkey, but it' didn't seem to help. I'm going run some more diagnostics, but maybe someone here can find what I'm doing wrong.

This is the GetCardsInPlayerDeck code I have:


function GetCardsInPlayerDeck() {
  int i=1;
  int numofcards = 0;
  while (i<=Game.InventoryItemCount) {
    numofcards = numofcards + cPlayerdeckc.InventoryQuantity[i];
    i++;
  }
  return numofcards;
}


Maybe that helps. Like I said, it's consistently returning the value I expect.