Advertisement
dimipan80

Exams - Tetris Figures

Nov 19th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* In the classical Tetris game we have 7 Tetris figures (also called "tetriminos"): I, L, J, O, Z, S and T.
  2.  You are given a rectangular Tetris game field consisting of full end empty cells. Your task is to write
  3.  a JavaScript function to count the number of each of these 7 tetriminos (with overlapping, without rotations).
  4.  The input is passed as array of strings holding the game field lines. Each game field line holds only
  5.  two letters: '-' and 'o' (empty and full cells). All game field lines have the same length. Print at the console
  6.  the number of I, L, J, O, Z, S and T tetriminos found in the game field (with overlapping and without rotations)
  7.  as JSON string*/
  8.  
  9. "use strict";
  10.  
  11. function solve(args) {
  12.     var tetriminos = {"I": 0, "L": 0, "J": 0, "O": 0, "Z": 0, "S": 0, "T": 0};
  13.     for (var i = 0; i < args.length; i += 1) {
  14.         for (var j = 0; j < args[i].length; j += 1) {
  15.             if (args[i][j] == 'o') {
  16.                 tetriminosCounter(args, i, j, tetriminos);
  17.             }
  18.         }
  19.     }
  20.  
  21.     console.log(JSON.stringify(tetriminos));
  22.  
  23.     function tetriminosCounter(matrix, row, col, object) {
  24.         if (row + 3 < matrix.length && matrix[row + 1][col] == 'o' &&
  25.             matrix[row + 2][col] == 'o' && matrix[row + 3][col] == 'o') {
  26.             object.I += 1;
  27.         }
  28.  
  29.         if (row + 2 < matrix.length && col + 1 < matrix[row + 2].length &&
  30.             matrix[row + 1][col] == 'o' && matrix[row + 2][col] == 'o' &&
  31.             matrix[row + 2][col + 1] == 'o') {
  32.             object.L += 1;
  33.         }
  34.  
  35.         if (row + 2 < matrix.length && col - 1 >= 0 && matrix[row + 1][col] == 'o' &&
  36.             matrix[row + 2][col] == 'o' && matrix[row + 2][col - 1] == 'o') {
  37.             object.J += 1;
  38.         }
  39.  
  40.         if (row + 1 < matrix.length && col + 1 < matrix[row].length &&
  41.             matrix[row][col + 1] == 'o' && matrix[row + 1][col] == 'o' &&
  42.             matrix[row + 1][col + 1] == 'o') {
  43.             object.O += 1;
  44.         }
  45.  
  46.         if (row + 1 < matrix.length && col + 2 < matrix[row].length &&
  47.             matrix[row][col + 1] == 'o' && matrix[row + 1][col + 1] == 'o' &&
  48.             matrix[row + 1][col + 2] == 'o') {
  49.             object.Z += 1;
  50.         }
  51.  
  52.         if (row + 1 < matrix.length && col - 1 >= 0 && col + 1 < matrix[row].length &&
  53.             matrix[row][col + 1] == 'o' && matrix[row + 1][col] == 'o' &&
  54.             matrix[row + 1][col - 1] == 'o') {
  55.             object.S += 1;
  56.         }
  57.  
  58.         if (row + 1 < matrix.length && col + 2 < matrix[row].length &&
  59.             matrix[row][col + 1] == 'o' && matrix[row][col + 2] == 'o' &&
  60.             matrix[row + 1][col + 1] == 'o') {
  61.             object.T += 1;
  62.         }
  63.     }
  64. }
  65.  
  66. solve([
  67.     '--o--o-',
  68.     '--oo-oo',
  69.     'ooo-oo-',
  70.     '-ooooo-',
  71.     '---oo--'
  72. ]);
  73.  
  74. solve([
  75.     '-oo',
  76.     'ooo',
  77.     'ooo'
  78. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement