Skip to content

Added randomizer #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/tetris.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@
paused = 0,
splash = 1


// To properly shuffle an array of 5, deciding next pieces
// In tetris the next 6 pieces are considered a packet
// Every time a packet ends a new one randomly generates
// All packets contain one of every type of pieces
// This way player knows that the piece he needs will be here in ,in worst case, 11 pieces aheadat most
function shuffle(array) {
let currentIndex = array.length, randomIndex;

// While there remain elements to shuffle.
while (currentIndex != 0) {

// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;

// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}

return array;
}


function fieldIndex( i, x, y )
{
return 10 * ( y + i / 4 << 0 ) + x + i % 4
Expand Down Expand Up @@ -165,6 +190,24 @@
fig1, fig2 )
}

function newFigurePack()
{
// 1 = I piece
// 2 = Z piece
// 3 = S piece
// 4 = O piece
// 5 is L piece
// 6 is J piece
var pack = shuffle([1,2,3,4,5,6])

// Couldn't figure out how you implemented piece system
// (I'm new to js :D)
// But if you use randomizer the gameplay will be way better
// I suffered a lot because of non-proper randomization :D
return pack
}


function testCollision( figure )
{
if ( !shapes[ figure.i ]
Expand Down