c++ - How to produce a random number sequence that doesn't produce more than X consecutive elements -


ok, don't know how frame question because barely have idea how describe want in 1 sentence , apologize.

let me straight point , can skip rest cause want show i've tried , not coming here ask question on whim.

i need algorithm produces 6 random numbers may not produce more 2 consecutive numbers in sequence.

example: 3 3 4 4 2 1

^fine.

example: 3 3 3 4 4 2

^no! no! wrong!

obviously, have no idea how without tripping on myself constantly.

is there stl or boost feature can this? or maybe here knows how concoct algorithm it. awesome.

what i'm trying , i've tried.(the part can skip)

this in c++. i'm trying make panel de pon/tetris attack/puzzle league whatever clone practice. game has 6 block row , 3 or more matching blocks destroy blocks. here's video in case you're not familiar.

when new row comes bottom must not come out 3 horizontal matching blocks or else automatically disappear. not want horizontal. vertical fine though.

i've tried accomplish , appears can't right. when start game chunks of blocks missing because detects match when shouldn't. method more heavy handed , convoluted you'll see.

enum blocktype {empty, star, up_triangle, down_triangle, circle, heart, diamond}; vector<block> blockfield::constructrow() {     vector<block> row;      int type = (rand() % 6)+1;      (int i=0;i<6;i++)     {         row.push_back(block(type));         type = (rand() % 6) +1;     }      // must in order last first of enumeration     rowcheck(row, diamond_match);     rowcheck(row, heart_match);     rowcheck(row, circle_match);     rowcheck(row, downtriangle_match);     rowcheck(row, uptriangle_match);     rowcheck(row, star_match);      return row; }  void blockfield::rowcheck(vector<block> &row, block blockcheckarray[3]) {     vector<block>::iterator block1 = row.begin();     vector<block>::iterator block2 = row.begin()+1;     vector<block>::iterator block3 = row.begin()+2;     vector<block>::iterator block4 = row.begin()+3;     vector<block>::iterator block5 = row.begin()+4;     vector<block>::iterator block6 = row.begin()+5;      int bt1 = (*block1).blocktype();     int bt2 = (*block2).blocktype();     int bt3 = (*block3).blocktype();     int bt4 = (*block4).blocktype();     int type = 0;      if (equal(block1, block4, blockcheckarray))      {         type = bt1 - 1;         if (type <= 0) type = 6;         (*block1).assignblocktype(type);     }     else if (equal(block2, block5, blockcheckarray))      {         type = bt2 - 1;         if (type <= 0) type = 6;         (*block2).assignblocktype(type);     }     else if (equal(block3, block6, blockcheckarray))      {         type = bt3 - 1;         if (type == bt3) type--;         if (type <= 0) type = 6;         (*block3).assignblocktype(type);     }     else if (equal(block4, row.end(), blockcheckarray))      {         type = bt4 - 1;         if (type == bt3) type--;         if (type <= 0) type = 6;          (*block4).assignblocktype(type);     } } 

sigh, i'm not sure if helps show this...at least shows i've tried something.

basically, construct row assigning random block types, described blocktype enum, block object's constructor(a block object has blocktype , position).

then use rowcheck function see if there's 3 consecutive blocktypes in 1 row , have block types. *_match variables arrays of 3 block objects same block type. if find there 3 consecutive block types then, subtract first value one. if might end inadvertently producing 3 match make sure block types going in order greatest least.

ok, it's crappy, it's convoluted , doesn't work! that's why need help.

it should suffice keep record of previous 2 values, , loop when newly generated 1 matches both of previous values.

for arbitrary run length, make sense size history buffer on fly , comparisons in loop well. should close matching requirements.

int type, type_old, type_older;  type_older = (rand() % 6)+1; row.push_back(block(type_older));  type_old = (rand() % 6)+1; row.push_back(block(type_old));  (int i=2; i<6; i++) {     type = (rand() % 6) +1;     while ((type == type_old) && (type == type_older)) {         type = (rand() % 6) +1;     }      row.push_back(block(type));     type_older = type_old;     type_old = type; } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -