Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Neo on Sun 24/08/2008 15:09:37

Title: Random Positioning - Objects (Memory Game) (SOLVED)
Post by: Neo on Sun 24/08/2008 15:09:37
Hi all,

Some time ago I had an idea to create a memory game (pairs) in AGS, and while contemplating
how the game would work I thought It would be a breeze.

Well, I was wrong.

I have everything  figured out, but one thing - how to randomly position 32 cards (for which I used objects) without typing thousands of lines of code.

What I wanted was to, before room fade-in, allocate cards(objetcs) randomly to predefined coordinates.

Something like:
random_number = Random(31);
if (random_number == 0) {
object[0].SetPosition(4, 104);
}
else {
if (random_number == 1) {
object[1].SetPosition(4, 104);
}
.
.
.
.
.
.


So, my question is how would you go about it? How would you randomly position, let's say, 8 cards in 8 predefined positions?


Hope I'm in the right forum ???

Cheers!

-Neo Jezh
Title: Re: Random Positioning - Objects (Memory Game)
Post by: Gilbert on Sun 24/08/2008 15:11:25
Something like:

random_number = Random(31);
object[random_number].SetPosition(4, 104);


Title: Re: Random Positioning - Objects (Memory Game)
Post by: Neo on Sun 24/08/2008 18:30:33
Quote from: Gilbot V7000a on Sun 24/08/2008 15:11:25
Something like:

random_number = Random(31);
object[random_number].SetPosition(4, 104);




I'm an idiot. Can't belive I missed that.

But the thing is, with that code I put one object to that position (position1), now I need to fill the remaining positions.

For example 8 objects into 8 predefined positions. I don't see how I could go about this, certanly not like this


random_0 = Random(7);
object[random_0].SetPosition(4, 104);

random_1 = Random(7);
if  (random_0 == random_1) {  // If the number (object) is the same
  random_1 = Random(7); //Choose another random number
  }
object[random_1].SetPosition(37, 104);
.
.
.




How can I make sure that all positions are filled with different objects, that is, how to always have a different random number?

- Neo Jezh
Title: Re: Random Positioning - Objects (Memory Game)
Post by: Lt. Smash on Sun 24/08/2008 19:55:50
you could create a struct array with x, y coordinates in which you save your puzzle coordinates:

//script header
struct coordinates
{
  int x;
  int y;
  bool used;
};
import coordinates Point[MAX_POINTS];

//main script
coordinates Point[MAX_POINTS];
export Point;


then you use in :

Point[0].x = 12;
Point[0].y = 123;
...


[edit]Let's make it a bit nicer!
The mix() function:

//header
struct mem //in that struct you can add your functions for pair checking etc.
{
  import void mix();
  ...
};
import mem Memory;
//script
mem Memory; //on top
export Memory;

mem::mix()
{
  int i;
  while (i < MAX_POINTS) { //reseting memory
    Point[i].used = false;
    i++;
  }
  i = 0;
  int pos;
  while (i < MAX_POINTS) { //mixing the cards
    pos = Random (MAX_POINTS-1);
    if (!Point[pos].used)
    {
      Point[pos].used = true;
      object[i].SetPosition(Point[pos].x, Point[pos].y);
      i++;
    }
  }
}

in the room script then just use

Memory.mix();



[edit]tested and works!
Title: Re: Random Positioning - Objects (Memory Game)
Post by: Neo on Sun 24/08/2008 22:13:50
@Lt. Smash

Works like a charm. Thank you very much.
This could serve like a good tutorial of what you can do and how to use structs und arrays.

Thanks again ;)

- Neo Jezh