Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* In the classical Tetris game we have 7 Tetris figures (also called "tetriminos"): I, L, J, O, Z, S and T.
- You are given a rectangular Tetris game field consisting of full end empty cells. Your task is to write
- a JavaScript function to count the number of each of these 7 tetriminos (with overlapping, without rotations).
- The input is passed as array of strings holding the game field lines. Each game field line holds only
- two letters: '-' and 'o' (empty and full cells). All game field lines have the same length. Print at the console
- the number of I, L, J, O, Z, S and T tetriminos found in the game field (with overlapping and without rotations)
- as JSON string*/
- "use strict";
- function solve(args) {
- var tetriminos = {"I": 0, "L": 0, "J": 0, "O": 0, "Z": 0, "S": 0, "T": 0};
- for (var i = 0; i < args.length; i += 1) {
- for (var j = 0; j < args[i].length; j += 1) {
- if (args[i][j] == 'o') {
- tetriminosCounter(args, i, j, tetriminos);
- }
- }
- }
- console.log(JSON.stringify(tetriminos));
- function tetriminosCounter(matrix, row, col, object) {
- if (row + 3 < matrix.length && matrix[row + 1][col] == 'o' &&
- matrix[row + 2][col] == 'o' && matrix[row + 3][col] == 'o') {
- object.I += 1;
- }
- if (row + 2 < matrix.length && col + 1 < matrix[row + 2].length &&
- matrix[row + 1][col] == 'o' && matrix[row + 2][col] == 'o' &&
- matrix[row + 2][col + 1] == 'o') {
- object.L += 1;
- }
- if (row + 2 < matrix.length && col - 1 >= 0 && matrix[row + 1][col] == 'o' &&
- matrix[row + 2][col] == 'o' && matrix[row + 2][col - 1] == 'o') {
- object.J += 1;
- }
- if (row + 1 < matrix.length && col + 1 < matrix[row].length &&
- matrix[row][col + 1] == 'o' && matrix[row + 1][col] == 'o' &&
- matrix[row + 1][col + 1] == 'o') {
- object.O += 1;
- }
- if (row + 1 < matrix.length && col + 2 < matrix[row].length &&
- matrix[row][col + 1] == 'o' && matrix[row + 1][col + 1] == 'o' &&
- matrix[row + 1][col + 2] == 'o') {
- object.Z += 1;
- }
- if (row + 1 < matrix.length && col - 1 >= 0 && col + 1 < matrix[row].length &&
- matrix[row][col + 1] == 'o' && matrix[row + 1][col] == 'o' &&
- matrix[row + 1][col - 1] == 'o') {
- object.S += 1;
- }
- if (row + 1 < matrix.length && col + 2 < matrix[row].length &&
- matrix[row][col + 1] == 'o' && matrix[row][col + 2] == 'o' &&
- matrix[row + 1][col + 1] == 'o') {
- object.T += 1;
- }
- }
- }
- solve([
- '--o--o-',
- '--oo-oo',
- 'ooo-oo-',
- '-ooooo-',
- '---oo--'
- ]);
- solve([
- '-oo',
- 'ooo',
- 'ooo'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement