Skip to content

Commit

Permalink
finish J clockwise test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ziloka committed Oct 21, 2023
1 parent 4f38978 commit ac462cf
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 47 deletions.
26 changes: 19 additions & 7 deletions scripts/wallkicks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
import * as cheerio from "https://esm.sh/cheerio@0.22.0";
const $ = cheerio.load(await Deno.readTextFile("input.txt"));

function generate_test_case(table, init_rot_indx, dest_rot_index) {
function get_rot_indx(s) {
if (s === "R") {
s = 1;
} else if (s === "L") {
s = 3;
}
return s
}

function generate_test_case(table, offset, init_rot_indx, dest_rot_index) {
init_rot_indx = get_rot_indx(init_rot_indx);
dest_rot_index = get_rot_indx(dest_rot_index);

const HEIGHT = 7;
const WIDTH = 5;

Expand Down Expand Up @@ -53,8 +65,8 @@ function generate_test_case(table, init_rot_indx, dest_rot_index) {

const format = (arr) => JSON.stringify(arr).replace(/"/g, '').replace(/\[/g, "vec![");

console.log(`// ${init_rot_indx}->${dest_rot_index}`);
console.log(`generate(${format(board)}, TETROMINO_TYPE, ${format(active_piece_initial)}, ${init_rot_indx}, ${format(active_piece_destination)})\n`);
console.log(`// ${init_rot_indx}->${dest_rot_index} ${offset}`);
console.log(`generate(${format(board)}, TETROMINO_TYPE, ${format(active_piece_initial)}, ${init_rot_indx}, ${format(active_piece_destination)});\n`);
// console.trace();
}

Expand All @@ -64,16 +76,16 @@ function generate_test_case(table, init_rot_indx, dest_rot_index) {
// document.querySelector("table[style=\"text-align:center;\"]").querySelector("tr[align=\"center\"]").querySelectorAll("td[width=\"74\"]:nth-child(n+7)");
$("table[style=\"text-align:center;\"]").each((i, tetrominoTests) => {
// if ([1, 3].includes(i)) return;
console.log(`Tetromino test#${i}`);
// console.log(`Tetromino test#${i}`);
$(tetrominoTests).find("tr[align=\"center\"]").each((j, row) => {
console.log(`On row #${j}`);
// console.log(`On row #${j}`);

const [init_rot_indx, dest_rot_indx] = $(row).find("th").text().trim().split("⇒");
// this finds the last two elements in each row, except for the I table(s)
$(row).find("td:nth-child(n+7)").each((_, table) => {
if ($(table).children().length == 0) return;
const offset = $(table).text();
generate_test_case(table, init_rot_indx, dest_rot_indx);
generate_test_case(table, $(table).text().trim(), init_rot_indx, dest_rot_indx);
});
});
console.log("\n");
});
138 changes: 98 additions & 40 deletions tests/wallkicks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ use tetris::tetris::{
};

// make sure the first element in the initial_pos is the origin cell
// if a test fails, to prevent head pain, make sure that the first piece of the initial_pos parameter is the tetromino origin point
// https://harddrop.com/wiki/File:SRS-true-rotations.png
fn generate(
matrix: Vec<Vec<Option<(u8, u8, u8)>>>,
tetromino_type: Tetromino,
initial_pos: Vec<Vec2>,
rotation_index: i8,
dest_pos: Vec<Vec2>,
) {
assert!(initial_pos.len() == 4 && initial_pos.len() == dest_pos.len(), "expected initial and dest vectors contain 4 cells, found: init: {}, dest: {}", initial_pos.len(), dest_pos.len());
assert!(
initial_pos.len() == 4 && initial_pos.len() == dest_pos.len(),
"expected initial and dest vectors contain 4 cells, found: init: {}, dest: {}",
initial_pos.len(),
dest_pos.len()
);

let mut board = Board::import(matrix, 0);
board.active_piece.tetromino = tetromino_type;
Expand All @@ -39,54 +46,105 @@ const N: Option<(u8, u8, u8)> = None;
fn tetromino_j_clockwise_wallkicks() {
// 0->R (-1, 0)
generate(
vec![vec![G,N,N,G,N],vec![G,N,N,N,N],vec![G,N,N,N,N],vec![G,G,N,N,N],vec![N,N,N,N,N],vec![N,N,N,N,N],vec![N,N,N,N,N]],
tetris::tetris::consts::Tetromino::L,
vec![ vec2(2., 1.), vec2(1., 1.), vec2(3., 1.), vec2(3., 2.)],
vec![
vec![G, N, N, G, N],
vec![G, N, N, N, N],
vec![G, N, N, N, N],
vec![G, G, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
],
Tetromino::J,
vec![vec2(2., 1.), vec2(3., 2.), vec2(1., 1.), vec2(3., 1.)],
0,
vec![ vec2(1., 2.), vec2(1., 1.), vec2(1., 0.), vec2(2., 0.)],
vec![vec2(1., 2.), vec2(1., 1.), vec2(1., 0.), vec2(2., 0.)],
);

// 0->R (-1, -2)
// 0->R (-1,-2)
generate(
vec![vec![G,N,N,G,G],vec![G,N,G,G,G],vec![G,N,G,G,G],vec![G,N,N,N,N],vec![G,G,N,N,N],vec![N,N,N,N,N],vec![N,N,N,N,N]],
tetris::tetris::consts::Tetromino::L,
vec![ vec2(2., 3.), vec2(1., 3.), vec2(3., 3.), vec2(3., 4.)],
vec![
vec![G, N, N, G, G],
vec![G, N, G, G, G],
vec![G, N, G, G, G],
vec![G, N, N, N, N],
vec![G, G, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
],
Tetromino::J,
vec![vec2(2., 3.), vec2(3., 4.), vec2(1., 3.), vec2(3., 3.)],
0,
vec![ vec2(1., 2.), vec2(1., 1.), vec2(1., 0.), vec2(2., 0.)],
vec![vec2(1., 2.), vec2(1., 1.), vec2(1., 0.), vec2(2., 0.)],
);

// R-> 2 (+1, -1)
let positive_1x_0y = vec![
vec![0, 0, 0, 0, 0],
vec![0, 0, 0, 0, 0],
vec![0, 0, 0, 0, 0],
vec![0, 0, 0, 0, 0],
vec![0, 1, 0, 2, 2],
vec![0, 1, 3, 3, 2],
vec![2, 1, 1, 2, 2],
];
// R->2 (+1,-1)
generate(
vec![
vec![G, N, G, G, G],
vec![G, N, N, N, G],
vec![N, N, N, G, G],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
],
Tetromino::J,
vec![vec2(1., 2.), vec2(1., 3.), vec2(1., 1.), vec2(2., 1.)],
1,
vec![vec2(3., 1.), vec2(1., 0.), vec2(1., 1.), vec2(2., 1.)],
);

// 2->L
let positive_1x_negative_2y = vec![
vec![0, 0, 0, 0, 0],
vec![0, 0, 0, 0, 0],
vec![0, 0, 2, 2, 2],
vec![0, 1, 1, 1, 2],
vec![0, 1, 3, 3, 2],
vec![2, 2, 2, 3, 2],
vec![2, 2, 2, 3, 2],
];
// R->2 (+1, 0)
generate(
vec![
vec![G, N, N, G, G],
vec![N, N, N, N, G],
vec![N, N, N, G, G],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
],
Tetromino::J,
vec![vec2(1., 1.), vec2(1., 2.), vec2(1., 0.), vec2(2., 0.)],
1,
vec![vec2(2., 1.), vec2(3., 1.), vec2(1., 1.), vec2(1., 0.)],
);

// L->0
let negative_1x_negative_1y = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[2, 2, 2, 1, 2],
[2, 3, 3, 1, 2],
];
// 2->L (+1,-2)
generate(
vec![
vec![G, G, G, N, G],
vec![G, G, G, N, G],
vec![N, N, N, N, G],
vec![N, N, N, N, G],
vec![N, N, G, G, G],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
],
Tetromino::J,
vec![vec2(2., 3.), vec2(1., 3.), vec2(3., 3.), vec2(1., 2.)],
2,
vec![vec2(2., 2.), vec2(3., 2.), vec2(3., 1.), vec2(3., 0.)],
);

// L->0 (-1,-1)
generate(
vec![
vec![G, N, N, N, G],
vec![G, G, G, N, G],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
vec![N, N, N, N, N],
],
Tetromino::J,
vec![vec2(3., 1.), vec2(2., 2.), vec2(3., 2.), vec2(3., 0.)],
3,
vec![vec2(1., 0.), vec2(2., 0.), vec2(3., 0.), vec2(3., 1.)],
);
}

#[test]
Expand Down

0 comments on commit ac462cf

Please sign in to comment.