Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to produce a random number sequence that doesn't produce more than X consecutive elements
    text
    copied!<p>Ok, I really don't know how to frame the question properly because I barely have any idea how to describe what I want in one sentence and I apologize.</p> <p>Let me get straight to the point and you can just skip the rest cause I just want to show that I've tried something and not coming here to ask a question on a whim.</p> <p>I need an algorithm that produces 6 random numbers where it may not produce more than 2 consecutive numbers in that sequence.</p> <p>example: 3 3 4 4 2 1</p> <p>^FINE.</p> <p>example: 3 3 3 4 4 2</p> <p>^<strong>NO! <em>NO!</em></strong> WRONG!</p> <p>Obviously, I have no idea how to do this without tripping over myself constantly. </p> <p>Is there a STL or Boost feature that can do this? Or maybe someone here knows how to concoct an algorithm for it. That would be awesome.</p> <p><strong>What I'm trying to do and what I've tried.</strong>(the part you can skip)</p> <p>This is in C++. I'm trying to make a Panel de Pon/Tetris Attack/Puzzle League whatever clone for practice. The game has a 6 block row and 3 or more matching blocks will destroy the blocks. <a href="http://www.youtube.com/watch?v=zVKbhgQA3OY&amp;feature=related" rel="nofollow">Here's a video in case you're not familiar.</a></p> <p>When a new row comes from the bottom it must not come out with 3 horizontal matching blocks or else it will automatically disappear. Something I do not want for horizontal. Vertical is fine though.</p> <p>I've tried to accomplish just that and it appears I can't get it right. When I start the game chunks of blocks are missing because it detects a match when it shouldn't. My method is more than likely heavy handed and too convoluted as you'll see.</p> <pre><code>enum BlockType {EMPTY, STAR, UP_TRIANGLE, DOWN_TRIANGLE, CIRCLE, HEART, DIAMOND}; vector&lt;Block&gt; BlockField::ConstructRow() { vector&lt;Block&gt; row; int type = (rand() % 6)+1; for (int i=0;i&lt;6;i++) { row.push_back(Block(type)); type = (rand() % 6) +1; } // must be in order from last to first of the 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&lt;Block&gt; &amp;row, Block blockCheckArray[3]) { vector&lt;Block&gt;::iterator block1 = row.begin(); vector&lt;Block&gt;::iterator block2 = row.begin()+1; vector&lt;Block&gt;::iterator block3 = row.begin()+2; vector&lt;Block&gt;::iterator block4 = row.begin()+3; vector&lt;Block&gt;::iterator block5 = row.begin()+4; vector&lt;Block&gt;::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 &lt;= 0) type = 6; (*block1).AssignBlockType(type); } else if (equal(block2, block5, blockCheckArray)) { type = bt2 - 1; if (type &lt;= 0) type = 6; (*block2).AssignBlockType(type); } else if (equal(block3, block6, blockCheckArray)) { type = bt3 - 1; if (type == bt3) type--; if (type &lt;= 0) type = 6; (*block3).AssignBlockType(type); } else if (equal(block4, row.end(), blockCheckArray)) { type = bt4 - 1; if (type == bt3) type--; if (type &lt;= 0) type = 6; (*block4).AssignBlockType(type); } } </code></pre> <p><em>Sigh</em>, I'm not sure if it helps to show this...At least it shows that I've tried something.</p> <p>Basically, I construct the row by assigning random block types, described by the BlockType enum, to a Block object's constructor(a Block object has blockType and a position). </p> <p>Then I use a RowCheck function to see if there's 3 consecutive blockTypes in one row and I have do this for all block types. The *_match variables are arrays of 3 Block objects with the same block type. If I do find that there are 3 consecutive block types then, I just simply subtract the first value by one. However if I do that I might end up inadvertently producing another 3 match so I just make sure the block types are going in order from greatest to least.</p> <p>Ok, it's crappy, it's convoluted and it doesn't work! That's why I need your help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload