Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: abibliophobe on Mon 31/01/2005 14:17:00

Title: Dealing cards randomly to seven characters...? [SOLVED]
Post by: abibliophobe on Mon 31/01/2005 14:17:00
 ??? I have tried several different approaches to do this, and I can't get the scripting to work the way I want it to!
I am trying to give seven different characters inventory items (I was quite surprised to discover NPCs have inventories) randomly (oh, and the items are the cards mentioned in the title). I imagine it would be simple to give them specific items, but I don't want any of these randomly given items to be given to more than one character...

Does anyone know of an easy way of doing this? Or if it is even possible? Help please!
Title: Re: Dealing cards randomly to seven characters...?
Post by: Scorpiorus on Mon 31/01/2005 14:33:00
What item numbers are these cards?
Title: Re: Dealing cards randomly to seven characters...?
Post by: Radiant on Mon 31/01/2005 15:23:52

int cnum=1;
while (cnum < 8) {
  int itm=0;
  while (itm<4) {
    AddInventoryToCharacter (cnum, Random (20) + 10);
    itm++;
  }
  cnum ++;
}


will give characters 1 - 7 four cards each, given that the cards are inventory items 10 through 30 inclusive.
Title: Re: Dealing cards randomly to seven characters...?
Post by: Kweepa on Mon 31/01/2005 16:04:02
Radiant,
Your solution will very likely give the same card to more than one person.

The best way to do this in AGS is I think to make a list of the cards, shuffle it, and deal the cards out.

With the assumptions: cards are inv 10-37, characters 1-7:


// make a list
int cardList[28];
int card = 0;
while (card < 28)
{
Ã,  cardList[card] = 10 + card;Ã,  // inv 10-37
Ã,  card++;
}

// shuffle it
int shuffleSteps = 50;
while (shuffleSteps > 0)
{
Ã,  // swap two random cards
Ã,  int firstCardIndex = Random(27);
Ã,  int secondCardIndex = Random(27);
Ã,  int temp = cardList[firstCardIndex];
Ã,  cardList[firstCardIndex] = cardList[secondCardIndex];
Ã,  cardList[secondCardIndex] = temp;
Ã,  shuffleSteps--;
}

// now deal out to characters
int charIndex = 1;
int cardIndex = 0;
while (charIndex < 8)
{
Ã,  int cardCount = 0;
Ã,  while (cardCount < 4)
Ã,  {
Ã,  Ã,  AddInventoryToCharacter(charIndex, cardIndex);
Ã,  Ã,  cardIndex++;
Ã,  Ã,  cardCount++;
Ã,  }
Ã,  charIndex++;
}


Cheers,
Steve
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 00:34:15
Well, since there are only seven cards, they are items one through to seven (inclusive) but I will try to figure out what you've done and fix it... I hope...  ;D

Thanks!
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 00:42:17
Is there any way to stop any of the characters from getting inventory item 0 - since there is not such item, but it can come up by using Random(x)?
(Was this already fixed and I just stuffed it up by "editing" the script?)
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 00:51:42
Don't worry, I fixed that by changing a number somewhere (I'd made it zero from the original ten rather than changing it to one).
However, now the characters seem to get the same cards everytime I play...
(that's a problem  ;) )
Title: Re: Dealing cards randomly to seven characters...?
Post by: Kweepa on Tue 01/02/2005 01:00:48
Oh good.

I don't know if you can "seed" the random number generator (don't see anything in the help) but you can do this to fake it...


int seed = GetTime(3); // returns the current time's seconds (0-59)
while (seed > 0)
{
Ã,  int chuckAway = Random(99);
Ã,  seed--;
}
// then the shuffle code
// ...
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 01:16:38
That didn't help.

Maybe I should tell you what I've done. I have put it in the interactions thingy for when the player enters the screen  (before fadein). Every time the room reloads it removes all the inventory items everyone has and then re-gives them out (and supposedly this should be different every time). However, it is not!  :)
(oh, and is there a simple way of removing everyone's inventory items without listing them all (ie. LoseInventoryFromCharacter(ONE, 1) etc. all the way through - it does nothing if they don't have the item luckilly!)?)
Title: Re: Dealing cards randomly to seven characters...?
Post by: Kweepa on Tue 01/02/2005 01:29:24
Removing the inventory items - I'd put it in a loop.
(Alternatively you could store which card each character has in an array.)


int charIndex = 1;
while (charIndex < 8)
{
  int cardIndex = 0;
  while (cardIndex < 7)
  {
    LoseInventoryFromCharacter(charIndex, cardIndex);
    cardIndex++;
  }
  charIndex++;
}


