~sircmpwn/tetrominoes

e1bdb09f2d2b5319c502ca245d37c4bb2a53b26a — Drew DeVault 2 years ago a3235b0
Spawn sets of shuffled tetrominoes

Instead of picking the next one randomly every time.
4 files changed, 30 insertions(+), 2 deletions(-)

M board.ha
M main.ha
M notes.txt
M update.ha
M board.ha => board.ha +20 -0
@@ 1,3 1,4 @@
use math::random;
use sdl2;

def BOARD_WIDTH: int = 10;


@@ 30,10 31,16 @@ type board = struct {
	cells: [BOARD_WIDTH * BOARD_HEIGHT]color,
	offs_x: int,
	offs_y: int,

	// Queue of tetrominoes to spawn, shuffled from [[tetrominoes]]
	tetrominoes: []int,
	next: size,

	active: [][TETROMINO_LEN]color,
	active_i: int,
	active_x: int,
	active_y: int,

	ghost_x: int,
	ghost_y: int,
};


@@ 127,6 134,19 @@ fn canmove(
	return true;
};

fn shuffle_tetrominoes(state: *state) void = {
	static let items = [0, 1, 2, 3, 4, 5, 6];
	const board = &state.board;
	for (let i = len(items) - 1; i > 0; i -= 1) {
		let j = random::next(&state.rand): size % i;
		let x = items[i];
		items[i] = items[j];
		items[j] = x;
	};
	board.tetrominoes = items[..];
	board.next = 0;
};

fn update_ghost(state: *state) void = {
	const board = &state.board;
	const cells = &board.cells;

M main.ha => main.ha +1 -0
@@ 125,6 125,7 @@ fn run() (void | fs::error | sdl2::error) = {
		sdl2::game_controller_close(c);
	};

	shuffle_tetrominoes(&state);
	do_spawn(&state, sdl2::get_ticks());

	for (state.run) {

M notes.txt => notes.txt +1 -1
@@ 1,5 1,5 @@
- tetromino swapping
- tetromino shuffling
- rotate pieces around center origin
- music
- sound effects
- level files & timings

M update.ha => update.ha +8 -1
@@ 75,7 75,12 @@ fn update(state: *state) (void | sdl2::error) = {
};

fn do_spawn(state: *state, now: u32) void = {
	let next = random::next(&state.rand): size % len(tetrominoes);
	let next = state.board.tetrominoes[state.board.next];
	state.board.next += 1;
	if (state.board.next >= len(state.board.tetrominoes)) {
		shuffle_tetrominoes(state);
	};

	let col = color::EMPTY;
	for (true) {
		let next = random::next(&state.rand): color - 1;


@@ 89,6 94,7 @@ fn do_spawn(state: *state, now: u32) void = {
	state.lastcolor = col;
	state.board.active = tetrominoes[next];
	state.board.active_i = 0;

	let active = &state.board.active;
	for (let i = 0z; i < len(active); i += 1) {
		let active = &active[i];


@@ 98,6 104,7 @@ fn do_spawn(state: *state, now: u32) void = {
			};
		};
	};

	let width = 0, height = 0;
	activesize(state, &width, &height);
	state.board.active_y = -height;