Storing cells in y_iterate.
#1

Basically I need to be able to select a random index from an iterator and use that index in that iterator and another.

For example, I have this to get the random index of one:
" new rand = random(Iter_Count(Frontier[0]))"
But how would I return that index from two iterators (Frontier[0] and Frontier[1])?

The iterator will always have the same amount, one is the X of a cell and the other is the Y of a cell. The index represents cells stored.

More parts of the code:

The iterator and inserting cells:
pawn Код:
new Iterator:Frontier[2]<MAX_CELL_SIZE>;
        Iter_Init(Frontier);
        for(new X=0;X<X_SIZE;X++) for(new Y=0;Y<Y_SIZE;Y++)
        {
            if(!(X==0||X==X_SIZE-1||Y==0||Y==Y_SIZE-1))
            {
                if(Current[0] != X && Current[1] != Y &&
                    (CELL[X][Y-1][in]&&CELL[X][Y+1][in]&&CELL[X-1][Y][in]&&CELL[X+1][Y][in]))
               
                //!WALLS_COLOR
                Iter_Add(Frontier[0], X);
                Iter_Add(Frontier[1], Y);
            }
        }
Where I need to draw a marble from the bag:
pawn Код:
new rand = random(Iter_Count(Frontier[0]))
            RandomCell[0] = //What do I put here to get data from the index (rand) of Frontier[0]?
            RandomCell[1] = //What do I put here to get data from the index (rand) of Frontier[1]?
I've tried (but got an expected token ';' error on both lines):
pawn Код:
new rand = random(Iter_Count(Frontier[0]))
            RandomCell[0] = Iter_Prev(Frontier[0], rand+1);
            RandomCell[1] = Iter_Prev(Frontier[1], rand+1);
Reply
#2

Quote:
Originally Posted by Crayder
Посмотреть сообщение
I've tried (but got an expected token ';' error on both lines):
pawn Код:
new rand = random(Iter_Count(Frontier[0]))
            RandomCell[0] = Iter_Prev(Frontier[0], rand+1);
            RandomCell[1] = Iter_Prev(Frontier[1], rand+1);
You clearly missed a ; there, so that might be the issue, I personally don't use y_iterate, so can't really say if that's the issue.
Reply
#3

As @up said, also in code I gave you earlier I used Iter_Random - it uses random interally, except that it returns -1 if there are no elements in ther iterator.
Reply
#4

Quote:
Originally Posted by Misiur
Посмотреть сообщение
As @up said, also in code I gave you earlier I used Iter_Random - it uses random interally, except that it returns -1 if there are no elements in ther iterator.
There will be elements no matter what in my case, and I'm using Iter_Random. The problem is that I need the same index of the value received from that for both iterators.

Oh, and the expected ';' was actually the line above... :P
I feel dumb for having mistaken that.
Reply
#5

foreach uses the values as indexes, so I think this could work
pawn Код:
new rand = Iter_Random(Frontier[0]));

if(Iter_Next(Frontier[0], rand) == Iter_Size(Frontier[0])) {
    rand = Iter_First(Frontier[0]);
}
RandomCell[0] = Iter_Next(Frontier[0], rand);
RandomCell[1] = Iter_Next(Frontier[1], rand);
Reply
#6

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
foreach uses the values as indexes, so I think this could work
pawn Код:
new rand = Iter_Random(Frontier[0]));

if(Iter_Next(Frontier[0], rand) == Iter_Size(Frontier[0])) {
    rand = Iter_First(Frontier[0]);
}
RandomCell[0] = Iter_Next(Frontier[0], rand);
RandomCell[1] = Iter_Next(Frontier[1], rand);
Thats almost exactly what I had, but wouldn't you subtract from rand? Doing so would remove the need to loop back to index one.

Anyways, now that that's off my plate I need a way to loop through all cells and store only frontier cells. My current method did not work. If you do not know what I mean go here.
Reply
#7

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Thats almost exactly what I had, but wouldn't you subtract from rand? Doing so would remove the need to loop back to index one.
You shouldn't subtract from rand because foreach isn't linear, it uses the cells as indexes

An iterator could look like that (it is sorted ascending)
array[sizeof array] = 3 (Iter_First), array[3] = 5, array[5] = 7, array[7] = sizeof array [thats the end]

It is a one way ticket, it is better to use Iter_Next which is just "array[value]"

Actually I think we should use Iter_Begin instead of Iter_First, as we want the first index not the first value
pawn Код:
new rand = Iter_Random(Frontier[0]));

if(Iter_Next(Frontier[0], rand) == Iter_Size(Frontier[0])) {
    rand = Iter_Begin(Frontier[0]);
}
RandomCell[0] = Iter_Next(Frontier[0], rand);
RandomCell[1] = Iter_Next(Frontier[1], rand);
Reply
#8

For future reference, there is a way without collection of iterators:
pawn Код:
new
    Matrix[X_SIZE][Y_SIZE],
    Iterator:Matrix<X_SIZE * Y_SIZE>
;

public OnGameModeInit()
{
    //Example cells:
    for(new x = 0; x != 3; ++x) {
        for(new y = 0; y != 3; ++y) {
            Matrix[x][y] = x * y;
            Iter_Add(Matrix, x * Y_SIZE + y);
        }
    }

    //Retrieving random cell:
    new
        rnd = Iter_Random(Matrix),
        x = rnd / Y_SIZE,
        y = rnd % Y_SIZE
    ;
   
    printf("Cell at (%d, %d) value is equal %d", x, y, Matrix[x][y]);

    return 1;
}
Reply
#9

Quote:
Originally Posted by ******
Посмотреть сообщение
From what you've written, I don't think what you want is possible at all with y_iterate, as it doesn't work the way you seem to think it does. Values ARE indexes, so if you randomly select the value "4" from one iterator, that is stored at index "4", and getting a value from the same slot in a different iterator will get you exactly the same value.
Oh I guess you're right, I always thought it stored the values in the iterator.

Quote:
Originally Posted by Misiur
Посмотреть сообщение
For future reference, there is a way without collection of iterators:
pawn Код:
new
    Matrix[X_SIZE][Y_SIZE],
    Iterator:Matrix<X_SIZE * Y_SIZE>
;

public OnGameModeInit()
{
    //Example cells:
    for(new x = 0; x != 3; ++x) {
        for(new y = 0; y != 3; ++y) {
            Matrix[x][y] = x * y;
            Iter_Add(Matrix, x * Y_SIZE + y);
        }
    }

    //Retrieving random cell:
    new
        rnd = Iter_Random(Matrix),
        x = rnd / Y_SIZE,
        y = rnd % Y_SIZE
    ;
   
    printf("Cell at (%d, %d) value is equal %d", x, y, Matrix[x][y]);

    return 1;
}
Ok, what I need now is a way to get frontier cells only.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)