Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: modern Tetris randomization logic #4

Open
JobLeonard opened this issue Mar 31, 2019 · 2 comments
Open

Request: modern Tetris randomization logic #4

JobLeonard opened this issue Mar 31, 2019 · 2 comments

Comments

@JobLeonard
Copy link

Currently any piece can follow any piece. In modern Tetris implementations we instead get pieces in shuffled sequences, where each sequence contains every tetronimo piece exactly once. See also:

https://tetris.fandom.com/wiki/Random_Generator

Would make the game a lot more "fair" to play :)

@sneakernets
Copy link

Here's what I did for my terrible Commodore version: I created a "bag" and put each piece in twice, then I made sure you didn't get the same piece twice. It ended up being a good chunk of the program, though.

It's probably not the best way to do it but it sure kept the program from generating a deluge of S and Z pieces.

@JobLeonard
Copy link
Author

Why two though? Wouldn't the simplest way be to have a sequence of seven and shuffle that sequence every seven steps?

Currently, the logic for a new piece is:

// create a new piece, don't remove old one (it has landed and should stick)
void new_piece() {
  y = py = 0;
  p = rand() % 7;
  r = pr = rand() % 4;
  x = px = rand() % (10 - NUM(r, 16));
}

The key line being p = rand() % 7;

If I'm correct, something like this should work: (just wrote this of the top of my head, haven't really written C++ in a while so probably contains a mistake somewhere)

// index into bag sequence and bag of seven tetronimos
int n = 0, bag[7] = {0, 1, 2, 3, 4, 5, 6}

void new_piece() {
  y = py = 0;
  // Fisher-Yates shuffle the bag every time
  // one random sequence is finished
  if (n == 0) { 
    for(int i = 1; i < 7; i++) {
      int j = rand() % i;
      p = bag[j];
      bag[j] = bag[i];
      bag[i] = p;
    }
  }
  p = bag[n];
  n = (n + 1) % 7;
  r = pr = rand() % 4;
  x = px = rand() % (10 - NUM(r, 16));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants