A random pouch system (infinite)

Hi!

I recently got obsessed with tetris and I just started making it on processing (full graphical version with hold and next-in-line feature. If you ever played a game of original tetris (or an accurate remake) you will know there is a “pouch” system. It serves to make every block appear exactly once every “pouch” (all 7 blocks picked at random). I’ve made a prototype that seems to be working just fine so if you ever struggle to make it (just like I did) feel free to use this code. If you have any improvements feel free to reply and i will make modifications (except for arraylists those things are not my thing and am struggling with those every since i started working with processing).

//TETRIS (optional) pouch mechanic. Reason for creation: hate tetris games without it :P
String blockline = ""; //the line of segments,serves as a way to see next X 
//(default 3, change in blockline.length() < cblock + 3) blocks aswell as a log for previous ones
int cblock = 0; //serves as a counter and a way to show your current block
//EDIT: cblock = 0 instead of 1! Now its fixed
void setup() {
  addPouch(100); //making a pouch before the start of the game (OPTIONAL but reccomended)
}

void draw() {
  //Checking if the blockline is about to run out (in tetris you can see the next 3 blocks,
  //but you have to generate a new pouch beforehand to keep seeing 3 in advance 
  if(blockline.length() < cblock + 3) { //(3 moves in advance)
    addPouch(100);
    println("new pouch generated");
  }
  println(blockline.charAt(cblock) + "    cblock = " + cblock);
  cblock+=1; //counter
  
  delay(1000); //just to observe the process as it is happening
}

void addPouch(int swaps) { 
  //adding pouches to blockline. Swaps means how well the pouch is shuffeled but the bigger the number the slower it gets.
  //its a bad design but simple and easy to understand. I chose this for simplicity
  char blocks[] = {'1','2','3','4','5','6','7'}; //all possible blocks.Feel free to add more, or even turn them into string (WARNNG [next line])
  /* WARNING FOR USING STRINGS
  If you want string i reccomend that in the for loop that puts them into blockline, you add a ',' into the "" (brakcets)
  so it is "," + blocks[i] . You need to use String words[] = split(blockline,",") when trying to get the block info from it.
  It splits the string everytime there is a ',' and makes an array out of it (INSANELY USEFULL!!!) 
  */
  for(int i = 0; i < swaps; i++) { //swapping 2 random blocks
    int char1 = floor(random(blocks.length));
    int char2 = floor(random(blocks.length));
    String placeHolder = "" + blocks[char2];
    blocks[int(char2)] = ( (blocks[char1]+"").charAt(0));
    blocks[int(char1)] = ( placeHolder.charAt(0));
  }
  for(int i = 0; i < blocks.length; i++) { //adding them to the blockline
    blockline += "" + blocks[i];
  }
}

this is it! Well it’s not optimised, its just a quick little subproject.

Again feel free to use it as you wish!

1 Like

ERROR IN MY CODE!!!
I just saw a bug while testing the program. You have to set cblock at the start of the code to 0 not 1!!!

It’s a mistake i forgot to fix while transforming the program from string operated (,3,1,5,3,2,5,6,7) to just line of numbers (31532567)!

Fix it if you want it to work accurately