Dealing cards randomly to seven characters...? [SOLVED]

Started by abibliophobe, Mon 31/01/2005 14:17:00

Previous topic - Next topic

abibliophobe

 ??? 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!

Scorpiorus


Radiant

Code: ags

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.

Kweepa

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:

Code: ags

// 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
Still waiting for Purity of the Surf II

abibliophobe

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!

abibliophobe

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?)

abibliophobe

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  ;) )

Kweepa

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...

Code: ags

int seed = GetTime(3); // returns the current time's seconds (0-59)
while (seed > 0)
{
Ã,  int chuckAway = Random(99);
Ã,  seed--;
}
// then the shuffle code
// ...
Still waiting for Purity of the Surf II

abibliophobe

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!)?)

Kweepa

Removing the inventory items - I'd put it in a loop.
(Alternatively you could store which card each character has in an array.)

Code: ags

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...
Still waiting for Purity of the Surf II

abibliophobe

Actually I had just decided that my changes must be the problem! I'll post the whole thing so farÃ,  ;D
Code: ags

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)

Kweepa

Yipes - I see I forgot to look up the new cards in the card list!

Anyway, here is some code that should work:

Code: ags

// "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++;
}
Still waiting for Purity of the Surf II

abibliophobe

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).

Kweepa

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.
Still waiting for Purity of the Surf II

abibliophobe

Actually 1-7  ;)
I'll try it again and see if it works.

Kweepa

Umm, you have seven cards and eight characters?
You're on your own... :=
Still waiting for Purity of the Surf II

abibliophobe

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!

SMF spam blocked by CleanTalk