Perhaps you should post your code to deal out the cards. It *should* be different every time...
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 01:39:19
Actually I had just decided that my changes must be the problem! I'll post the whole thing so farÃ,  ;D

int charIndex2 = 0;
while (charIndex2 < 8)
{
Ã,  int cardIndex2 = 0;
Ã,  while (cardIndex2 < 8)
Ã,  {
Ã,  Ã,  LoseInventoryFromCharacter(charIndex2, cardIndex2);
Ã,  Ã,  cardIndex2++;
Ã,  }
Ã,  charIndex2++;
}

// make a list
int cardList[28];
int card = 1;
while (card < 28)
{
Ã,  cardList[card] = 0 + card;Ã,  // inv 0-7
Ã,  card++;
}



// shuffle it
int shuffleSteps = 50;
while (shuffleSteps > 0)
{
Ã,  // swap two random cards
Ã,  int firstCardIndex = Random(27);
Ã,  int secondCardIndex = Random(27);
Ã,  int temp = cardList[firstCardIndex];
Ã,  cardList[firstCardIndex] = cardList[secondCardIndex];
Ã,  cardList[secondCardIndex] = temp;
Ã,  shuffleSteps--;
}

// now deal out to characters
int charIndex = 0;
int cardIndex = 1;
while (charIndex < 8)
{
Ã,  int cardCount = 0;
Ã,  while (cardCount < 4)
Ã,  {
Ã,  Ã,  AddInventoryToCharacter(charIndex, cardIndex);
Ã,  Ã,  cardIndex++;
Ã,  Ã,  cardCount++;
Ã,  Ã,  charIndex++;
Ã,  }

}}

I changed some of the numbers to zero so that the player is also included (being character number 0)
Title: Re: Dealing cards randomly to seven characters...?
Post by: Kweepa on Tue 01/02/2005 01:48:55
Yipes - I see I forgot to look up the new cards in the card list!

Anyway, here is some code that should work:


// "seed" the random number generator
int seed = GetTime(3); // returns the current time's seconds (0-59)
while (seed > 0)
{
Ã,  int chuckAway = Random(99);
Ã,  seed--;
}

// remove cards from characters
int charIndex2 = 0;
while (charIndex2 < 8)
{
Ã,  int cardIndex2 = 0;
Ã,  while (cardIndex2 < 8)
Ã,  {
Ã,  Ã,  LoseInventoryFromCharacter(charIndex2, cardIndex2);
Ã,  Ã,  cardIndex2++;
Ã,  }
Ã,  charIndex2++;
}

// make a list
int cardList[8];
int card = 0;
while (card < 8)
{
Ã,  cardList[card] = 0 + card;Ã,  // inv 0-7
Ã,  card++;
}

// shuffle it
int shuffleSteps = 50;
while (shuffleSteps > 0)
{
Ã,  // swap two random cards
Ã,  int firstCardIndex = Random(7);
Ã,  int secondCardIndex = Random(7);
Ã,  int temp = cardList[firstCardIndex];
Ã,  cardList[firstCardIndex] = cardList[secondCardIndex];
Ã,  cardList[secondCardIndex] = temp;
Ã,  shuffleSteps--;
}

// now deal out to characters
int charIndex = 0;
int cardIndex = 0;
while (charIndex < 8)
{
Ã,  AddInventoryToCharacter(charIndex, cardList[cardIndex]); // my mistake first time
Ã,  cardIndex++;
Ã,  charIndex++;
}
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 01:56:30
Thanks, that works so much better!
BUT (lol) it seems to be giving out inventory item 0, which does not exist (and therefore the character does not get a card).
Title: Re: Dealing cards randomly to seven characters...?
Post by: Kweepa on Tue 01/02/2005 02:14:05
You changed that line! :=
What inventory numbers are the cards? 1-8? If so then make it 1+card when making a list rather than 0+card.
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 02:21:13
Actually 1-7  ;)
I'll try it again and see if it works.
Title: Re: Dealing cards randomly to seven characters...?
Post by: Kweepa on Tue 01/02/2005 03:38:11
Umm, you have seven cards and eight characters?
You're on your own... :=
Title: Re: Dealing cards randomly to seven characters...?
Post by: abibliophobe on Tue 01/02/2005 04:59:58
No, no, no!  ;D There are seven of each, but the cards go from one to seven, and the characters go from zero to six (the seventh character is the player). So there are seven of each.
And I got it working anyway, I just had to change a couple of numbers.
So, problem solved! Thanks!